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

View File

@ -72,67 +72,67 @@ class PowerSongImport(SongImport):
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):
self.importWizard.progressBar.setMaximum(len(self.importSource))
for file in self.importSource:
if self.stopImportFlag:
return
self.setDefaults()
with open(file, 'rb') as self.song_file:
# Get title and check file is valid PowerSong song format
if not isinstance(self.importSource, list):
return
self.importWizard.progressBar.setMaximum(len(self.importSource))
for file in self.importSource:
if self.stopImportFlag:
return
self.setDefaults()
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()
if label != u'TITLE':
self.logError(file, unicode(
translate('SongsPlugin.PowerSongSongImport',
('Invalid PowerSong song file. Missing '
'"TITLE" header.'))))
continue
if not label:
break
if label == u'AUTHOR':
self.parseAuthor(field)
elif label == u'COPYRIGHTLINE':
found_copyright = True
self.parseCopyrightCCLI(field)
elif label == u'PART':
self.addVerse(field)
else:
self.title = field.replace(u'\n', u' ')
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:
parse_error = True
self.logError(file, unicode(
translate('SongsPlugin.PowerSongSongImport',
('"%s" Invalid PowerSong song file. Missing '
'"COPYRIGHTLINE" string.' % self.title))))
continue
# Check for at least one verse
if not self.verses:
self.logError(file, unicode(
translate('SongsPlugin.PowerSongSongImport',
('"%s" No verses found. Missing "PART" string.'
% self.title))))
continue
if not self.finish():
self.logError(file)
translate('SongsPlugin.PowerSongSongImport', \
'"%s" Invalid PowerSong file. Unknown header: "%s".'
% (self.title, label))))
break
if parse_error:
continue
# Check that file had COPYRIGHTLINE label
if not found_copyright:
self.logError(file, unicode(
translate('SongsPlugin.PowerSongSongImport', \
'"%s" Invalid PowerSong file. Missing "COPYRIGHTLINE" \
header.' % self.title)))
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):
"""
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(
self.readLength()), u'utf-8', u'ignore')
@ -145,13 +145,30 @@ class PowerSongImport(SongImport):
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)
if not this_byte_char:
this_byte = self.song_file.read(1)
if not this_byte:
return 0
this_byte = ord(this_byte_char)
if this_byte < 128:
return this_byte
this_byte_val = ord(this_byte)
if this_byte_val < 128:
return this_byte_val
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