gmyth-stream/server/0.3/lib/file_handler.py
author renatofilho
Tue Aug 21 16:04:44 2007 +0100 (2007-08-21)
branchtrunk
changeset 815 7f290a3a34b1
parent 776 9add73d88bce
permissions -rw-r--r--
[svn r821] created debian packages;\nImplemented server deamon;\n
     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             dat_file = output_file + ".dat";
    36             dat_path = os.path.join (utils.config.get_transcoded_location(),
    37                 dat_file);
    38 
    39             output = open(dat_path, "wb")
    40             # dumps data using the highest protocol
    41             pickle.dump(self.opts, output, -1)
    42             output.close()
    43         else:
    44             name = os.path.splitext(os.path.basename(filename))[0]
    45             dat_file = name + ".dat";
    46             dat_path = os.path.join (utils.config.get_transcoded_location(),
    47                 dat_file);
    48             pkl_file = open(dat_path, "rb")
    49             self.opts = pickle.load(pkl_file)
    50     # __init__()
    51 
    52 # TranscodedFile
    53 
    54 
    55 class FileList(list):
    56     """Class to hold file's list - reimplements str and repr."""
    57     def __str__(self):
    58         ret = ""
    59         if len(self) > 0:
    60             for item in self:
    61                 ret = ret + "%s" % item
    62         return ret
    63     # __str__()
    64 
    65     def __repr__(self):
    66         return self.__str__()
    67     # __repr__()
    68 
    69 # FileList
    70 
    71 def list_media_files(directory, file_list):
    72     """Show all the media files with extension defined in the var 'ext'
    73     that are in the directory, appending each one to 'file_list'."""
    74     ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
    75     for root, dirs, files in os.walk(directory):
    76         for name in files:
    77             if os.path.splitext(name)[1].strip(".") in ext:
    78                 dat_file = os.path.join(sys.path[0],root,
    79                                         os.path.splitext(name)[0]+".dat")
    80 
    81                 if name not in file_list and \
    82                        os.path.exists(dat_file):
    83                     file_list.append(name)
    84 
    85     return True
    86 # list_media_files()