Commits

Felix Krull  committed 7149df9

Add proper handling for GST errors.

  • Participants
  • Parent commits b76159e

Comments (0)

Files changed (5)

File rgain/__init__.py

         return other is not None and (self.gain == other.gain and
             self.peak == other.peak and self.ref_level == other.ref_level)
 
+class GSTError(Exception):
+    def __init__(self, gerror, debug):
+        self.domain = gerror.domain
+        self.code = gerror.code
+        self.message = gerror.message
+        self.debug = debug
+
+    def __unicode__(self):
+        return u"GST error: %s (%s)" % (self.message, self.debug)

File rgain/rgcalc.py

 import gst
 import gobject
 
-from rgain import GainData
+from rgain import GainData, GSTError
 
 GST_TAG_REFERENCE_LEVEL = "replaygain-reference-level"
 
     
     __gsignals__ = {
         "all-finished": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                         (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT)),
+            (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT)),
         "track-started": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                          (gobject.TYPE_STRING,)),
+            (gobject.TYPE_STRING,)),
         "track-finished": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
-                           (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
+            (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
+        "error": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
+            (gobject.TYPE_PYOBJECT,)),
     }
     
     
             if ret:
                 self.rg.set_locked_state(False)
                 self.pipe.set_state(gst.STATE_PLAYING)
-
+        elif msg.type == gst.MESSAGE_ERROR:
+            self.rg.set_locked_state(True)
+            self.pipe.set_state(gst.STATE_NULL)
+            err, debug = msg.parse_error()
+            self.emit("error", GSTError(err, debug))
 
 
 def calculate(*args, **kwargs):
     the same arguments, but setups its own main loop and returns the results
     once everything's finished.
     """
+    exc_slot = [None]
+
     def on_finished(evsrc, trackdata, albumdata):
         # all done
         loop.quit()
-    
+
+    def on_error(evsrc, exc):
+        exc_slot[0] = exc
+        loop.quit()
     rg = ReplayGain(*args, **kwargs)
     rg.connect("all-finished", on_finished)
+    rg.connect("error", on_error)
     loop = gobject.MainLoop()
     rg.start()
     loop.run()
+    if exc_slot[0] is not None:
+        raise exc_slot[0]
     return (rg.track_data, rg.album_data)
-

File rgain/script/__init__.py

             return unicode(u"".join(traceback.format_exception(*self.exc_info)))
 
     def _output_full_exception(self):
-        return self.exc_info[0] not in [IOError, rgain.rgio.AudioFormatError]
+        return self.exc_info[0] not in [IOError, rgain.rgio.AudioFormatError,
+            rgain.GSTError]
 
 
 def common_options():

File rgain/script/collectiongain.py

     try:
         tags = mutagen.File(properpath)
     except Exception, exc:
-        raise Error(u"%s: error - %s" % (filepath, exc))
+        raise Error(u"%s: %s" % (filepath, exc))
 
     album_id = None
     if ext == ".mp3":
             print ou(u"%s:" % job_key[1]),
         do_gain(files, ref_level, force, dry_run, album, mp3_format)
         print
+    except Exception, exc:
+        print
+        print >> stdout, ou(unicode(exc))
     finally:
         sys.stdout = old_stdout
         sys.stderr = old_stderr
             [os.path.join(music_dir, path) for path in single_tracks],
             ref_level, force, dry_run, False, mp3_format])
         num_jobs += 1
-        #do_gain((os.path.join(music_dir, path) for path in single_tracks),
-        #        ref_level, force, dry_run, False, mp3_format)
-        # update cache information
-        #if not dry_run:
-        #    update_cache(files, music_dir, single_tracks, None)
-        #print
 
     for album_id, album_files in albums.iteritems():
         #print ou(u"%s:" % album_id),
             [os.path.join(music_dir, path) for path in album_files],
             ref_level, force, dry_run, True, mp3_format])
         num_jobs += 1
-        #do_gain((os.path.join(music_dir, path) for path in album_files),
-        #        ref_level, force, dry_run, True, mp3_format)
-        # update cache
-        #if not dry_run:
-        #    update_cache(files, music_dir, album_files, album_id)
-        #print
     pool.close()
 
     print "Now waiting for results ..."
         do_collectiongain(args[0], opts.ref_level, opts.force, opts.dry_run,
                           opts.mp3_format, opts.ignore_cache, opts.jobs)
     except Error, exc:
+        print
         print >> sys.stderr, ou(unicode(exc))
         sys.exit(1)
     except KeyboardInterrupt:

File rgain/script/replaygain.py

     # this has to be done here since Gstreamer hooks into the command line
     # arguments if it's imported on module level
     from rgain import rgcalc
+
+    exc_slot = [None]
     
     # handlers
     def on_finished(evsrc, trackdata, albumdata):
             print "%.2f dB" % gaindata.gain
         else:
             print "done"
+
+    def on_error(evsrc, exc):
+        exc_slot[0] = exc
+        loop.quit()
     
     rg = rgcalc.ReplayGain(files, True, ref_level)
     rg.connect("all-finished", on_finished)
     rg.connect("track-started", on_trk_started)
     rg.connect("track-finished", on_trk_finished)
+    rg.connect("error", on_error)
     loop = gobject.MainLoop()
     rg.start()
     loop.run()
+    if exc_slot[0] is not None:
+        raise exc_slot[0]
     return rg.track_data, rg.album_data
 
 
             try:
                 trackdata, albumdata = formats_map.read_gain(filename)
             except Exception, exc:
-                raise Error(u"%s: error - %s" % (filename, exc))
+                raise Error(u"%s: %s" % (filename, exc))
             else:
                 if trackdata and albumdata:
                     print "track and album"
             try:
                 formats_map.write_gain(filename, trackdata, albumdata)
             except Exception, exc:
-                raise Error(u"%s: error - %s" % (filename, exc))
+                raise Error(u"%s: %s" % (filename, exc))
             else:
                 print "done"
     
             do_gain(args, opts.ref_level, opts.force, opts.dry_run, opts.album,
                     opts.mp3_format)
         except Error, exc:
+            print
             print >> sys.stderr, ou(unicode(exc))
             sys.exit(1)
         except KeyboardInterrupt: