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