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