openlp/openlp/plugins/songs/lib/opensongimport.py

198 lines
7.5 KiB
Python
Raw Normal View History

2010-06-15 20:03:47 +00:00
# -*- 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 #
###############################################################################
import os
import re
2010-06-15 21:25:50 +00:00
from songimport import SongImport
from lxml.etree import Element
from lxml import objectify
2010-06-15 20:03:47 +00:00
class OpenSongImportError(Exception):
pass
2010-06-15 21:25:50 +00:00
class OpenSongImport:
2010-06-15 20:03:47 +00:00
"""
Import songs exported from OpenSong - the format is described loosly here:
http://www.opensong.org/d/manual/song_file_format_specification
However, it doesn't describe the <lyrics> section, so here's an attempt:
Verses can be expressed in one of 2 ways:
<lyrics>
[v1]List of words
Another Line
[v2]Some words for the 2nd verse
etc...
</lyrics>
The 'v' can be left out - it is implied
2010-06-15 20:03:47 +00:00
or:
<lyrics>
[V]
2010-06-15 20:03:47 +00:00
1List of words
2Some words for the 2nd Verse
1Another Line
2etc...
</lyrics>
Either or both forms can be used in one song. The Number does not necessarily appear at the start of the line
2010-06-15 20:03:47 +00:00
The [v1] labels can have either upper or lower case Vs
2010-06-15 20:03:47 +00:00
Other labels can be used also:
C - Chorus
B - Bridge
Guitar chords can be provided 'above' the lyrics (the line is preceeded by a'.') and _s can be used to signify long-drawn-out words:
. A7 Bm
1 Some____ Words
Chords and _s are removed by this importer.
The verses etc. are imported and tagged appropriately.
The <presentation> tag is used to populate the OpenLP verse
display order field. The Author and Copyright tags are also
imported to the appropriate places.
"""
def __init__(self, songmanager):
"""
Initialise the class. Requires a songmanager class which is passed
to SongImport for writing song to disk
"""
2010-06-28 19:55:04 +00:00
self.songmanager = songmanager
2010-06-15 20:03:47 +00:00
self.song = None
2010-06-15 21:25:50 +00:00
def do_import(self, filename):
2010-06-28 19:55:04 +00:00
file = open(filename)
self.do_import_file(file)
def do_import_file(self, file):
2010-06-15 20:03:47 +00:00
"""
Process the OpenSong file
"""
2010-06-15 21:25:50 +00:00
self.song = SongImport(self.songmanager)
2010-06-28 19:55:04 +00:00
tree = objectify.parse(file)
root = tree.getroot()
fields = dir(root)
decode = {u'copyright':self.song.add_copyright,
u'author':self.song.parse_author,
u'title':self.song.set_title,
u'aka':self.song.set_alternate_title,
u'hymn_number':self.song.set_song_number}
for (attr, fn) in decode.items():
if attr in fields:
fn(unicode(root.__getattr__(attr)))
2010-06-15 20:03:47 +00:00
2010-06-15 21:25:50 +00:00
# data storage while importing
2010-06-28 19:55:04 +00:00
verses = {}
lyrics = unicode(root.lyrics)
# keep track of a "default" verse order, in case none is specified
2010-06-28 19:55:04 +00:00
our_verse_order = []
verses_seen = {}
# in the absence of any other indication, verses are the default, erm, versetype!
2010-06-28 19:55:04 +00:00
versetype = u'V'
for l in lyrics.split(u'\n'):
2010-06-15 21:25:50 +00:00
# remove comments
semicolon = l.find(u';')
2010-06-15 21:25:50 +00:00
if semicolon >= 0:
2010-06-28 19:55:04 +00:00
l = l[:semicolon]
l = l.strip()
if len(l) == 0:
2010-06-15 21:25:50 +00:00
continue
# skip inline guitar chords
if l[0] == u'.':
continue
2010-06-15 21:25:50 +00:00
# verse/chorus/etc. marker
if l[0] == u'[':
2010-06-28 19:55:04 +00:00
versetype = l[1].upper()
if versetype.isdigit():
2010-06-28 19:55:04 +00:00
versenum = versetype
versetype = u'V'
elif l[2] != u']':
2010-06-15 21:25:50 +00:00
# there's a number to go with it - extract that as well
2010-06-28 19:55:04 +00:00
right_bracket = l.find(u']')
versenum = l[2:right_bracket]
2010-06-15 21:25:50 +00:00
else:
versenum = u''
2010-06-15 21:25:50 +00:00
continue
words=None
# number at start of line.. it's verse number
if l[0].isdigit():
2010-06-28 19:55:04 +00:00
versenum = l[0]
words = l[1:].strip()
2010-06-15 21:25:50 +00:00
if words is None and \
versenum is not None and \
versetype is not None:
words=l
if versenum is not None:
2010-06-28 19:55:04 +00:00
versetag = u'%s%s'%(versetype,versenum)
if not verses.has_key(versetype):
2010-06-28 19:55:04 +00:00
verses[versetype] = {}
if not verses[versetype].has_key(versenum):
2010-06-28 19:55:04 +00:00
verses[versetype][versenum] = [] # storage for lines in this verse
if not verses_seen.has_key(versetag):
verses_seen[versetag] = 1
our_verse_order.append(versetag)
2010-06-15 21:25:50 +00:00
if words:
# Tidy text and remove the ____s from extended words
# words=self.song.tidy_text(words)
words=words.replace('_', '')
2010-06-15 21:25:50 +00:00
verses[versetype][versenum].append(words)
# done parsing
2010-06-28 19:55:04 +00:00
versetypes = verses.keys()
2010-06-15 21:25:50 +00:00
versetypes.sort()
2010-06-28 19:55:04 +00:00
versetags = {}
2010-06-15 21:25:50 +00:00
for v in versetypes:
2010-06-28 19:55:04 +00:00
versenums = verses[v].keys()
2010-06-15 21:25:50 +00:00
versenums.sort()
for n in versenums:
2010-06-28 19:55:04 +00:00
versetag = u'%s%s' %(v,n)
lines = u'\n'.join(verses[v][n])
2010-06-15 21:25:50 +00:00
self.song.verses.append([versetag, lines])
2010-06-28 19:55:04 +00:00
versetags[versetag] = 1 # keep track of what we have for error checking later
2010-06-15 21:25:50 +00:00
# now figure out the presentation order
if u'presentation' in fields and root.presentation != u'':
2010-06-28 19:55:04 +00:00
order = unicode(root.presentation)
order = order.split()
else:
assert len(our_verse_order)>0
2010-06-28 19:55:04 +00:00
order = our_verse_order
for tag in order:
if not versetags.has_key(tag):
print u'Got order', tag, u'but not in versetags, skipping'
2010-06-28 19:55:04 +00:00
raise OpenSongImportError # xxx keep error, or just warn?
else:
self.song.verse_order_list.append(tag)
def finish(self):
""" Separate function, allows test suite to not pollute database"""
self.song.finish()