Refactor exception handling

This commit is contained in:
Samuel Findlay 2012-06-06 00:12:19 +10:00
parent 2fb09b2462
commit 34de12040f
1 changed files with 37 additions and 42 deletions

View File

@ -88,11 +88,10 @@ class ZionWorxImport(SongImport):
'Error reading CSV file.')), 'Error reading CSV file.')),
unicode(translate('SongsPlugin.ZionWorxImport', unicode(translate('SongsPlugin.ZionWorxImport',
'Line %d: %s' % (songs_reader.line_num, e)))) 'Line %d: %s' % (songs_reader.line_num, e))))
else: return
num_records = len(records) num_records = len(records)
log.info(u'%s records found in CSV file' % num_records) log.info(u'%s records found in CSV file' % num_records)
self.importWizard.progressBar.setMaximum(num_records) self.importWizard.progressBar.setMaximum(num_records)
try:
for index, record in enumerate(records, 1): for index, record in enumerate(records, 1):
if self.stopImportFlag: if self.stopImportFlag:
return return
@ -100,19 +99,21 @@ class ZionWorxImport(SongImport):
try: try:
self.title = self._decode(record[u'Title1']) self.title = self._decode(record[u'Title1'])
if record[u'Title2']: if record[u'Title2']:
self.alternateTitle = self._decode( self.alternateTitle = self._decode(record[u'Title2'])
record[u'Title2'])
self.parseAuthor(self._decode(record[u'Writer'])) self.parseAuthor(self._decode(record[u'Writer']))
self.addCopyright(self._decode( self.addCopyright(self._decode(record[u'Copyright']))
record[u'Copyright']))
lyrics = self._decode(record[u'Lyrics']) lyrics = self._decode(record[u'Lyrics'])
except UnicodeDecodeError, e: except UnicodeDecodeError, e:
self.logError(unicode(translate( self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport', 'SongsPlugin.ZionWorxImport', 'Record %d' % index)),
'Decoding error.')),
unicode(translate('SongsPlugin.ZionWorxImport', unicode(translate('SongsPlugin.ZionWorxImport',
'Record %d: %s' % (index, e)))) 'Decoding error: %s' % e)))
else: continue
except TypeError, e:
self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport', 'File not valid ZionWorx '
'CSV format.')), u'TypeError: %s' % e)
return
verse = u'' verse = u''
for line in lyrics.splitlines(): for line in lyrics.splitlines():
if line and not line.isspace(): if line and not line.isspace():
@ -125,14 +126,8 @@ class ZionWorxImport(SongImport):
title = self.title title = self.title
if not self.finish(): if not self.finish():
self.logError(unicode(translate( self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport', 'SongsPlugin.ZionWorxImport', 'Record %d' % index))
'Record %d' % index))
+ (u': "' + title + u'"' if title else u'')) + (u': "' + title + u'"' if title else u''))
except TypeError, e:
self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport',
'File not valid ZionWorx CSV format.')),
u'TypeError: %s' % e)
def _decode(self, str): def _decode(self, str):
""" """