Changes to CCLI file importer to work with import wizard

This commit is contained in:
Derek Scotney 2010-08-28 15:17:27 +02:00
parent 80ded239a8
commit 676cc9305c

View File

@ -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
@ -78,46 +78,54 @@ 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)
lines = [] log.debug('Starting CCLI File Import')
filename = unicode(filename) song_total = len(self.filenames)
if os.path.isfile(filename): self.import_wizard.importProgressBar.setMaximum(song_total)
detect_file = open(filename, u'r') song_count = 1
details = chardet.detect(detect_file.read(2048)) for filename in self.filenames:
detect_file.close() self.import_wizard.incrementProgressBar(
infile = codecs.open(filename, u'r', details['encoding']) u'Importing song %s of %s' % (song_count, song_total))
lines = infile.readlines() 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()
ext = os.path.splitext(filename)[1] ext = os.path.splitext(filename)[1]
if ext.lower() == ".usr": if ext.lower() == ".usr":
log.info('SongSelect .usr format file found %s', filename) log.info('SongSelect .usr format file found %s: ' , filename)
self.do_import_usr_file(lines) self.do_import_usr_file(lines)
if commit: elif ext.lower() == ".txt":
self.finish() log.info('SongSelect .txt format file found %s: ', filename)
elif ext.lower() == ".txt": self.do_import_txt_file(lines)
log.info('SongSelect .txt format file found %s', filename) else:
self.do_import_txt_file(lines) log.info(u'Extension %s is not valid', filename)
if commit: pass
self.finish() song_count += 1
else: if self.stop_import_flag:
log.info(u'Extension %s is not valid', filename) return False
pass
return True
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()