From e28b1f344123f4151efbd3dbebf42523a9a4135f Mon Sep 17 00:00:00 2001 From: Carsten Tinggaard Date: Tue, 4 Nov 2008 22:46:39 +0000 Subject: [PATCH] Implemented text file import (CCLI format) incl test bzr-revno: 79 --- openlp/song/song.py | 58 ++++++++++++++++++-- openlp/song/test/data_text/CCLI example.txt | 36 ++++++++++++ openlp/song/test/data_text/PÃ¥EnFjern.txt | 61 +++++++++++++++++++++ openlp/song/test/test_song_text.py | 34 ++++++++++-- 4 files changed, 180 insertions(+), 9 deletions(-) create mode 100644 openlp/song/test/data_text/CCLI example.txt create mode 100644 openlp/song/test/data_text/PÃ¥EnFjern.txt diff --git a/openlp/song/song.py b/openlp/song/song.py index 70e0d9887..ec644dae6 100644 --- a/openlp/song/song.py +++ b/openlp/song/song.py @@ -275,19 +275,67 @@ class Song(XmlRootClass) : textList (list of strings) -- the song """ self._reset() - # TODO: Implement CCLI text parse + # extract the following fields + # - name + # - author + # - CCLI no + sName = "" + sAuthor = "" + sCopyright = "" + sCcli = "" + lastpart = 0 + n = 0 + metMisc = False + lyrics = [] + for l in textList : + n += 1 + if lastpart > 0 : + lastpart += 1 + if lastpart == 2 : + sCopyright = l[1:].strip() + if lastpart == 3 : + sAuthor = l + elif l.startswith('CCLI Song') : + sCcli = l[13:].strip() + lastpart = 1 + else : + if metMisc : + metMisc = False + if l.upper().startswith("(BRIDGE)") : + lyrics.append("# Bridge") + # otherwise unknown misc keyword + elif l.startswith("Misc") : + metMisc = True + elif l.startswith("Verse") or l.startswith("Chorus"): + lyrics.append("# %s"%l) + else : + # should we remove multiple blank lines? + if n == 1 : + sName = l + else : + lyrics.append(l) + # split on known separators + lst = sAuthor.split('/') + if len(lst) < 2: + lst = sAuthor.split('|') + authorList = ", ".join(lst) + self.SetTitle(sName) + self.SetAuthorList(authorList) + self.SetCopyright(sCopyright) + self.SetSongCcliNo(sCcli) + self.SetLyrics(lyrics) def FromTextFile(self, textFileName): """Create song from a list of texts read from given file textFileName -- path to text file """ - textList = [] + lines = [] f = open(textFileName, 'r') - for line in f : - textList.append(line) + for orgline in f: + lines.append(orgline.rstrip()) f.close() - self.FromText(textList) + self.FromTextList(lines) def _assureString(self, s): """Force a string is returned""" diff --git a/openlp/song/test/data_text/CCLI example.txt b/openlp/song/test/data_text/CCLI example.txt new file mode 100644 index 000000000..9c8329294 --- /dev/null +++ b/openlp/song/test/data_text/CCLI example.txt @@ -0,0 +1,36 @@ +Song Title Here + + +Chorus 1 +Lyrics +Lyrics +Lyrics +Lyrics + + +Verse 1 +Lyrics +Lyrics +Lyrics + + +Verse 2 +Lyrics +Lyrics +Lyrics + + +Misc 1 +(BRIDGE) +Lyrics +Lyrics +Lyrics +Lyrics + + + +CCLI Song No. 1234567 +© 1996 Publisher Info +Author/artist name +For use solely in accordance with the SongSelect Advanced Terms of Agreement. All rights Reserved. +CCLI License No. 1234567 \ No newline at end of file diff --git a/openlp/song/test/data_text/PÃ¥EnFjern.txt b/openlp/song/test/data_text/PÃ¥EnFjern.txt new file mode 100644 index 000000000..490dedac4 --- /dev/null +++ b/openlp/song/test/data_text/PÃ¥EnFjern.txt @@ -0,0 +1,61 @@ +På en fjern ensom høj + +Verse 1 +På en fjern ensom høj, +Jesu kors dyrest stod, +symbolet på smerte og skam. +O, jeg elsker det kors, +hvor Guds søn gjorde bod, +da forbandelsen blev lagt på ham. + +Chorus 1 +Jeg vil elske det urgamle kors, +i det kraft er der sejer og sang. +Lad mig favne det hellige kors, +det med kronen ombyttes engang. + +Verse 2 +O, det urgamle kors, +med sin hvile og fred, +tilhyllet i verdens foragt. +Se, det hellige lam, +som på Golgatha stred, +og til jorden Guds nåde har bragt. + +Chorus 2 +Jeg vil elske det urgamle kors, +i det kraft er der sejer og sang. +Lad mig favne det hellige kors, +det med kronen ombyttes engang. + +Verse 3 +I det urgamle kors, +i hans blod farvet rødt, +en underfuld skønhed jeg ser. +Ja, det var på det kors, +at han selv blev forstødt, +nu skal aldrig for dommen jeg mer. + +Chorus 3 +Jeg vil elske det urgamle kors, +i det kraft er der sejer og sang. +Lad mig favne det hellige kors, +det med kronen ombyttes engang. + +Verse 4 +For det urgamle kors, +står mit hjerte i brand, +min plads jeg nu har ved dets fod. +Til han kalder en dag, +mig til himmelens land, +og til hvilen hos Faderen god. + +Chorus 4 +Jeg vil elske det urgamle kors, +i det kraft er der sejer og sang. +Lad mig favne det hellige kors, +det med kronen ombyttes engang. + +CCLI Song No. +© +Georg Bennard diff --git a/openlp/song/test/test_song_text.py b/openlp/song/test/test_song_text.py index 765b0cf6a..8c5240641 100644 --- a/openlp/song/test/test_song_text.py +++ b/openlp/song/test/test_song_text.py @@ -1,3 +1,4 @@ +# -*- coding:iso-8859-1 -*- """ OpenLP - Open Source Lyrics Projection Copyright (c) 2008 Raoul Snyman @@ -19,13 +20,38 @@ Place, Suite 330, Boston, MA 02111-1307 USA import os import sys sys.path.append(os.path.abspath("./../../..")) +from openlp.song import * -from openlp.song import Song +__ThisDir__ = os.path.abspath(".") class Test_Text(object): """Test cases for converting from text format to Song""" - def test_Simple(self): - """Text: Simply return True""" - assert(True) + def test_file1(self): + """OpenSong: parse CCLI example""" + global __ThisDir__ + s = Song() + s.FromTextFile("%s/data_text/CCLI example.txt"%(__ThisDir__)) + assert(s.GetTitle() == "Song Title Here") + assert(s.GetAuthorList(True) == "Author, artist name") + assert(s.GetCopyright() == "1996 Publisher Info") + assert(s.GetSongCcliNo() == "1234567") + assert(s.GetNumberOfSlides() == 4) + + def test_file2(self): + """OpenSong: parse PåEnFjern (danish)""" + global __ThisDir__ + s = Song() + s.FromTextFile("%s/data_text/PåEnFjern.txt"%(__ThisDir__)) + assert(s.GetTitle() == "På en fjern ensom høj") + assert(s.GetAuthorList(True) == "Georg Bennard") + assert(s.GetCopyright() == "") + assert(s.GetSongCcliNo() == "") + assert(s.GetNumberOfSlides() == 8) + +if '__main__' == __name__: + # for local debugging + r = Test_Text() + r.test_file1() + r.test_file2()