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,51 +88,46 @@ 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)
for index, record in enumerate(records, 1):
if self.stopImportFlag:
return
self.setDefaults()
try: try:
for index, record in enumerate(records, 1): self.title = self._decode(record[u'Title1'])
if self.stopImportFlag: if record[u'Title2']:
return self.alternateTitle = self._decode(record[u'Title2'])
self.setDefaults() self.parseAuthor(self._decode(record[u'Writer']))
try: self.addCopyright(self._decode(record[u'Copyright']))
self.title = self._decode(record[u'Title1']) lyrics = self._decode(record[u'Lyrics'])
if record[u'Title2']: except UnicodeDecodeError, e:
self.alternateTitle = self._decode( self.logError(unicode(translate(
record[u'Title2']) 'SongsPlugin.ZionWorxImport', 'Record %d' % index)),
self.parseAuthor(self._decode(record[u'Writer'])) unicode(translate('SongsPlugin.ZionWorxImport',
self.addCopyright(self._decode( 'Decoding error: %s' % e)))
record[u'Copyright'])) continue
lyrics = self._decode(record[u'Lyrics'])
except UnicodeDecodeError, e:
self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport',
'Decoding error.')),
unicode(translate('SongsPlugin.ZionWorxImport',
'Record %d: %s' % (index, e))))
else:
verse = u''
for line in lyrics.splitlines():
if line and not line.isspace():
verse += line + u'\n'
elif verse:
self.addVerse(verse)
verse = u''
if verse:
self.addVerse(verse)
title = self.title
if not self.finish():
self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport',
'Record %d' % index))
+ (u': "' + title + u'"' if title else u''))
except TypeError, e: except TypeError, e:
self.logError(unicode(translate( self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport', 'SongsPlugin.ZionWorxImport', 'File not valid ZionWorx '
'File not valid ZionWorx CSV format.')), 'CSV format.')), u'TypeError: %s' % e)
u'TypeError: %s' % e) return
verse = u''
for line in lyrics.splitlines():
if line and not line.isspace():
verse += line + u'\n'
elif verse:
self.addVerse(verse)
verse = u''
if verse:
self.addVerse(verse)
title = self.title
if not self.finish():
self.logError(unicode(translate(
'SongsPlugin.ZionWorxImport', 'Record %d' % index))
+ (u': "' + title + u'"' if title else u''))
def _decode(self, str): def _decode(self, str):
""" """