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