gmyth-stream/server/0.3/lib/file_handler.py
author renatofilho
Thu Aug 16 14:46:11 2007 +0100 (2007-08-16)
branchtrunk
changeset 807 add4025ca678
parent 775 7cd98c74da22
child 815 7f290a3a34b1
permissions -rw-r--r--
[svn r813] created a timeout function for invalid channels
     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 logging
    12 import lib.utils as utils
    13 
    14 from stat import *
    15 
    16 __all__ = ("FileList", "list_media_files")
    17 
    18 
    19 class TranscodedFile(object):
    20     """This class creates and reads information about transcoded files."""
    21     opts = {}
    22     log = logging.getLogger("gms.file_handler")
    23 
    24     def __init__(self, filename, args):
    25         if filename == "" or not os.path.exists(filename):
    26             self.opts = args.copy()
    27 
    28             if self.opts["type"][0] != "myth":
    29                 self.opts["original_mtime"] = os.path.getmtime(
    30                     self.opts["uri"][0])
    31 
    32             name = os.path.basename(self.opts["uri"][0])
    33             self.opts["original"] = [name]
    34             output_file = os.path.basename(self.opts["outfile"][0])
    35             output = open(".transcoded/%s.dat" % \
    36                           os.path.splitext(output_file)[0], "wb")
    37             # dumps data using the highest protocol
    38             pickle.dump(self.opts, output, -1)
    39             output.close()
    40         else:
    41             name = os.path.splitext(os.path.basename(filename))[0]
    42             pkl_file = open(".transcoded/%s.dat" % name, "rb")
    43             self.opts = pickle.load(pkl_file)
    44     # __init__()
    45 
    46 # TranscodedFile
    47 
    48 
    49 class FileList(list):
    50     """Class to hold file's list - reimplements str and repr."""
    51     def __str__(self):
    52         ret = ""
    53         if len(self) > 0:
    54             for item in self:
    55                 ret = ret + "%s" % item
    56         return ret
    57     # __str__()
    58 
    59     def __repr__(self):
    60         return self.__str__()
    61     # __repr__()
    62 
    63 # FileList
    64 
    65 def list_media_files(directory, file_list):
    66     """Show all the media files with extension defined in the var 'ext'
    67     that are in the directory, appending each one to 'file_list'."""
    68     ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
    69     for root, dirs, files in os.walk(directory):
    70         for name in files:
    71             if os.path.splitext(name)[1].strip(".") in ext:
    72                 dat_file = os.path.join(sys.path[0],root,
    73                                         os.path.splitext(name)[0]+".dat")
    74 
    75                 if name not in file_list and \
    76                        os.path.exists(dat_file):
    77                     file_list.append(name)
    78 
    79     return True
    80 # list_media_files()