morphbr@723: #!/usr/bin/env python morphbr@723: morphbr@723: __author__ = "Artur Duque de Souza" morphbr@723: __author_email__ = "artur.souza@indt.org.br" morphbr@723: __license__ = "GPL" morphbr@723: __version__ = "0.1" morphbr@723: morphbr@723: import os morphbr@727: import sys morphbr@723: import pickle morphbr@723: import lib.utils as utils morphbr@723: morphbr@723: from stat import * morphbr@723: morphbr@723: __all__ = ("FileList", "list_media_files") morphbr@723: morphbr@723: morphbr@723: class TranscodedFile(object): morphbr@723: """This class creates and reads information about transcoded files.""" morphbr@723: opts = {} morphbr@723: morphbr@723: def __init__(self, filename, args): morphbr@723: if filename == "" or not os.path.exists(filename): morphbr@723: self.opts = args.copy() morphbr@723: morphbr@726: if self.opts["type"][0] != "myth": morphbr@727: self.opts["original_mtime"] = os.path.getmtime( morphbr@727: self.opts["uri"][0]) morphbr@726: morphbr@726: name = os.path.basename(self.opts["uri"][0]) morphbr@726: self.opts["original"] = name morphbr@727: output_file = os.path.basename(self.opts["outfile"][0]) morphbr@726: output = open(".transcoded/%s.dat" % \ morphbr@727: os.path.splitext(output_file)[0], "wb") morphbr@723: # dumps data using the highest protocol morphbr@723: pickle.dump(self.opts, output, -1) morphbr@723: output.close() morphbr@723: else: morphbr@723: name = os.path.splitext(os.path.basename(filename))[0] morphbr@723: pkl_file = open(".transcoded/%s.dat" % name, "rb") morphbr@723: self.opts = pickle.load(pkl_file) morphbr@723: # __init__() morphbr@723: morphbr@723: # TranscodedFile morphbr@723: morphbr@723: morphbr@723: class FileList(list): morphbr@723: """Class to hold file's list - reimplements str and repr.""" morphbr@723: def __str__(self): morphbr@723: ret = "" morphbr@723: if len(self) > 0: morphbr@723: for item in self: morphbr@731: ret = ret + "%s" % item morphbr@723: return ret morphbr@723: # __str__() morphbr@723: morphbr@723: def __repr__(self): morphbr@723: return self.__str__() morphbr@723: # __repr__() morphbr@723: morphbr@723: # FileList morphbr@723: morphbr@723: def list_media_files(directory, file_list): morphbr@723: """Show all the media files with extension defined in the var 'ext' morphbr@723: that are in the directory, appending each one to 'file_list'.""" morphbr@723: ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov'] morphbr@723: for root, dirs, files in os.walk(directory): morphbr@723: for name in files: morphbr@723: if os.path.splitext(name)[1].strip(".") in ext: morphbr@727: dat_file = os.path.join(sys.path[0],root, morphbr@727: os.path.splitext(name)[0]+".dat") morphbr@727: morphbr@727: if name not in file_list and \ morphbr@727: os.path.exists(dat_file): morphbr@724: file_list.append(name) morphbr@723: morphbr@723: return True morphbr@723: # list_media_files()