gmyth-stream/server/0.3/lib/file_handler.py
author renatofilho
Sat Jun 30 00:25:48 2007 +0100 (2007-06-30)
branchtrunk
changeset 762 1be4b68bfb0e
parent 727 72f9152d4e2a
child 774 366b0fd1eead
permissions -rw-r--r--
[svn r768] implemented chuk mode for http
     1 #!/usr/bin/env python
     2 
     3 __author__ = "Artur Duque de Souza"
     4 __author_email__ = "artur.souza@indt.org.br"
     5 __license__ = "GPL"
     6 __version__ = "0.1"
     7 
     8 import os
     9 import sys
    10 import pickle
    11 import lib.utils as utils
    12 
    13 from stat import *
    14 
    15 __all__ = ("FileList", "list_media_files")
    16 
    17 
    18 class TranscodedFile(object):
    19     """This class creates and reads information about transcoded files."""
    20     opts = {}
    21 
    22     def __init__(self, filename, args):
    23         if filename == "" or not os.path.exists(filename):
    24             self.opts = args.copy()
    25 
    26             if self.opts["type"][0] != "myth":
    27                 self.opts["original_mtime"] = os.path.getmtime(
    28                     self.opts["uri"][0])
    29 
    30             name = os.path.basename(self.opts["uri"][0])
    31             self.opts["original"] = name
    32             output_file = os.path.basename(self.opts["outfile"][0])
    33             output = open(".transcoded/%s.dat" % \
    34                           os.path.splitext(output_file)[0], "wb")
    35             # dumps data using the highest protocol
    36             pickle.dump(self.opts, output, -1)
    37             output.close()
    38         else:
    39             name = os.path.splitext(os.path.basename(filename))[0]
    40             pkl_file = open(".transcoded/%s.dat" % name, "rb")
    41             self.opts = pickle.load(pkl_file)
    42     # __init__()
    43 
    44 # TranscodedFile
    45 
    46 
    47 class FileList(list):
    48     """Class to hold file's list - reimplements str and repr."""
    49     def __str__(self):
    50         ret = ""
    51         if len(self) > 0:
    52             for item in self:
    53                 ret = ret + "%s" % item
    54         return ret
    55     # __str__()
    56 
    57     def __repr__(self):
    58         return self.__str__()
    59     # __repr__()
    60 
    61 # FileList
    62 
    63 def list_media_files(directory, file_list):
    64     """Show all the media files with extension defined in the var 'ext'
    65     that are in the directory, appending each one to 'file_list'."""
    66     ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
    67     for root, dirs, files in os.walk(directory):
    68         for name in files:
    69             if os.path.splitext(name)[1].strip(".") in ext:
    70                 dat_file = os.path.join(sys.path[0],root,
    71                                         os.path.splitext(name)[0]+".dat")
    72 
    73                 if name not in file_list and \
    74                        os.path.exists(dat_file):
    75                     file_list.append(name)
    76 
    77     return True
    78 # list_media_files()