gmyth-stream/server/lib/file_handler.py
branchtrunk
changeset 897 715f854a93cd
parent 815 7f290a3a34b1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/server/lib/file_handler.py	Tue Jan 29 20:12:30 2008 +0000
     1.3 @@ -0,0 +1,86 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +__author__ = "Artur Duque de Souza"
     1.7 +__author_email__ = "artur.souza@indt.org.br"
     1.8 +__license__ = "GPL"
     1.9 +__version__ = "0.1"
    1.10 +
    1.11 +import os
    1.12 +import sys
    1.13 +import pickle
    1.14 +import logging
    1.15 +import lib.utils as utils
    1.16 +
    1.17 +from stat import *
    1.18 +
    1.19 +__all__ = ("FileList", "list_media_files")
    1.20 +
    1.21 +
    1.22 +class TranscodedFile(object):
    1.23 +    """This class creates and reads information about transcoded files."""
    1.24 +    opts = {}
    1.25 +    log = logging.getLogger("gms.file_handler")
    1.26 +
    1.27 +    def __init__(self, filename, args):
    1.28 +        if filename == "" or not os.path.exists(filename):
    1.29 +            self.opts = args.copy()
    1.30 +
    1.31 +            if self.opts["type"][0] != "myth":
    1.32 +                self.opts["original_mtime"] = os.path.getmtime(
    1.33 +                    self.opts["uri"][0])
    1.34 +
    1.35 +            name = os.path.basename(self.opts["uri"][0])
    1.36 +            self.opts["original"] = [name]
    1.37 +            output_file = os.path.basename(self.opts["outfile"][0])
    1.38 +            dat_file = output_file + ".dat";
    1.39 +            dat_path = os.path.join (utils.config.get_transcoded_location(),
    1.40 +                dat_file);
    1.41 +
    1.42 +            output = open(dat_path, "wb")
    1.43 +            # dumps data using the highest protocol
    1.44 +            pickle.dump(self.opts, output, -1)
    1.45 +            output.close()
    1.46 +        else:
    1.47 +            name = os.path.splitext(os.path.basename(filename))[0]
    1.48 +            dat_file = name + ".dat";
    1.49 +            dat_path = os.path.join (utils.config.get_transcoded_location(),
    1.50 +                dat_file);
    1.51 +            pkl_file = open(dat_path, "rb")
    1.52 +            self.opts = pickle.load(pkl_file)
    1.53 +    # __init__()
    1.54 +
    1.55 +# TranscodedFile
    1.56 +
    1.57 +
    1.58 +class FileList(list):
    1.59 +    """Class to hold file's list - reimplements str and repr."""
    1.60 +    def __str__(self):
    1.61 +        ret = ""
    1.62 +        if len(self) > 0:
    1.63 +            for item in self:
    1.64 +                ret = ret + "%s" % item
    1.65 +        return ret
    1.66 +    # __str__()
    1.67 +
    1.68 +    def __repr__(self):
    1.69 +        return self.__str__()
    1.70 +    # __repr__()
    1.71 +
    1.72 +# FileList
    1.73 +
    1.74 +def list_media_files(directory, file_list):
    1.75 +    """Show all the media files with extension defined in the var 'ext'
    1.76 +    that are in the directory, appending each one to 'file_list'."""
    1.77 +    ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
    1.78 +    for root, dirs, files in os.walk(directory):
    1.79 +        for name in files:
    1.80 +            if os.path.splitext(name)[1].strip(".") in ext:
    1.81 +                dat_file = os.path.join(sys.path[0],root,
    1.82 +                                        os.path.splitext(name)[0]+".dat")
    1.83 +
    1.84 +                if name not in file_list and \
    1.85 +                       os.path.exists(dat_file):
    1.86 +                    file_list.append(name)
    1.87 +
    1.88 +    return True
    1.89 +# list_media_files()