Skip to content

Commit

Permalink
Improved error reporting support.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fkrull committed May 26, 2011
1 parent db07087 commit 47fe7b4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
23 changes: 21 additions & 2 deletions rgain/rgio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# kate: indent-width 4; indent-mode python;
#
# Copyright (c) 2009, 2010 Felix Krull <[email protected]>
#
Expand All @@ -25,6 +26,9 @@
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()
Expand All @@ -47,7 +51,9 @@ def parse_peak(string):
# 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
Expand All @@ -72,6 +78,8 @@ def read_gain_data(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
Expand All @@ -88,6 +96,8 @@ def rg_write_gain(filename, trackdata, albumdata):
# 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
Expand All @@ -108,6 +118,8 @@ def read_gain_data(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,
Expand All @@ -129,6 +141,8 @@ def mp3_ql_write_gain(filename, trackdata, albumdata):
# 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
Expand Down Expand Up @@ -156,6 +170,8 @@ def read_gain_data(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,
Expand All @@ -179,6 +195,8 @@ def write_gain_data(desc, gaindata):
# 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
Expand All @@ -204,6 +222,8 @@ def read_gain_data(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
Expand Down Expand Up @@ -301,4 +321,3 @@ def write_gain(self, filename, trackgain, albumgain):
raise UnknownFiletype(ext)

accessor[1](filename, trackgain, albumgain)

14 changes: 9 additions & 5 deletions rgain/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from optparse import OptionParser
import traceback

from rgain.rgio import AudioFormatError


stdout_encoding = sys.stdout.encoding or sys.getfilesystemencoding()
def ou(arg):
Expand All @@ -34,18 +36,20 @@ def un(arg, encoding):


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():
Expand Down

0 comments on commit 47fe7b4

Please sign in to comment.