forked from openlp/openlp
Changes to CCLI file importer to work with import wizard
This commit is contained in:
parent
80ded239a8
commit
676cc9305c
@ -33,10 +33,10 @@ from songimport import SongImport
|
|||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class SongSelectFileImportError(Exception):
|
class CCLIFileImportError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class SongSelectFileImport(object):
|
class CCLIFileImport(SongImport):
|
||||||
"""
|
"""
|
||||||
Import songs from CCLI SongSelect files in both .txt and .usr formats
|
Import songs from CCLI SongSelect files in both .txt and .usr formats
|
||||||
http://www.ccli.com
|
http://www.ccli.com
|
||||||
@ -77,47 +77,55 @@ class SongSelectFileImport(object):
|
|||||||
==========
|
==========
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, songmanager):
|
def __init__(self, master_manager, **kwargs):
|
||||||
"""
|
"""
|
||||||
Initialise the class. Requires a songmanager class which
|
Initialise the class. Requires a songmanager class which
|
||||||
is passed to SongImport for writing song to disk
|
is passed to SongImport for writing song to disk
|
||||||
"""
|
"""
|
||||||
self.songmanager = songmanager
|
SongImport.__init__(self, master_manager)
|
||||||
self.song = None
|
self.master_manager = master_manager
|
||||||
|
if u'filenames' in kwargs:
|
||||||
|
self.filenames = kwargs[u'filenames']
|
||||||
|
log.debug(self.filenames)
|
||||||
|
|
||||||
def do_import(self, filename, commit=True):
|
def do_import(self):
|
||||||
"""
|
"""
|
||||||
Import either a .usr or a .txt SongSelect file
|
Import either a .usr or a .txt SongSelect file
|
||||||
If the commit parameter is set False,
|
|
||||||
the import will not be committed to the database
|
|
||||||
(useful for test scripts)
|
|
||||||
"""
|
"""
|
||||||
self.song_import = SongImport(self.songmanager)
|
|
||||||
|
log.debug('Starting CCLI File Import')
|
||||||
|
song_total = len(self.filenames)
|
||||||
|
self.import_wizard.importProgressBar.setMaximum(song_total)
|
||||||
|
song_count = 1
|
||||||
|
for filename in self.filenames:
|
||||||
|
self.import_wizard.incrementProgressBar(
|
||||||
|
u'Importing song %s of %s' % (song_count, song_total))
|
||||||
|
filename = unicode(filename)
|
||||||
|
log.debug('Importing CCLI File: %s', filename)
|
||||||
|
lines = []
|
||||||
|
if os.path.isfile(filename):
|
||||||
|
detect_file = open(filename, u'r')
|
||||||
|
details = chardet.detect(detect_file.read(2048))
|
||||||
|
detect_file.close()
|
||||||
|
infile = codecs.open(filename, u'r', details['encoding'])
|
||||||
|
lines = infile.readlines()
|
||||||
|
|
||||||
lines = []
|
ext = os.path.splitext(filename)[1]
|
||||||
filename = unicode(filename)
|
if ext.lower() == ".usr":
|
||||||
if os.path.isfile(filename):
|
log.info('SongSelect .usr format file found %s: ' , filename)
|
||||||
detect_file = open(filename, u'r')
|
self.do_import_usr_file(lines)
|
||||||
details = chardet.detect(detect_file.read(2048))
|
elif ext.lower() == ".txt":
|
||||||
detect_file.close()
|
log.info('SongSelect .txt format file found %s: ', filename)
|
||||||
infile = codecs.open(filename, u'r', details['encoding'])
|
self.do_import_txt_file(lines)
|
||||||
lines = infile.readlines()
|
else:
|
||||||
|
log.info(u'Extension %s is not valid', filename)
|
||||||
ext = os.path.splitext(filename)[1]
|
pass
|
||||||
if ext.lower() == ".usr":
|
song_count += 1
|
||||||
log.info('SongSelect .usr format file found %s', filename)
|
if self.stop_import_flag:
|
||||||
self.do_import_usr_file(lines)
|
return False
|
||||||
if commit:
|
|
||||||
self.finish()
|
return True
|
||||||
elif ext.lower() == ".txt":
|
|
||||||
log.info('SongSelect .txt format file found %s', filename)
|
|
||||||
self.do_import_txt_file(lines)
|
|
||||||
if commit:
|
|
||||||
self.finish()
|
|
||||||
else:
|
|
||||||
log.info(u'Extension %s is not valid', filename)
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def do_import_usr_file(self, textList):
|
def do_import_usr_file(self, textList):
|
||||||
@ -125,9 +133,13 @@ class SongSelectFileImport(object):
|
|||||||
Process the USR file - pass in a list of lines
|
Process the USR file - pass in a list of lines
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
log.debug('USR file text: %s', textList)
|
||||||
|
|
||||||
n = 0 # line number
|
n = 0 # line number
|
||||||
lyrics = []
|
lyrics = []
|
||||||
|
|
||||||
|
new_song = SongImport(self.master_manager)
|
||||||
|
|
||||||
for line in textList:
|
for line in textList:
|
||||||
n += 1
|
n += 1
|
||||||
if line.startswith(u'Title='):
|
if line.startswith(u'Title='):
|
||||||
@ -161,7 +173,8 @@ class SongSelectFileImport(object):
|
|||||||
vtype = u'O'
|
vtype = u'O'
|
||||||
vcontent = unicode(wordslst[i])
|
vcontent = unicode(wordslst[i])
|
||||||
vcontent = vcontent.replace("/n", "\n")
|
vcontent = vcontent.replace("/n", "\n")
|
||||||
self.song_import.add_verse(vcontent, vtype);
|
if (len(vcontent) > 0):
|
||||||
|
new_song.add_verse(vcontent, vtype);
|
||||||
|
|
||||||
#Handle multiple authors
|
#Handle multiple authors
|
||||||
lst = sauthor.split(u'/')
|
lst = sauthor.split(u'/')
|
||||||
@ -169,11 +182,12 @@ class SongSelectFileImport(object):
|
|||||||
lst = sauthor.split(u'|')
|
lst = sauthor.split(u'|')
|
||||||
for author in lst:
|
for author in lst:
|
||||||
seperated = author.split(u',')
|
seperated = author.split(u',')
|
||||||
self.song_import.add_author(seperated[1].strip() + " " + seperated[0].strip())
|
new_song.add_author(seperated[1].strip() + " " + seperated[0].strip())
|
||||||
|
|
||||||
self.song_import.title = sname
|
new_song.title = sname
|
||||||
self.song_import.copyright = scopyright
|
new_song.copyright = scopyright
|
||||||
self.song_import.ccli_number = sccli
|
new_song.ccli_number = sccli
|
||||||
|
new_song.finish()
|
||||||
|
|
||||||
|
|
||||||
def do_import_txt_file(self, textList):
|
def do_import_txt_file(self, textList):
|
||||||
@ -181,6 +195,9 @@ class SongSelectFileImport(object):
|
|||||||
Process the TXT file - pass in a list of lines
|
Process the TXT file - pass in a list of lines
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
log.debug('TXT file text: %s', textList)
|
||||||
|
|
||||||
|
new_song = SongImport(self.master_manager)
|
||||||
n = 0
|
n = 0
|
||||||
vcontent = u''
|
vcontent = u''
|
||||||
scomments = u''
|
scomments = u''
|
||||||
@ -193,9 +210,10 @@ class SongSelectFileImport(object):
|
|||||||
if (n==0):
|
if (n==0):
|
||||||
continue
|
continue
|
||||||
elif (verse_start == True):
|
elif (verse_start == True):
|
||||||
self.song_import.add_verse(vcontent, vtype)
|
if (len(vcontent) > 0):
|
||||||
vcontent = ''
|
new_song.add_verse(vcontent, vtype)
|
||||||
verse_start = False
|
vcontent = ''
|
||||||
|
verse_start = False
|
||||||
else:
|
else:
|
||||||
if (n==0): #n=0, song title
|
if (n==0): #n=0, song title
|
||||||
sname = ln
|
sname = ln
|
||||||
@ -239,13 +257,9 @@ class SongSelectFileImport(object):
|
|||||||
alist = sauthor.split(u'/')
|
alist = sauthor.split(u'/')
|
||||||
if len(alist) < 2:
|
if len(alist) < 2:
|
||||||
alist = sauthor.split(u'|')
|
alist = sauthor.split(u'|')
|
||||||
self.song_import.authors = alist
|
new_song.authors = alist
|
||||||
self.song_import.title = sname
|
new_song.title = sname
|
||||||
self.song_import.copyright = scopyright
|
new_song.copyright = scopyright
|
||||||
self.song_import.ccli_number = sccli
|
new_song.ccli_number = sccli
|
||||||
self.song_import.comments = scomments
|
new_song.comments = scomments
|
||||||
|
new_song.finish()
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
""" Separate function, allows test suite to not pollute database"""
|
|
||||||
self.song_import.finish()
|
|
Loading…
Reference in New Issue
Block a user