From 5872034ebf758a4104d4b36400556a26a1420c9e Mon Sep 17 00:00:00 2001 From: yomguy <> Date: Wed, 23 May 2007 16:59:52 +0000 Subject: [PATCH] - Fixed #26 - Create ExportProcessError class in export.api --- telemeta/export/api.py | 18 ++++++++- telemeta/export/core.py | 42 +++++++++++--------- telemeta/export/flac.py | 18 ++------- telemeta/export/mp3.py | 33 +++++++--------- telemeta/export/ogg.py | 45 +++++++++------------ telemeta/export/wav.py | 87 ++++++++++++++++++----------------------- 6 files changed, 115 insertions(+), 128 deletions(-) diff --git a/telemeta/export/api.py b/telemeta/export/api.py index e5e1a06f..8e031d81 100644 --- a/telemeta/export/api.py +++ b/telemeta/export/api.py @@ -12,7 +12,8 @@ # Author: Olivier Guilyardi # Guillaume Pellerin -from telemeta.core import * +from telemeta.core import Interface, TelemetaError + class IExporter(Interface): """Export driver interface""" @@ -68,3 +69,18 @@ class IExporter(Interface): It should be possible to make subsequent calls to process() with different items, using the same driver instance. """ + +class ExportProcessError(TelemetaError): + + def __init__(self, message, command, subprocess): + self.message = message + self.command = command + self.subprocess = subprocess + + def __str__(self): + error = self.subprocess.stderr.read() + return "%s ; command: %s; error: %s" % (self.message, + self.command, + error) + + \ No newline at end of file diff --git a/telemeta/export/core.py b/telemeta/export/core.py index e7666646..00e87b13 100644 --- a/telemeta/export/core.py +++ b/telemeta/export/core.py @@ -107,7 +107,6 @@ class ExporterCore(Component): file_name = get_file_name(self.source) file_name_wo_ext, file_ext = split_file_name(file_name) self.cache_dir = cache_dir - self.metadata = metadata #self.collection = self.metadata['Collection'] #self.artist = self.metadata['Artist'] @@ -131,47 +130,52 @@ class ExporterCore(Component): # Define and create the destination path # At the moment, the target directory is built with this scheme in # the cache directory : ./%Format/%Collection/%Artist/ - self.cache_dir = os.path.join(self.cache_dir,'cache') + #self.cache_dir = os.path.join(self.cache_dir,'cache') #export_dir = os.path.join(self.ext,self.collection,self.artist) export_dir = os.path.join(self.cache_dir,self.ext) if not os.path.exists(export_dir): export_dir_split = export_dir.split(os.sep) - path = export_dir_split[0] + path = os.sep + export_dir_split[0] for _dir in export_dir_split[1:]: path = os.path.join(path,_dir) if not os.path.exists(path): os.mkdir(path) else: - self.dest = export_dir + path = export_dir # Set the target file #target_file = file_name_wo_ext+'.'+self.ext target_file = self.item_id+'.'+self.ext - self.dest = os.path.join(self.dest,target_file) - return self.dest + dest = os.path.join(path,target_file) + return dest - def core_process(self,command,buffer_size,dest): + def core_process(self, command, buffer_size, dest): """Streams encoded audio data through a generator""" __chunk = 0 file_out = open(dest,'w') - proc = subprocess.Popen(command, - shell = True, - bufsize = buffer_size, - stdin = subprocess.PIPE, - stdout = subprocess.PIPE, - close_fds = True) - - __chunk = proc.stdout.read(buffer_size) - yield __chunk - file_out.write(__chunk) + try: + proc = subprocess.Popen(command, + shell = True, + bufsize = buffer_size, + stdin = subprocess.PIPE, + stdout = subprocess.PIPE, + close_fds = True) + except: + raise ExportProcessError('Command failure:', command, proc) + # Processing - while __chunk: + while True: __chunk = proc.stdout.read(buffer_size) + status = proc.poll() + if status != None and status != 0: + raise ExportProcessError('Command failure:', command, proc) + if len(__chunk) == 0: + break yield __chunk file_out.write(__chunk) @@ -181,7 +185,7 @@ class ExporterCore(Component): def post_process(self, item_id, source, metadata, ext, cache_dir, options=None): """ Post processing : write tags, print infos, etc...""" - self.write_tags() + #self.write_tags() if not options is None: if 'verbose' in self.options and self.options['verbose'] != '0': print self.dest diff --git a/telemeta/export/flac.py b/telemeta/export/flac.py index ddb9ad82..4626e167 100644 --- a/telemeta/export/flac.py +++ b/telemeta/export/flac.py @@ -111,35 +111,25 @@ class FlacExporter(ExporterCore): '| flac '+self.args+' -c -' # Pre-proccessing - #try: self.dest = self.pre_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) - #except: - #raise 'ExporterError [3]: pre_process' # Processing (streaming + cache writing) - try: - stream = self.core_process(self.command,self.buffer_size,self.dest) - for chunk in stream: - yield chunk - except: - raise 'ExporterError: core_process' + stream = self.core_process(self.command,self.buffer_size,self.dest) + for chunk in stream: + yield chunk # Post-proccessing - try: - self.post_process(self.item_id, + self.post_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) - except: - raise 'ExporterError: post_process' - # Encoding #os.system('flac '+args+' -o "'+self.dest+'" "'+ \ diff --git a/telemeta/export/mp3.py b/telemeta/export/mp3.py index 0a0affa3..77643120 100644 --- a/telemeta/export/mp3.py +++ b/telemeta/export/mp3.py @@ -62,6 +62,9 @@ class Mp3Exporter(ExporterCore): def get_description(self): return "FIXME" + def set_cache_dir(self,path): + self.cache_dir = path + def get_file_info(self): try: file_out1, file_out2 = os.popen4('mp3info "'+self.dest+'"') @@ -118,7 +121,7 @@ class Mp3Exporter(ExporterCore): #if tag in self.dub2args_dict.keys(): #arg = self.dub2args_dict[tag] #value = clean_word(self.metadata[tag]) - #args = args + ' --' + arg + ' "' +value +'" ' + #args = args.append(' --' + arg + ' "' +value +'" ' return args @@ -129,38 +132,30 @@ class Mp3Exporter(ExporterCore): self.args = self.get_args(self.metadata,options) self.ext = self.get_file_extension() self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \ - '| lame '+self.args+' - -' + '| lame '+self.args+' - ' # Pre-proccessing - try: - self.dest = self.pre_process(self.item_id, + self.dest = self.pre_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) - except: - raise 'ExporterError [3]: pre_process' # Processing (streaming + cache writing) - try: - stream = self.core_process(self.command,self.buffer_size,self.dest) - for chunk in stream: - yield chunk - except: - raise 'ExporterError: core_process' - - # Post-proccessing - try: - self.post_process(self.item_id, + # FIXME return stream + stream = self.core_process(self.command,self.buffer_size,self.dest) + for chunk in stream: + yield chunk + + # Post-proccessing + self.post_process(self.item_id, self.source, self.metadata, self.ext, self.cache_dir, self.options) - except: - raise 'ExporterError: post_process' - + # Encoding # os.system('lame '+args+' --tc "default" "'+self.source+ # '" "'+self.dest+'"') diff --git a/telemeta/export/ogg.py b/telemeta/export/ogg.py index 08e96607..5d6b239e 100644 --- a/telemeta/export/ogg.py +++ b/telemeta/export/ogg.py @@ -57,8 +57,8 @@ class OggExporter(ExporterCore): except IOError: return 'Exporter error [1]: file does not exist.' - #def set_cache_dir(self,path): - # self.cache_dir = path + def set_cache_dir(self,path): + self.cache_dir = path def decode(self): try: @@ -106,36 +106,27 @@ class OggExporter(ExporterCore): '| oggenc '+self.args+' -' # Pre-proccessing - try: - self.dest = self.pre_process(self.item_id, - self.source, - self.metadata, - self.ext, - self.cache_dir, - self.options) - except: - raise 'ExporterError [3]: pre_process' + + self.dest = self.pre_process(self.item_id, + self.source, + self.metadata, + self.ext, + self.cache_dir, + self.options) # Processing (streaming + cache writing) - try: - stream = self.core_process(self.command,self.buffer_size,self.dest) - for chunk in stream: - yield chunk - except: - raise 'ExporterError: core_process' + stream = self.core_process(self.command,self.buffer_size,self.dest) + for chunk in stream: + yield chunk # Post-proccessing - try: - self.post_process(self.item_id, - self.source, - self.metadata, - self.ext, - self.cache_dir, - self.options) - except: - raise 'ExporterError: post_process' - + self.post_process(self.item_id, + self.source, + self.metadata, + self.ext, + self.cache_dir, + self.options) # Post-proccessing #os.system('sox "'+self.source+'" -w -r 44100 -t wav -c2 - \ diff --git a/telemeta/export/wav.py b/telemeta/export/wav.py index 3881787f..c323fa90 100644 --- a/telemeta/export/wav.py +++ b/telemeta/export/wav.py @@ -55,12 +55,6 @@ class WavExporter(ExporterCore): return 'Exporter error [1]: file does not exist.' def set_cache_dir(self,path): - """Set the directory where cached files should be stored. Does nothing - if the exporter doesn't support caching. - - The driver shouldn't assume that this method will always get called. A - temporary directory should be used if that's not the case. - """ self.cache_dir = path def decode(self): @@ -106,52 +100,49 @@ class WavExporter(ExporterCore): if not options is None: self.options = options - try: - # Pre-proccessing - self.ext = self.get_file_extension() - self.dest = self.pre_process(self.item_id, - self.source, - self.metadata, - self.ext, - self.cache_dir, - self.options) - - # Initializing - chunk = 0 - file_in = open(self.source,'rb') - file_out = open(self.dest,'w') - + + # Pre-proccessing + self.ext = self.get_file_extension() + self.dest = self.pre_process(self.item_id, + self.source, + self.metadata, + self.ext, + self.cache_dir, + self.options) + + # Initializing + chunk = 0 + file_in = open(self.source,'rb') + file_out = open(self.dest,'w') + + chunk = file_in.read(self.buffer_size) + yield chunk + file_out.write(chunk) + + # Core Processing + while chunk: chunk = file_in.read(self.buffer_size) yield chunk file_out.write(chunk) - - # Core Processing - while chunk: - chunk = file_in.read(self.buffer_size) - yield chunk - file_out.write(chunk) - - file_in.close() - file_out.close() - - # Create the md5 key - #if 'md5' in self.metadata and self.metadata['md5']: - self.create_md5_key() - - # Create the par2 key - #if 'par2' in self.metadata and self.metadata['par2']: - self.create_par_key() - - # Pre-proccessing - self.post_process(self.item_id, - self.source, - self.metadata, - self.ext, - self.cache_dir, - self.options) - except IOError: - raise 'ExporterError [3]: source file does not exist.' + file_in.close() + file_out.close() + + # Create the md5 key + #if 'md5' in self.metadata and self.metadata['md5']: + self.create_md5_key() + + # Create the par2 key + #if 'par2' in self.metadata and self.metadata['par2']: + self.create_par_key() + + # Pre-proccessing + self.post_process(self.item_id, + self.source, + self.metadata, + self.ext, + self.cache_dir, + self.options) -- 2.39.5