more exceptions and a few other changes

This commit is contained in:
Mattias Põldaru 2011-01-18 00:27:25 +02:00
parent f090c5409f
commit 0c38bccd45
2 changed files with 77 additions and 84 deletions

View File

@ -79,7 +79,7 @@ class EasiSlidesImport(SongImport):
success = self._parse_song(song) success = self._parse_song(song)
if not success or self.stop_import_flag: if not success or self.stop_import_flag:
return False return False
if self.commit: elif self.commit:
self.finish() self.finish()
return True return True
@ -97,14 +97,18 @@ class EasiSlidesImport(SongImport):
def _add_title(self, song): def _add_title(self, song):
try: try:
self.title = unicode(song.Title1).strip() self.title = unicode(song.Title1).strip()
except: except UnicodeDecodeError:
log.info(u'no Title1') log.exception(u'Title1 unicode decode error')
except AttributeError:
log.exception(u'no Title1')
self._success = False self._success = False
def _add_alttitle(self, song): def _add_alttitle(self, song):
try: try:
self.alternate_title = unicode(self.song.Title2).strip() self.alternate_title = unicode(song.Title2).strip()
except: except UnicodeDecodeError:
log.exception(u'Title2 unicode decode error')
except AttributeError:
pass pass
def _add_number(self, song): def _add_number(self, song):
@ -112,74 +116,80 @@ class EasiSlidesImport(SongImport):
number = int(song.SongNumber) number = int(song.SongNumber)
if number != 0: if number != 0:
self.song_number = number self.song_number = number
print number except UnicodeDecodeError:
except: log.exception(u'SongNumber unicode decode error')
except AttributeError:
pass pass
def _add_authors(self, song): def _add_authors(self, song):
try: try:
authors = unicode(song.Writer).strip().split(u',') authors = unicode(song.Writer).split(u',')
for author in authors: for author in authors:
self.authors.append(author.strip()) author = author.strip()
except: if len(author) > 0:
self.authors.append(author)
except UnicodeDecodeError:
log.exception(u'Writer unicode decode error')
except AttributeError:
pass pass
def _add_copyright(self, song): def _add_copyright(self, song):
copyright = [] copyright = []
try: try:
copyright.append(unicode(song.Copyright).strip()) copyright.append(unicode(song.Copyright).strip())
except: except UnicodeDecodeError:
log.exception(u'Copyright unicode decode error')
except AttributeError:
pass pass
try: try:
copyright.append(unicode(song.LicenceAdmin1).strip()) copyright.append(unicode(song.LicenceAdmin1).strip())
except: except UnicodeDecodeError:
log.exception(u'LicenceAdmin1 unicode decode error')
except AttributeError:
pass pass
try: try:
copyright.append(unicode(song.LicenceAdmin2).strip()) copyright.append(unicode(song.LicenceAdmin2).strip())
except: except UnicodeDecodeError:
log.exception(u'LicenceAdmin2 unicode decode error')
except AttributeError:
pass pass
self.add_copyright(u' '.join(copyright)) self.add_copyright(u' '.join(copyright))
def _add_book(self, song): def _add_book(self, song):
try: try:
self.song_book_name = unicode(song.BookReference).strip() self.song_book_name = unicode(song.BookReference).strip()
except: except UnicodeDecodeError:
log.exception(u'BookReference unicode decode error')
except AttributeError:
pass pass
def _parse_and_add_lyrics(self, song): def _parse_and_add_lyrics(self, song):
try: try:
lyrics = unicode(song.Contents).strip() lyrics = unicode(song.Contents).strip()
except: except UnicodeDecodeError:
log.info(u'no Contents') log.exception(u'Contents unicode decode error')
except AttributeError:
log.exception(u'no Contents')
self._success = False self._success = False
lines = lyrics.split(u'\n') lines = lyrics.split(u'\n')
length = len(lines)
# we go over all lines first, to determine information, # we go over all lines first, to determine information,
# which tells us how to parse verses later # which tells us how to parse verses later
emptylines = 0
regionlines = {} regionlines = {}
separatorlines = 0 separatorlines = 0
for i in range(length): for line in lines:
lines[i] = lines[i].strip() line = line.strip()
thisline = lines[i] if len(line) == 0:
if len(thisline) == 0: continue
emptylines = emptylines + 1 elif line[1:7] == u'region':
elif thisline[0] == u'[': # this is region separator, probably [region 2]
if thisline[1:7] == u'region': region = self._extractRegion(line)
# this is region separator [region 2]
# Easislides song can have only one extra region zone,
# at least by now, but just in case the file happens
# to have [region 3] or more, we add a possiblity to
# count these separately, yeah, rather stupid, but
# count this as a programming exercise
region = self._extractRegion(thisline)
if regionlines.has_key(region): if regionlines.has_key(region):
regionlines[region] = regionlines[region] + 1 regionlines[region] = regionlines[region] + 1
else: else:
regionlines[region] = 1 regionlines[region] = 1
else: elif line[0] == u'[':
separatorlines = separatorlines + 1 separatorlines = separatorlines + 1
# if the song has separators # if the song has separators
@ -196,15 +206,6 @@ class EasiSlidesImport(SongImport):
regionsInVerses = (regions and \ regionsInVerses = (regions and \
regionlines[regionlines.keys()[0]] > 1) regionlines[regionlines.keys()[0]] > 1)
verses = {}
# list as [region, versetype, versenum, instance]
our_verse_order = []
defaultregion = u'1'
reg = defaultregion
verses[reg] = {}
# instance differentiates occurrences of same verse tag
inst = 1
MarkTypes = { MarkTypes = {
u'chorus': u'C', u'chorus': u'C',
u'verse': u'V', u'verse': u'V',
@ -213,15 +214,21 @@ class EasiSlidesImport(SongImport):
u'bridge': u'B', u'bridge': u'B',
u'prechorus': u'P'} u'prechorus': u'P'}
for i in range(length): verses = {}
thisline = lines[i] # list as [region, versetype, versenum, instance]
if i < length-1: our_verse_order = []
nextline = lines[i+1].strip() defaultregion = u'1'
else: reg = defaultregion
# there is no nextline at the last line verses[reg] = {}
nextline = False # instance differentiates occurrences of same verse tag
inst = 1
vt = u'V'
vn = u'1'
if len(thisline) == 0: for line in lines:
line = line.strip()
if len(line) == 0:
if separators: if separators:
# separators are used, so empty line means slide break # separators are used, so empty line means slide break
# inside verse # inside verse
@ -237,43 +244,32 @@ class EasiSlidesImport(SongImport):
vn = u'1' vn = u'1'
inst = 1 inst = 1
if not [reg, vt, vn, inst] in our_verse_order:
our_verse_order.append([reg, vt, vn, inst])
continue continue
continue continue
elif thisline[0:7] == u'[region': elif line[0:7] == u'[region':
reg = self._extractRegion(thisline) reg = self._extractRegion(line)
if not verses.has_key(reg): if not verses.has_key(reg):
verses[reg] = {} verses[reg] = {}
if i == 0:
# the file started with [region 2]
vt = u'V'
vn = u'1'
our_verse_order.append([reg, vt, vn, inst])
continue continue
elif thisline[0] == u'[': elif line[0] == u'[':
# this is a normal section marker # this is a normal section marker
# drop the square brackets # drop the square brackets
right_bracket = thisline.find(u']') right_bracket = line.find(u']')
marker = thisline[1:right_bracket].upper() marker = line[1:right_bracket].upper()
# have we got any digits? # have we got any digits?
# If so, versenumber is everything from the digits to the end # If so, versenumber is everything from the digits to the end
match = re.match(u'(.*)(\d+.*)', marker) match = re.match(u'(.*)(\d+.*)', marker)
if match is not None: if match is not None:
vt = match.group(1).strip() vt = match.group(1).strip()
vn = match.group(2) vn = match.group(2)
if vt == u'': if MarkTypes.has_key(vt.lower()):
vt = u'V'
elif MarkTypes.has_key(vt.lower()):
vt = MarkTypes[vt.lower()] vt = MarkTypes[vt.lower()]
else: else:
vt = u'O' vt = u'O'
else: else:
if marker == u'': if MarkTypes.has_key(marker.lower()):
vt = u'V'
elif MarkTypes.has_key(marker.lower()):
vt = MarkTypes[marker.lower()] vt = MarkTypes[marker.lower()]
else: else:
vt = u'O' vt = u'O'
@ -285,16 +281,9 @@ class EasiSlidesImport(SongImport):
inst = 1 inst = 1
if self._listHas(verses, [reg, vt, vn, inst]): if self._listHas(verses, [reg, vt, vn, inst]):
inst = len(verses[reg][vt][vn])+1 inst = len(verses[reg][vt][vn])+1
if not [reg, vt, vn, inst] in our_verse_order:
our_verse_order.append([reg, vt, vn, inst])
continue continue
if i == 0: if not [reg, vt, vn, inst] in our_verse_order:
# this is the first line, but no separator is found,
# we say it's V1
vt = u'V'
vn = u'1'
our_verse_order.append([reg, vt, vn, inst]) our_verse_order.append([reg, vt, vn, inst])
# We have versetype/number data, if it was there, now # We have versetype/number data, if it was there, now
@ -306,7 +295,7 @@ class EasiSlidesImport(SongImport):
if not verses[reg][vt][vn].has_key(inst): if not verses[reg][vt][vn].has_key(inst):
verses[reg][vt][vn][inst] = [] verses[reg][vt][vn][inst] = []
words = self.tidy_text(thisline) words = self.tidy_text(line)
verses[reg][vt][vn][inst].append(words) verses[reg][vt][vn][inst].append(words)
# done parsing # done parsing
@ -340,7 +329,9 @@ class EasiSlidesImport(SongImport):
try: try:
order = unicode(song.Sequence).strip().split(u',') order = unicode(song.Sequence).strip().split(u',')
for tag in order: for tag in order:
if tag[0].isdigit(): if len(tag) == 0:
continue
elif tag[0].isdigit():
# it's a verse if it has no prefix, but has a number # it's a verse if it has no prefix, but has a number
tag = u'V' + tag tag = u'V' + tag
elif SeqTypes.has_key(tag.lower()): elif SeqTypes.has_key(tag.lower()):
@ -353,7 +344,9 @@ class EasiSlidesImport(SongImport):
u'dropping item from presentation order', tag) u'dropping item from presentation order', tag)
else: else:
self.verse_order_list.append(tag) self.verse_order_list.append(tag)
except: except UnicodeDecodeError:
log.exception(u'Sequence unicode decode error')
except AttributeError:
pass pass
def _listHas(self, lst, subitems): def _listHas(self, lst, subitems):
@ -367,6 +360,6 @@ class EasiSlidesImport(SongImport):
return True return True
def _extractRegion(self, line): def _extractRegion(self, line):
# this was true already: thisline[0:7] == u'[region': # this was true already: line[0:7] == u'[region':
right_bracket = line.find(u']') right_bracket = line.find(u']')
return line[7:right_bracket].strip() return line[7:right_bracket].strip()

View File

@ -129,7 +129,7 @@ class OpenSongImport(SongImport):
else: else:
numfiles += 1 numfiles += 1
log.debug(u'Total number of files: %d', numfiles) log.debug(u'Total number of files: %d', numfiles)
self.import_wizard.importProgressBar.setMaximum(numfiles) self.import_wizard.progressBar.setMaximum(numfiles)
for filename in self.filenames: for filename in self.filenames:
if self.stop_import_flag: if self.stop_import_flag:
success = False success = False