New style string formatting, restructure indent=0 cases

This commit is contained in:
John Lines 2019-07-12 15:36:14 +01:00
parent 515abee3ae
commit 36b5528d43

View File

@ -117,7 +117,7 @@ class SingingTheFaithImport(SongImport):
try: try:
for line in file: for line in file:
line_number += 1 line_number += 1
if hints_available and (str(line_number) in self.hint_line): if hints_available and str(line_number) in self.hint_line:
hint = self.hint_line[str(line_number)] hint = self.hint_line[str(line_number)]
if hint == 'Comment': if hint == 'Comment':
line.strip() line.strip()
@ -137,24 +137,26 @@ class SingingTheFaithImport(SongImport):
next(file) next(file)
continue continue
elif hint.startswith('VariantVerse'): elif hint.startswith('VariantVerse'):
(vv, hintverse, replace) = hint.split(' ', 2) vv, hintverse, replace = hint.split(' ', 2)
this_verse = self.verses[int(hintverse) - 1] this_verse = self.verses[int(hintverse) - 1]
this_verse_str = this_verse[1] this_verse_str = this_verse[1]
new_verse = this_verse_str new_verse = this_verse_str
# There might be multiple replace pairs separated by | # There might be multiple replace pairs separated by |
replaces = replace.split('|') replaces = replace.split('|')
for rep in replaces: for rep in replaces:
(source_str, dest_str) = rep.split('/') source_str, dest_str = rep.split('/')
new_verse = new_verse.replace(source_str, dest_str) new_verse = new_verse.replace(source_str, dest_str)
self.add_verse(new_verse, 'v') self.add_verse(new_verse, 'v')
self.verse_order_list.append('v' + str(current_verse_number)) self.verse_order_list.append('v{}'.format(str(current_verse_number)))
current_verse_number += 1 current_verse_number += 1
line_number += 1 line_number += 1
next(file) next(file)
continue continue
else: else:
self.log_error(translate('SongsPlugin.SingingTheFaithImport', 'File %s' % file.name), self.log_error(translate('SongsPlugin.SingingTheFaithImport',
translate('SongsPlugin.SingingTheFaithImport', 'Unknown hint %s' % hint)) 'File {file})'.format(file=file.name)),
translate('SongsPlugin.SingingTheFaithImport',
'Unknown hint {hint}').format(hint=hint))
return return
# STF exported lines have a leading verse number at the start of each verse. # STF exported lines have a leading verse number at the start of each verse.
# remove them - note that we want to track the indent as that shows a chorus # remove them - note that we want to track the indent as that shows a chorus
@ -185,26 +187,22 @@ class SingingTheFaithImport(SongImport):
if song_number_match: if song_number_match:
song_number = song_number_match.group() song_number = song_number_match.group()
continue continue
# If the indent is 0 and it contains '(c)' then it is a Copyright line elif indent == 0:
elif (indent == 0) and ('(c)' in line): # If the indent is 0 and it contains '(c)' then it is a Copyright line
copyright = line if '(c)' in line:
continue copyright = line
elif (indent == 0) and (line.startswith('Liturgical ')): continue
self.add_comment(line) elif (line.startswith('Liturgical ') or line.startswith('From The ') or
continue line.startswith('From Common ')):
elif (indent == 0) and (line.startswith('From The ')): self.add_comment(line)
self.add_comment(line) continue
continue # If indent is 0 it may be the author, unless it was one of the cases covered above
elif (indent == 0) and (line.startswith('From Common ')): elif len(line) > 0:
self.add_comment(line) # May have more than one author, separated by ' and '
continue authors = line.split(' and ')
# If indent is 0 it may be the author, unless it was one of the cases covered above for a in authors:
elif (indent == 0) and len(line) > 0: self.parse_author(a)
# May have more than one author, separated by ' and ' continue
authors = line.split(' and ')
for a in authors:
self.parse_author(a)
continue
if line == '': if line == '':
if current_verse != '': if current_verse != '':
self.add_verse(current_verse, current_verse_type) self.add_verse(current_verse, current_verse_type)
@ -237,13 +235,13 @@ class SingingTheFaithImport(SongImport):
current_verse += '\n' + line current_verse += '\n' + line
old_indent = indent old_indent = indent
except Exception as e: except Exception as e:
self.log_error(translate('SongsPlugin.SingingTheFaithImport', 'File %s' % file.name), self.log_error(translate('SongsPlugin.SingingTheFaithImport', 'File {file}').format(file=file.name),
translate('SongsPlugin.SingingTheFaithImport', 'Error: %s') % e) translate('SongsPlugin.SingingTheFaithImport', 'Error: {error}').format(error=e))
return return
if self.hint_song_title: if self.hint_song_title:
song_title = self.hint_song_title song_title = self.hint_song_title
self.title = check_flag + 'STF' + song_number.zfill(3) + ' - ' + song_title self.title = '{}STF{} - {title}'.format(check_flag, song_number.zfill(3), title=song_title)
self.song_book_name = 'Singing The Faith' self.song_book_name = 'Singing The Faith'
self.song_number = song_number self.song_number = song_number
self.ccli_number = ccli self.ccli_number = ccli
@ -254,7 +252,7 @@ class SingingTheFaithImport(SongImport):
if has_chorus and not self.hint_verse_order and not self.checks_needed: if has_chorus and not self.hint_verse_order and not self.checks_needed:
auto_verse_order_ok = False auto_verse_order_ok = False
# Popular case V1 C2 V2 ... # Popular case V1 C2 V2 ...
if len(self.verse_order_list) >= 1: # protect against odd cases if self.verse_order_list: # protect against odd cases
if (self.verse_order_list[0] == 'v1') and (self.verse_order_list[1] == 'c2'): if (self.verse_order_list[0] == 'v1') and (self.verse_order_list[1] == 'c2'):
new_verse_order_list = ['v1', 'c1'] new_verse_order_list = ['v1', 'c1']
i = 2 i = 2
@ -291,7 +289,7 @@ class SingingTheFaithImport(SongImport):
check_flag = '' check_flag = ''
elif not hints_available and has_chorus and auto_verse_order_ok: elif not hints_available and has_chorus and auto_verse_order_ok:
check_flag = '' check_flag = ''
self.title = check_flag + 'STF' + song_number.zfill(3) + ' - ' + song_title self.title = '{}STF{} - {title}'.format(check_flag, song_number.zfill(3), title=song_title)
if not self.finish(): if not self.finish():
self.log_error(file.name) self.log_error(file.name)
@ -312,7 +310,7 @@ class SingingTheFaithImport(SongImport):
self.hint_file_version = val self.hint_file_version = val
continue continue
if (tag == 'Hymn') and (val == song_number): if (tag == 'Hymn') and (val == song_number):
self.add_comment('Using hints version ' + str(self.hint_file_version)) self.add_comment('Using hints version {}'.format(str(self.hint_file_version)))
hintfound = True hintfound = True
# Assume, unless the hints has ManualCheck that if hinted all will be OK # Assume, unless the hints has ManualCheck that if hinted all will be OK
self.checks_needed = False self.checks_needed = False
@ -342,11 +340,11 @@ class SingingTheFaithImport(SongImport):
self.hint_ignore_indent = True self.hint_ignore_indent = True
elif tag == 'VariantVerse': elif tag == 'VariantVerse':
vvline = val.split(' ', 1) vvline = val.split(' ', 1)
self.hint_line[vvline[0].strip()] = 'VariantVerse ' + vvline[1].strip() self.hint_line[vvline[0].strip()] = 'VariantVerse {}'.format(vvline[1].strip())
elif tag == 'SongTitle': elif tag == 'SongTitle':
self.hint_song_title = val self.hint_song_title = val
elif tag == 'AddComment': elif tag == 'AddComment':
self.hint_comments += '\n' + val self.hint_comments += '\n' + val
else: else:
self.log_error(file.name, 'Unknown tag ' + tag + ' value ' + val) self.log_error(file.name, 'Unknown tag {} value {}'.format(tag, val))
return hintfound return hintfound