Tidy up code, stonger error checking.

This commit is contained in:
Samuel Findlay 2012-05-02 19:14:30 +10:00
parent ecc943ae20
commit 8877484aea
1 changed files with 78 additions and 61 deletions

View File

@ -72,67 +72,67 @@ class PowerSongImport(SongImport):
def doImport(self): def doImport(self):
""" """
Receive a single file or a list of files to import. Receive a list of files to import.
""" """
if isinstance(self.importSource, list): if not isinstance(self.importSource, list):
self.importWizard.progressBar.setMaximum(len(self.importSource)) return
for file in self.importSource: self.importWizard.progressBar.setMaximum(len(self.importSource))
if self.stopImportFlag: for file in self.importSource:
return if self.stopImportFlag:
self.setDefaults() return
with open(file, 'rb') as self.song_file: self.setDefaults()
# Get title and check file is valid PowerSong song format parse_error = False
with open(file, 'rb') as self.song_file:
# Get title to check file is valid PowerSong song format
label, field = self.readLabelField()
if label == u'TITLE':
self.title = field.replace(u'\n', u' ')
else:
self.logError(file, unicode(
translate('SongsPlugin.PowerSongSongImport', \
'Invalid PowerSong file. Missing "TITLE" header.')))
continue
# Get rest of fields from file
while True:
label, field = self.readLabelField() label, field = self.readLabelField()
if label != u'TITLE': if not label:
self.logError(file, unicode( break
translate('SongsPlugin.PowerSongSongImport', if label == u'AUTHOR':
('Invalid PowerSong song file. Missing ' self.parseAuthor(field)
'"TITLE" header.')))) elif label == u'COPYRIGHTLINE':
continue found_copyright = True
self.parseCopyrightCCLI(field)
elif label == u'PART':
self.addVerse(field)
else: else:
self.title = field.replace(u'\n', u' ') parse_error = True
while label:
label, field = self.readLabelField()
# Get the author(s)
if label == u'AUTHOR':
self.parseAuthor(field)
# Get copyright and look for CCLI number
elif label == u'COPYRIGHTLINE':
found_copyright = True
copyright, sep, ccli_no = field.rpartition(u'CCLI')
if not sep:
copyright = ccli_no
ccli_no = u''
if copyright:
self.addCopyright(copyright.rstrip(
u'\n').replace(u'\n', u' '))
if ccli_no:
ccli_no = ccli_no.strip(u' :')
if ccli_no.isdigit():
self.ccliNumber = ccli_no
# Get verse(s)
elif label == u'PART':
self.addVerse(field)
# Check for copyright label
if not found_copyright:
self.logError(file, unicode( self.logError(file, unicode(
translate('SongsPlugin.PowerSongSongImport', translate('SongsPlugin.PowerSongSongImport', \
('"%s" Invalid PowerSong song file. Missing ' '"%s" Invalid PowerSong file. Unknown header: "%s".'
'"COPYRIGHTLINE" string.' % self.title)))) % (self.title, label))))
continue break
# Check for at least one verse if parse_error:
if not self.verses: continue
self.logError(file, unicode( # Check that file had COPYRIGHTLINE label
translate('SongsPlugin.PowerSongSongImport', if not found_copyright:
('"%s" No verses found. Missing "PART" string.' self.logError(file, unicode(
% self.title)))) translate('SongsPlugin.PowerSongSongImport', \
continue '"%s" Invalid PowerSong file. Missing "COPYRIGHTLINE" \
if not self.finish(): header.' % self.title)))
self.logError(file) continue
# Check that file had at least one verse
if not self.verses:
self.logError(file, unicode(
translate('SongsPlugin.PowerSongSongImport', \
'"%s" Verses not found. Missing "PART" header.'
% self.title)))
continue
if not self.finish():
self.logError(file)
def readLabelField(self): def readLabelField(self):
""" """
Return as a 2-tuple the next two variable-length strings from song file Read (as a 2-tuple) the next two variable-length strings
""" """
label = unicode(self.song_file.read( label = unicode(self.song_file.read(
self.readLength()), u'utf-8', u'ignore') self.readLength()), u'utf-8', u'ignore')
@ -145,13 +145,30 @@ class PowerSongImport(SongImport):
def readLength(self): def readLength(self):
""" """
Return the byte-length of the next variable-length string in song file Read the byte-length of the next variable-length string
If at the end of the file, returns 0.
""" """
this_byte_char = self.song_file.read(1) this_byte = self.song_file.read(1)
if not this_byte_char: if not this_byte:
return 0 return 0
this_byte = ord(this_byte_char) this_byte_val = ord(this_byte)
if this_byte < 128: if this_byte_val < 128:
return this_byte return this_byte_val
else: else:
return (self.readLength() * 128) + (this_byte - 128) return (self.readLength() * 128) + (this_byte_val - 128)
def parseCopyrightCCLI(self, field):
"""
Look for CCLI song number, and get copyright
"""
copyright, sep, ccli_no = field.rpartition(u'CCLI')
if not sep:
copyright = ccli_no
ccli_no = u''
if copyright:
self.addCopyright(copyright.rstrip(u'\n').replace(u'\n', u' '))
if ccli_no:
ccli_no = ccli_no.strip(u' :')
if ccli_no.isdigit():
self.ccliNumber = ccli_no