Improved =s

This commit is contained in:
Martin Thompson 2010-06-28 20:55:04 +01:00
parent baec9d5c2f
commit 75c6ba2284
2 changed files with 60 additions and 37 deletions

View File

@ -86,11 +86,11 @@ class OpenSongImport:
Initialise the class. Requires a songmanager class which is passed Initialise the class. Requires a songmanager class which is passed
to SongImport for writing song to disk to SongImport for writing song to disk
""" """
self.songmanager=songmanager self.songmanager = songmanager
self.song = None self.song = None
def do_import(self, filename): def do_import(self, filename):
file=open(filename) file = open(filename)
self.do_import_file(file) self.do_import_file(file)
def do_import_file(self, file): def do_import_file(self, file):
@ -98,10 +98,10 @@ class OpenSongImport:
Process the OpenSong file Process the OpenSong file
""" """
self.song = SongImport(self.songmanager) self.song = SongImport(self.songmanager)
tree=objectify.parse(file) tree = objectify.parse(file)
root=tree.getroot() root = tree.getroot()
fields=dir(root) fields = dir(root)
decode={u'copyright':self.song.add_copyright, decode = {u'copyright':self.song.add_copyright,
u'author':self.song.parse_author, u'author':self.song.parse_author,
u'title':self.song.set_title, u'title':self.song.set_title,
u'aka':self.song.set_alternate_title, u'aka':self.song.set_alternate_title,
@ -111,20 +111,20 @@ class OpenSongImport:
fn(unicode(root.__getattr__(attr))) fn(unicode(root.__getattr__(attr)))
# data storage while importing # data storage while importing
verses={} verses = {}
lyrics=unicode(root.lyrics) lyrics = unicode(root.lyrics)
# keep track of a "default" verse order, in case none is specified # keep track of a "default" verse order, in case none is specified
our_verse_order=[] our_verse_order = []
verses_seen={} verses_seen = {}
# in the absence of any other indication, verses are the default, erm, versetype! # in the absence of any other indication, verses are the default, erm, versetype!
versetype=u'V' versetype = u'V'
for l in lyrics.split(u'\n'): for l in lyrics.split(u'\n'):
# remove comments # remove comments
semicolon = l.find(u';') semicolon = l.find(u';')
if semicolon >= 0: if semicolon >= 0:
l=l[:semicolon] l = l[:semicolon]
l=l.strip() l = l.strip()
if l==u'': if len(l) == 0:
continue continue
# skip inline guitar chords # skip inline guitar chords
if l[0] == u'.': if l[0] == u'.':
@ -132,14 +132,14 @@ class OpenSongImport:
# verse/chorus/etc. marker # verse/chorus/etc. marker
if l[0] == u'[': if l[0] == u'[':
versetype=l[1].upper() versetype = l[1].upper()
if versetype.isdigit(): if versetype.isdigit():
versenum=versetype versenum = versetype
versetype=u'V' versetype = u'V'
elif l[2] != u']': elif l[2] != u']':
# there's a number to go with it - extract that as well # there's a number to go with it - extract that as well
right_bracket=l.find(u']') right_bracket = l.find(u']')
versenum=l[2:right_bracket] versenum = l[2:right_bracket]
else: else:
versenum = u'' versenum = u''
continue continue
@ -147,18 +147,18 @@ class OpenSongImport:
# number at start of line.. it's verse number # number at start of line.. it's verse number
if l[0].isdigit(): if l[0].isdigit():
versenum=l[0] versenum = l[0]
words=l[1:].strip() words = l[1:].strip()
if words is None and \ if words is None and \
versenum is not None and \ versenum is not None and \
versetype is not None: versetype is not None:
words=l words=l
if versenum is not None: if versenum is not None:
versetag=u'%s%s'%(versetype,versenum) versetag = u'%s%s'%(versetype,versenum)
if not verses.has_key(versetype): if not verses.has_key(versetype):
verses[versetype]={} verses[versetype] = {}
if not verses[versetype].has_key(versenum): if not verses[versetype].has_key(versenum):
verses[versetype][versenum]=[] # storage for lines in this verse verses[versetype][versenum] = [] # storage for lines in this verse
if not verses_seen.has_key(versetag): if not verses_seen.has_key(versetag):
verses_seen[versetag] = 1 verses_seen[versetag] = 1
our_verse_order.append(versetag) our_verse_order.append(versetag)
@ -168,28 +168,28 @@ class OpenSongImport:
words=words.replace('_', '') words=words.replace('_', '')
verses[versetype][versenum].append(words) verses[versetype][versenum].append(words)
# done parsing # done parsing
versetypes=verses.keys() versetypes = verses.keys()
versetypes.sort() versetypes.sort()
versetags={} versetags = {}
for v in versetypes: for v in versetypes:
versenums=verses[v].keys() versenums = verses[v].keys()
versenums.sort() versenums.sort()
for n in versenums: for n in versenums:
versetag= u'%s%s' %(v,n) versetag = u'%s%s' %(v,n)
lines=u'\n'.join(verses[v][n]) lines = u'\n'.join(verses[v][n])
self.song.verses.append([versetag, lines]) self.song.verses.append([versetag, lines])
versetags[versetag]=1 # keep track of what we have for error checking later versetags[versetag] = 1 # keep track of what we have for error checking later
# now figure out the presentation order # now figure out the presentation order
if u'presentation' in fields and root.presentation != u'': if u'presentation' in fields and root.presentation != u'':
order=unicode(root.presentation) order = unicode(root.presentation)
order=order.split() order = order.split()
else: else:
assert len(our_verse_order)>0 assert len(our_verse_order)>0
order=our_verse_order order = our_verse_order
for tag in order: for tag in order:
if not versetags.has_key(tag): if not versetags.has_key(tag):
print u'Got order', tag, u'but not in versetags, skipping' print u'Got order', tag, u'but not in versetags, skipping'
raise OpenSongImportError raise OpenSongImportError # xxx keep error, or just warn?
else: else:
self.song.verse_order_list.append(tag) self.song.verse_order_list.append(tag)
def finish(self): def finish(self):

View File

@ -1,10 +1,33 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
# Thompson, Jon Tibble, Carsten Tinggaard #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
from openlp.plugins.songs.lib.opensongimport import OpenSongImport from openlp.plugins.songs.lib.opensongimport import OpenSongImport
from openlp.plugins.songs.lib.manager import SongManager from openlp.plugins.songs.lib.manager import SongManager
def test(): def test():
manager=SongManager() manager = SongManager()
o=OpenSongImport(manager) o = OpenSongImport(manager)
o.do_import(u'test.opensong') o.do_import(u'test.opensong')
# o.finish() # o.finish()
o.song.print_song() o.song.print_song()
@ -21,7 +44,7 @@ def test():
assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.song.verses assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.song.verses
assert o.song.verse_order_list == [u'V1', u'C', u'V2', u'C2', u'V3', u'B1', u'V1'] assert o.song.verse_order_list == [u'V1', u'C', u'V2', u'C2', u'V3', u'B1', u'V1']
o=OpenSongImport(manager) o = OpenSongImport(manager)
o.do_import(u'test2.opensong') o.do_import(u'test2.opensong')
# o.finish() # o.finish()
o.song.print_song() o.song.print_song()