SongBeamer import clean ups and tweaks

This commit is contained in:
Andreas Preikschat 2010-11-26 15:23:48 +01:00
parent d9b7dcd060
commit 66f230169d
3 changed files with 98 additions and 77 deletions

View File

@ -507,8 +507,7 @@ class SongImportForm(QtGui.QWizard, Ui_SongImportWizard):
filenames=self.getListOfFiles( filenames=self.getListOfFiles(
self.songBeamerFileListWidget) self.songBeamerFileListWidget)
) )
success = importer.do_import() if importer.do_import():
if success:
# reload songs # reload songs
self.importProgressLabel.setText( self.importProgressLabel.setText(
translate('SongsPlugin.SongImportForm', 'Finished import.')) translate('SongsPlugin.SongImportForm', 'Finished import.'))

View File

@ -57,6 +57,7 @@ class SongBeamerTypes(object):
u'Unknown': u'O' u'Unknown': u'O'
} }
class SongBeamerImport(SongImport): class SongBeamerImport(SongImport):
""" """
Import Song Beamer files(s) Import Song Beamer files(s)
@ -66,6 +67,7 @@ class SongBeamerImport(SongImport):
def __init__(self, master_manager, **kwargs): def __init__(self, master_manager, **kwargs):
""" """
Initialise the import. Initialise the import.
``master_manager`` ``master_manager``
The song manager for the running OpenLP installation. The song manager for the running OpenLP installation.
""" """
@ -88,6 +90,7 @@ class SongBeamerImport(SongImport):
# TODO: check that it is a valid SongBeamer file # TODO: check that it is a valid SongBeamer file
self.current_verse = u'' self.current_verse = u''
self.current_verse_type = u'V' self.current_verse_type = u'V'
read_verses = False
self.file_name = os.path.split(file)[1] self.file_name = os.path.split(file)[1]
self.import_wizard.incrementProgressBar( self.import_wizard.incrementProgressBar(
"Importing %s" % (self.file_name), 0) "Importing %s" % (self.file_name), 0)
@ -100,27 +103,26 @@ class SongBeamerImport(SongImport):
else: else:
return False return False
for line in self.songData: for line in self.songData:
line = line.strip() # Just make sure that the line is of the type 'Unicode'.
if line.startswith('#'): line = unicode(line).strip()
log.debug(u'find tag: %s' % line) if line.startswith(u'#') and not read_verses:
if not self.parse_tags(line): self.parse_tags(line)
return False elif line.startswith(u'---'):
elif line.startswith('---'): if self.current_verse:
log.debug(u'find ---')
if len(self.current_verse) > 0:
self.add_verse(self.current_verse, self.add_verse(self.current_verse,
self.current_verse_type) self.current_verse_type)
self.current_verse = u'' self.current_verse = u''
self.current_verse_type = u'V' self.current_verse_type = u'V'
self.read_verse = True read_verses = True
self.verse_start = True verse_start = True
elif self.read_verse: elif read_verses:
if self.verse_start: if verse_start:
self.check_verse_marks(line) verse_start = False
self.verse_start = False if not self.check_verse_marks(line):
self.current_verse = u'%s\n' % line
else: else:
self.current_verse += u'%s\n' % line self.current_verse += u'%s\n' % line
if len(self.current_verse) > 0: if self.current_verse:
self.add_verse(self.current_verse, self.current_verse_type) self.add_verse(self.current_verse, self.current_verse_type)
self.finish() self.finish()
self.import_wizard.incrementProgressBar( self.import_wizard.incrementProgressBar(
@ -128,106 +130,125 @@ class SongBeamerImport(SongImport):
return True return True
def parse_tags(self, line): def parse_tags(self, line):
tag_val = line.split('=') """
if len(tag_val[0]) == 0 or len(tag_val[1]) == 0: Parses a meta data line.
return True
if tag_val[0] == '#(c)': ``line``
The line in the file. It should consist of a tag and a value
for this tag. (unicode)
u'#Title=Nearer my God to Thee'
"""
tag_val = line.split(u'=', 1)
if len(tag_val) == 1:
return
if not tag_val[0] or not tag_val[1]:
return
if tag_val[0] == u'#(c)':
self.add_copyright(tag_val[1]) self.add_copyright(tag_val[1])
elif tag_val[0] == '#AddCopyrightInfo': elif tag_val[0] == u'#AddCopyrightInfo':
pass pass
elif tag_val[0] == '#Author': elif tag_val[0] == u'#Author':
#TODO split Authors self.parse_author(tag_val[1])
self.add_author(tag_val[1]) elif tag_val[0] == u'#BackgroundImage':
elif tag_val[0] == '#BackgroundImage':
pass pass
elif tag_val[0] == '#Bible': elif tag_val[0] == u'#Bible':
pass pass
elif tag_val[0] == '#Categories': elif tag_val[0] == u'#Categories':
self.topics = line.split(',') self.topics = line.split(',')
elif tag_val[0] == '#CCLI': elif tag_val[0] == u'#CCLI':
self.ccli_number = tag_val[1] self.ccli_number = tag_val[1]
elif tag_val[0] == '#Chords': elif tag_val[0] == u'#Chords':
pass pass
elif tag_val[0] == '#ChurchSongID': elif tag_val[0] == u'#ChurchSongID':
pass pass
elif tag_val[0] == '#ColorChords': elif tag_val[0] == u'#ColorChords':
pass pass
elif tag_val[0] == '#Comments': elif tag_val[0] == u'#Comments':
self.comments = tag_val[1] self.comments = tag_val[1]
elif tag_val[0] == '#Editor': elif tag_val[0] == u'#Editor':
pass pass
elif tag_val[0] == '#Font': elif tag_val[0] == u'#Font':
pass pass
elif tag_val[0] == '#FontLang2': elif tag_val[0] == u'#FontLang2':
pass pass
elif tag_val[0] == '#FontSize': elif tag_val[0] == u'#FontSize':
pass pass
elif tag_val[0] == '#Format': elif tag_val[0] == u'#Format':
pass pass
elif tag_val[0] == '#Format_PreLine': elif tag_val[0] == u'#Format_PreLine':
pass pass
elif tag_val[0] == '#Format_PrePage': elif tag_val[0] == u'#Format_PrePage':
pass pass
elif tag_val[0] == '#ID': elif tag_val[0] == u'#ID':
pass pass
elif tag_val[0] == '#Key': elif tag_val[0] == u'#Key':
pass pass
elif tag_val[0] == '#Keywords': elif tag_val[0] == u'#Keywords':
pass pass
elif tag_val[0] == '#LangCount': elif tag_val[0] == u'#LangCount':
pass pass
elif tag_val[0] == '#Melody': elif tag_val[0] == u'#Melody':
#TODO split Authors self.parse_author(tag_val[1])
self.add_author(tag_val[1]) elif tag_val[0] == u'#NatCopyright':
elif tag_val[0] == '#NatCopyright':
pass pass
elif tag_val[0] == '#OTitle': elif tag_val[0] == u'#OTitle':
pass pass
elif tag_val[0] == '#OutlineColor': elif tag_val[0] == u'#OutlineColor':
pass pass
elif tag_val[0] == '#OutlinedFont': elif tag_val[0] == u'#OutlinedFont':
pass pass
elif tag_val[0] == '#QuickFind': elif tag_val[0] == u'#QuickFind':
pass pass
elif tag_val[0] == '#Rights': elif tag_val[0] == u'#Rights':
song_book_pub = tag_val[1] song_book_pub = tag_val[1]
elif tag_val[0] == '#Songbook': elif tag_val[0] == u'#Songbook':
book_num = tag_val[1].split(' / ') book_num = tag_val[1].split(' / ')
self.song_book_name = book_num[0] self.song_book_name = book_num[0]
if len(book_num) == book_num[1]: if len(book_num) == book_num[1]:
self.song_number = u'' self.song_number = u''
elif tag_val[0] == '#Speed': elif tag_val[0] == u'#Speed':
pass pass
elif tag_val[0] == '#TextAlign': elif tag_val[0] == u'#TextAlign':
pass pass
elif tag_val[0] == '#Title': elif tag_val[0] == u'#Title':
self.title = u'%s' % tag_val[1] self.title = u'%s' % tag_val[1]
elif tag_val[0] == '#TitleAlign': elif tag_val[0] == u'#TitleAlign':
pass pass
elif tag_val[0] == '#TitleFontSize': elif tag_val[0] == u'#TitleFontSize':
pass pass
elif tag_val[0] == '#TitleLang2': elif tag_val[0] == u'#TitleLang2':
pass pass
elif tag_val[0] == '#TitleLang3': elif tag_val[0] == u'#TitleLang3':
pass pass
elif tag_val[0] == '#TitleLang4': elif tag_val[0] == u'#TitleLang4':
pass pass
elif tag_val[0] == '#Translation': elif tag_val[0] == u'#Translation':
pass pass
elif tag_val[0] == '#Transpose': elif tag_val[0] == u'#Transpose':
pass pass
elif tag_val[0] == '#TransposeAccidental': elif tag_val[0] == u'#TransposeAccidental':
pass pass
elif tag_val[0] == '#Version': elif tag_val[0] == u'#Version':
pass pass
else:
pass
return True
def check_verse_marks(self, line): def check_verse_marks(self, line):
marks = line.split(' ') """
Check and add the verse's MarkType. Returns ``True`` if the given mark
is correct otherwise ``False``.
``line``
The line to check for marks (unicode).
"""
marks = line.split(u' ')
if len(marks) <= 2 and marks[0] in SongBeamerTypes.MarkTypes: if len(marks) <= 2 and marks[0] in SongBeamerTypes.MarkTypes:
self.current_verse_type = SongBeamerTypes.MarkTypes[marks[0]] self.current_verse_type = SongBeamerTypes.MarkTypes[marks[0]]
if len(marks) == 2: if len(marks) == 2:
#TODO: may check, because of only digits are allowed # If we have a digit, we append it to current_verse_type.
self.current_verse_type += marks[1] try:
self.current_verse_type += u'%s' % int(marks[1])
except ValueError:
pass
return True
else:
return False

View File

@ -254,7 +254,8 @@ class SongImport(QtCore.QObject):
All fields have been set to this song. Write it away All fields have been set to this song. Write it away
""" """
if not self.authors: if not self.authors:
self.authors.append(u'Author unknown') self.authors.append(unicode(translate('SongsPlugin.SongImport',
'Author unknown')))
self.commit_song() self.commit_song()
def commit_song(self): def commit_song(self):