Felix Krull avatar Felix Krull committed 1fd632c

Improved error reporting support.

Notably, only report the short exception message for common and known
exceptions (IOError and new AudioFormatError), but full tracebacks for all other
exceptions in all cases.
Also, as noted, if Mutagen fails to open a file properly (because it's not what
its extension purports to be), an AudioFormatError is raised, which gets printed
in short form.

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
+# kate: indent-width 4; indent-mode python;
 # 
 # Copyright (c) 2009, 2010 Felix Krull <f_krull@gmx.de>
 # 
 from rgain import GainData
 
 
+class AudioFormatError(Exception):
+    def __init__(self, filename):
+        Exception.__init__(self, u"Did not understand file: %s" % filename)
 
 def parse_db(string):
     string = string.strip()
 # Flac files at least (also WavPack it seems)
 def rg_read_gain(filename):
     tags = mutagen.File(filename)
-    
+    if tags is None:
+        raise AudioFormatError(filename)
+
     def read_gain_data(desc):
         gain_tag = u"replaygain_%s_gain" % desc
         peak_tag = u"replaygain_%s_peak" % desc
 
 def rg_write_gain(filename, trackdata, albumdata):
     tags = mutagen.File(filename)
+    if tags is None:
+        raise AudioFormatError(filename)
     
     if trackdata:
         tags[u"replaygain_track_gain"] = u"%.8f dB" % trackdata.gain
 # ID3 for Quod Libet
 def mp3_ql_read_gain(filename):
     tags = ID3(filename)
+    if tags is None:
+        raise AudioFormatError(filename)
     
     def read_gain_data(desc):
         tag = u"RVA2:%s" % desc
 
 def mp3_ql_write_gain(filename, trackdata, albumdata):
     tags = ID3(filename)
+    if tags is None:
+        raise AudioFormatError(filename)
     
     if trackdata:
         trackgain = RVA2(desc=u"track", channel=1, gain=trackdata.gain,
 # ID3 for foobar2000
 def mp3_fb2k_read_gain(filename):
     tags = ID3(filename)
+    if tags is None:
+        raise AudioFormatError(filename)
     
     def read_gain_data(desc):
         gain_tag = u"TXXX:replaygain_%s_gain" % desc
 
 def mp3_fb2k_write_gain(filename, trackdata, albumdata):
     tags = ID3(filename)
+    if tags is None:
+        raise AudioFormatError(filename)
     
     def write_gain_data(desc, gaindata):
         gain_frame = TXXX(encoding=3, desc=u"replaygain_%s_gain" % desc,
 # I hope this works
 def mp3_mp3gain_read_gain(filename):
     tags = APEv2File(filename)
+    if tags is None:
+        raise AudioFormatError(filename)
     
     def read_gain_data(desc):
         gain_tag = u"replaygain_%s_gain" % desc
 
 def mp3_mp3gain_write_gain(filename, trackdata, albumdata):
     tags = APEv2File(filename)
+    if tags is None:
+        raise AudioFormatError(filename)
     
     if trackdata:
         tags[u"replaygain_track_gain"] = u"%.8f dB" % trackdata.gain
             raise UnknownFiletype(ext)
         
         accessor[1](filename, trackgain, albumgain)
-

rgain/script/__init__.py

 from optparse import OptionParser
 import traceback
 
+from rgain.rgio import AudioFormatError
+
 
 stdout_encoding = sys.stdout.encoding or sys.getfilesystemencoding()
 def ou(arg):
 
 
 class Error(Exception):
-    def __init__(self, message):
+    def __init__(self, message, exc_info=None):
         Exception.__init__(self, message)
         # as long as instances are only constructed in exception handlers, this
         # should get us what we want
-        self.exc_info = sys.exc_info()
+        self.exc_info = exc_info if exc_info else sys.exc_info()
     
     def __unicode__(self):
-        # not a particularly good metric
-        if not __debug__:
+        if not self._output_full_exception():
             return Exception.__unicode__(self)
         else:
-            return unicode(traceback.format_exception(*self.exc_info))
+            return unicode(u"".join(traceback.format_exception(*self.exc_info)))
+
+    def _output_full_exception(self):
+        return self.exc_info[0] not in [IOError, AudioFormatError]
 
 
 def common_options():
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.