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

311 lines
12 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 #
2010-07-24 22:10:47 +00:00
# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian #
# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard, Frode Woldsund #
2010-06-15 20:03:47 +00:00
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
2010-07-19 20:43:02 +00:00
import logging
2010-06-15 20:03:47 +00:00
import os
2010-07-18 19:27:27 +00:00
from zipfile import ZipFile
2010-06-15 21:25:50 +00:00
from lxml import objectify
from lxml.etree import Error, LxmlError
2010-09-02 20:21:31 +00:00
import re
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
log = logging.getLogger(__name__)
class OpenSongImportError(Exception):
pass
2010-08-28 23:09:05 +00:00
class OpenSongImport(SongImport):
2010-06-15 20:03:47 +00:00
"""
2010-08-28 23:09:05 +00:00
Import songs exported from OpenSong
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
The format is described loosly on the `OpenSong File Format Specification
<http://www.opensong.org/d/manual/song_file_format_specification>`_ page on
the OpenSong web site. However, it doesn't describe the <lyrics> section,
so here's an attempt:
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
Verses can be expressed in one of 2 ways, either in complete verses, or by
line grouping, i.e. grouping all line 1's of a verse together, all line 2's
of a verse together, and so on.
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
An example of complete verses::
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
<lyrics>
[v1]
List of words
Another Line
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
[v2]
Some words for the 2nd verse
etc...
</lyrics>
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
The 'v' in the verse specifiers above can be left out, it is implied.
An example of line grouping::
<lyrics>
[V]
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. Additionally, 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:
2010-08-28 23:09:05 +00:00
C
Chorus
B
Bridge
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
All verses are imported and tagged appropriately.
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
Guitar chords can be provided "above" the lyrics (the line is preceeded by
a period "."), and one or more "_" can be used to signify long-drawn-out
words. Chords and "_" are removed by this importer. For example::
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
. A7 Bm
1 Some____ Words
2010-06-15 20:03:47 +00:00
2010-08-28 23:09:05 +00:00
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.
2010-06-15 20:03:47 +00:00
"""
2010-08-28 23:09:05 +00:00
def __init__(self, manager, **kwargs):
2010-06-15 20:03:47 +00:00
"""
2010-08-28 23:09:05 +00:00
Initialise the class.
2010-06-15 20:03:47 +00:00
"""
2010-08-28 23:09:05 +00:00
SongImport.__init__(self, manager)
self.filenames = kwargs[u'filenames']
2010-06-15 20:03:47 +00:00
self.song = None
2010-08-28 23:09:05 +00:00
self.commit = True
2010-08-28 23:09:05 +00:00
def do_import(self):
2010-06-30 21:19:18 +00:00
"""
2010-08-30 20:19:16 +00:00
Import either each of the files in self.filenames - each element of
which can be either a single opensong file, or a zipfile containing
multiple opensong files. If `self.commit` 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-08-28 23:09:05 +00:00
success = False
numfiles = 0
for filename in self.filenames:
ext = os.path.splitext(filename)[1]
if ext.lower() == u'.zip':
z = ZipFile(filename, u'r')
numfiles += len(z.infolist())
else:
numfiles += 1
log.debug("Total number of files: %d", numfiles)
self.import_wizard.importProgressBar.setMaximum(numfiles)
2010-08-28 23:09:05 +00:00
for filename in self.filenames:
if self.stop_import_flag:
break
ext = os.path.splitext(filename)[1]
if ext.lower() == u'.zip':
2010-08-30 20:19:16 +00:00
log.info(u'Zipfile found %s', filename)
2010-08-28 23:09:05 +00:00
z = ZipFile(filename, u'r')
for song in z.infolist():
if self.stop_import_flag:
break
parts = os.path.split(song.filename)
if parts[-1] == u'':
#No final part => directory
continue
2010-08-30 20:19:16 +00:00
log.info(u'Zip importing %s', parts[-1])
2010-08-28 23:09:05 +00:00
self.import_wizard.incrementProgressBar(u'Importing %s...' \
% parts[-1])
songfile = z.open(song)
self.do_import_file(songfile)
if self.commit:
self.finish()
if self.stop_import_flag:
break
else:
log.info('Direct import %s', filename)
self.import_wizard.incrementProgressBar(u'Importing %s...' \
% os.path.split(filename)[-1])
file = open(filename)
self.do_import_file(file)
if self.commit:
2010-06-30 20:05:43 +00:00
self.finish()
2010-09-06 19:56:20 +00:00
if self.commit:
2010-08-28 23:09:05 +00:00
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-08-28 23:09:05 +00:00
"""
2010-09-06 19:42:39 +00:00
# self.setDefaults()
# Setup blank storage to append to
verse_order_list = []
topics = []
verselist = []
try:
tree = objectify.parse(file)
except Error, LxmlError:
log.exception(u'Error parsing XML')
return
2010-06-28 19:55:04 +00:00
root = tree.getroot()
fields = dir(root)
2010-08-28 23:09:05 +00:00
decode = {
u'copyright': self.add_copyright,
u'ccli': u'ccli_number',
u'author': self.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 isinstance(fn_or_string, basestring):
setattr(self, fn_or_string, ustring)
else:
fn_or_string(ustring)
if u'theme' in fields and unicode(root.theme) not in topics:
topics.append(unicode(root.theme))
if u'alttheme' in fields and unicode(root.alttheme) not in topics:
topics.append(unicode(root.alttheme))
2010-09-02 20:21:31 +00:00
lyrics = unicode(root.lyrics)
2010-06-15 21:25:50 +00:00
# data storage while importing
2010-06-28 19:55:04 +00:00
verses = {}
# 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-08-28 23:09:05 +00:00
versenum = None
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
# verse/chorus/etc. marker
2010-07-18 19:27:27 +00:00
if thisline[0] == u'[':
2010-09-02 20:21:31 +00:00
# drop the square brackets
right_bracket = thisline.find(u']')
content = thisline[1:right_bracket].upper()
# have we got any digits? If so, versenumber is everything from the digits
# to the end (even if there are some alpha chars on the end)
match = re.match(u'(.*)(\d+.*)', content)
if match is not None:
versetype = match.group(1)
versenum = match.group(2)
# otherwise we assume number 1 and take the whole prefix as versetype
2010-06-15 21:25:50 +00:00
else:
2010-09-02 20:21:31 +00:00
versetype = content
versenum = u'1'
2010-06-15 21:25:50 +00:00
continue
words = None
# 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()
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 not versenum:
versenum = u'1'
if versenum is not None:
2010-07-20 20:43:42 +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-07-20 20:43:42 +00:00
# storage for lines in this verse
verses[versetype][versenum] = []
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-08-28 23:09:05 +00:00
words = self.tidy_text(words)
2010-07-18 19:27:27 +00:00
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-07-18 19:27:27 +00:00
for versetype in versetypes:
our_verse_type = versetype
if our_verse_type == u'':
our_verse_type = u'V'
2010-07-18 19:27:27 +00:00
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' % (our_verse_type, num)
2010-07-18 19:27:27 +00:00
lines = u'\n'.join(verses[versetype][num])
verselist.append([versetag, lines])
2010-07-20 20:43:42 +00:00
# Keep track of what we have for error checking later
versetags[versetag] = 1
2010-06-15 21:25:50 +00:00
# now figure out the presentation order
order = []
if u'presentation' in fields and root.presentation != u'':
2010-06-28 19:55:04 +00:00
order = unicode(root.presentation)
2010-09-02 20:21:31 +00:00
# We make all the tags in the lyrics upper case, so match that here
# and then split into a list on the whitespace
order = order.upper().split()
else:
if len(our_verse_order) > 0:
order = our_verse_order
else:
log.warn(u'No verse order available (either explicit or inferred) for %s, skipping.', self.title)
for tag in order:
2010-09-02 20:21:31 +00:00
if tag[0].isdigit():
tag = u'V' + tag # Assume it's a verse if it has no prefix
elif not re.search('\d+', tag):
tag = tag + u'1' # Assume it's no.1 if there's no digits
if not versetags.has_key(tag):
2010-09-02 19:43:28 +00:00
log.info(u'Got order %s but not in versetags, dropping this item from presentation order', tag)
else:
verse_order_list.append(tag)
# now copy the data
self.topics = topics
self.verse_order_list = verse_order_list
self.verses = verselist
2010-09-06 19:56:20 +00:00
# xxx sort out where to call setdefaults
# xxx need to make calls to insert to database here