]> git.parisson.com Git - telemeta.git/commitdiff
- Fixed #26
authoryomguy <>
Wed, 23 May 2007 16:59:52 +0000 (16:59 +0000)
committeryomguy <>
Wed, 23 May 2007 16:59:52 +0000 (16:59 +0000)
- Create ExportProcessError class in export.api

telemeta/export/api.py
telemeta/export/core.py
telemeta/export/flac.py
telemeta/export/mp3.py
telemeta/export/ogg.py
telemeta/export/wav.py

index e5e1a06fca58e3a4e9875eb8b95b9963ddd1b2f3..8e031d81edfde0d78f131636dfc5c2e726e4a860 100644 (file)
@@ -12,7 +12,8 @@
 # Author: Olivier Guilyardi <olivier@samalyse.com>
 #         Guillaume Pellerin <pellerin@parisson.com>
 
-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
index e7666646b81cba6369ca474ed5f3da71b5001cca..00e87b13ca34210608fc70d2024215d3158dce62 100644 (file)
@@ -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
index ddb9ad82809ac95c055bdb0c6f6ef2f88855f8f8..4626e16745143cb50512ab09dfa2523da89d6538 100644 (file)
@@ -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+'" "'+ \
index 0a0affa35a135907919a0241edff3850dffdf7e6..77643120862e287963e2ac3ed4a3b2abe7af2aa9 100644 (file)
@@ -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+'"')
index 08e96607388e09f1bb5bf6926a3539b1f10719c7..5d6b239eafb4c9e36767dd6303db8d9979544591 100644 (file)
@@ -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 - \
index 3881787fdd5f6c87cb8aac8ce599230bd364fd4b..c323fa908e1ffa283903e9d2228e6d2cbde5bfaa 100644 (file)
@@ -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)