forked from openlp/openlp
Tidy up code, stonger error checking.
This commit is contained in:
parent
ecc943ae20
commit
8877484aea
@ -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):
|
||||||
|
return
|
||||||
self.importWizard.progressBar.setMaximum(len(self.importSource))
|
self.importWizard.progressBar.setMaximum(len(self.importSource))
|
||||||
for file in self.importSource:
|
for file in self.importSource:
|
||||||
if self.stopImportFlag:
|
if self.stopImportFlag:
|
||||||
return
|
return
|
||||||
self.setDefaults()
|
self.setDefaults()
|
||||||
|
parse_error = False
|
||||||
with open(file, 'rb') as self.song_file:
|
with open(file, 'rb') as self.song_file:
|
||||||
# Get title and check file is valid PowerSong song format
|
# Get title to check file is valid PowerSong song format
|
||||||
label, field = self.readLabelField()
|
label, field = self.readLabelField()
|
||||||
if label != u'TITLE':
|
if label == u'TITLE':
|
||||||
self.logError(file, unicode(
|
|
||||||
translate('SongsPlugin.PowerSongSongImport',
|
|
||||||
('Invalid PowerSong song file. Missing '
|
|
||||||
'"TITLE" header.'))))
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
self.title = field.replace(u'\n', u' ')
|
self.title = field.replace(u'\n', u' ')
|
||||||
while label:
|
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()
|
||||||
# Get the author(s)
|
if not label:
|
||||||
|
break
|
||||||
if label == u'AUTHOR':
|
if label == u'AUTHOR':
|
||||||
self.parseAuthor(field)
|
self.parseAuthor(field)
|
||||||
# Get copyright and look for CCLI number
|
|
||||||
elif label == u'COPYRIGHTLINE':
|
elif label == u'COPYRIGHTLINE':
|
||||||
found_copyright = True
|
found_copyright = True
|
||||||
copyright, sep, ccli_no = field.rpartition(u'CCLI')
|
self.parseCopyrightCCLI(field)
|
||||||
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':
|
elif label == u'PART':
|
||||||
self.addVerse(field)
|
self.addVerse(field)
|
||||||
# Check for copyright label
|
else:
|
||||||
|
parse_error = True
|
||||||
|
self.logError(file, unicode(
|
||||||
|
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:
|
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. Missing "COPYRIGHTLINE" \
|
||||||
'"COPYRIGHTLINE" string.' % self.title))))
|
header.' % self.title)))
|
||||||
continue
|
continue
|
||||||
# Check for at least one verse
|
# Check that file had at least one verse
|
||||||
if not self.verses:
|
if not self.verses:
|
||||||
self.logError(file, unicode(
|
self.logError(file, unicode(
|
||||||
translate('SongsPlugin.PowerSongSongImport',
|
translate('SongsPlugin.PowerSongSongImport', \
|
||||||
('"%s" No verses found. Missing "PART" string.'
|
'"%s" Verses not found. Missing "PART" header.'
|
||||||
% self.title))))
|
% self.title)))
|
||||||
continue
|
continue
|
||||||
if not self.finish():
|
if not self.finish():
|
||||||
self.logError(file)
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user