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

247 lines
9.4 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-07-18 19:27:27 +00:00
from zipfile import ZipFile
2010-06-15 21:25:50 +00:00
from lxml.etree import Element
from lxml import objectify
2010-06-15 20:03:47 +00:00
2010-07-18 19:27:27 +00:00
from openlp.plugins.songs.lib.songimport import SongImport
2010-06-30 20:05:43 +00:00
import logging
log = logging.getLogger(__name__)
class OpenSongImportError(Exception):
pass
class OpenSongImport(object):
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:
2010-06-15 20:03:47 +00:00
. 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-15 20:03:47 +00:00
"""
2010-06-28 19:55:04 +00:00
self.songmanager = songmanager
2010-06-15 20:03:47 +00:00
self.song = None
2010-06-30 20:05:43 +00:00
def do_import(self, filename, commit=True):
2010-06-30 21:19:18 +00:00
"""
Import either a single opensong file, or a zipfile
containing multiple opensong files If the commit parameter is
set False, the import will not be committed to the database
(useful for test scripts)
2010-06-30 21:19:18 +00:00
"""
2010-07-18 19:27:27 +00:00
ext = os.path.splitext(filename)[1]
2010-06-30 20:05:43 +00:00
if ext.lower() == ".zip":
log.info('Zipfile found %s', filename)
2010-07-18 19:27:27 +00:00
z = ZipFile(filename, u'r')
2010-06-30 20:05:43 +00:00
for song in z.infolist():
2010-07-18 19:27:27 +00:00
parts = os.path.split(song.filename)
2010-06-30 20:05:43 +00:00
if parts[-1] == u'':
#No final part => directory
continue
songfile = z.open(song)
2010-06-30 20:05:43 +00:00
self.do_import_file(songfile)
if commit:
self.finish()
else:
log.info('Direct import %s', filename)
file = open(filename)
self.do_import_file(file)
if commit:
self.finish()
2010-07-07 20:16:14 +00:00
def do_import_file(self, file):
2010-06-15 20:03:47 +00:00
"""
Process the OpenSong file - pass in a file-like object,
not a filename
2010-06-15 20:03:47 +00:00
"""
self.song_import = 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_import.add_copyright,
u'ccli':u'ccli_number',
u'author':self.song_import.parse_author,
u'title':u'title',
u'aka':u'alternate_title',
u'hymn_number':u'song_number'}
for (attr, fn_or_string) in decode.items():
if attr in fields:
ustring = unicode(root.__getattr__(attr))
if type(fn_or_string) == type(u''):
self.song_import.__setattr__(fn_or_string, ustring)
else:
fn_or_string(ustring)
2010-07-07 20:16:14 +00:00
res = []
if u'theme' in fields:
self.song_import.topics.append(unicode(root.theme))
2010-07-07 20:16:14 +00:00
if u'alttheme' in fields:
self.song_import.topics.append(unicode(root.alttheme))
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'
2010-07-18 19:27:27 +00:00
for thisline in lyrics.split(u'\n'):
2010-06-15 21:25:50 +00:00
# remove comments
2010-07-18 19:27:27 +00:00
semicolon = thisline.find(u';')
2010-06-15 21:25:50 +00:00
if semicolon >= 0:
2010-07-18 19:27:27 +00:00
thisline = thisline[:semicolon]
thisline = thisline.strip()
if len(thisline) == 0:
2010-06-15 21:25:50 +00:00
continue
2010-07-18 19:27:27 +00:00
# skip inthisline guitar chords and page and column breaks
if thisline[0] == u'.' or thisline.startswith(u'---') \
or thisline.startswith(u'-!!'):
2010-06-15 21:25:50 +00:00
continue
2010-06-15 21:25:50 +00:00
# verse/chorus/etc. marker
2010-07-18 19:27:27 +00:00
if thisline[0] == u'[':
versetype = thisline[1].upper()
if versetype.isdigit():
2010-06-28 19:55:04 +00:00
versenum = versetype
versetype = u'V'
2010-07-18 19:27:27 +00:00
elif thisline[2] != u']':
2010-06-15 21:25:50 +00:00
# there's a number to go with it - extract that as well
2010-07-18 19:27:27 +00:00
right_bracket = thisline.find(u']')
versenum = thisline[2:right_bracket]
2010-06-15 21:25:50 +00:00
else:
# if there's no number, assume it's no.1
versenum = u'1'
2010-06-15 21:25:50 +00:00
continue
words = None
2010-06-15 21:25:50 +00:00
# number at start of line.. it's verse number
2010-07-18 19:27:27 +00:00
if thisline[0].isdigit():
versenum = thisline[0]
words = thisline[1:].strip()
2010-06-15 21:25:50 +00:00
if words is None and \
versenum is not None and \
versetype is not None:
2010-07-18 19:27:27 +00:00
words = thisline
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
2010-07-18 19:27:27 +00:00
words = self.song_import.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 = {}
verse_renames = {}
2010-07-18 19:27:27 +00:00
for versetype in versetypes:
versenums = verses[versetype].keys()
2010-06-15 21:25:50 +00:00
versenums.sort()
2010-07-18 19:27:27 +00:00
for num in versenums:
versetag = u'%s%s' %(versetype,num)
lines = u'\n'.join(verses[versetype][num])
self.song_import.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 len(tag) == 1:
tag = tag + u'1' # Assume it's no.1 if it's not there
if not versetags.has_key(tag):
2010-06-30 21:19:18 +00:00
log.warn(u'Got order %s but not in versetags, skipping', tag)
else:
self.song_import.verse_order_list.append(tag)
def finish(self):
""" Separate function, allows test suite to not pollute database"""
self.song_import.finish()