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 #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2010-12-26 11:04:47 +00:00
|
|
|
# Copyright (c) 2008-2011 Raoul Snyman #
|
2011-05-26 16:25:54 +00:00
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Gerald Britton, Jonathan #
|
|
|
|
# Corwin, Michael Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, #
|
2011-05-26 17:11:22 +00:00
|
|
|
# Armin Köhler, Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias #
|
2011-06-12 16:02:52 +00:00
|
|
|
# Põldaru, Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
|
2011-06-12 15:41:01 +00:00
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, 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
|
2011-04-17 15:47:02 +00:00
|
|
|
import re
|
|
|
|
|
2010-06-15 21:25:50 +00:00
|
|
|
from lxml import objectify
|
2010-08-29 20:05:36 +00:00
|
|
|
from lxml.etree import Error, LxmlError
|
2010-06-15 20:03:47 +00:00
|
|
|
|
2011-04-25 22:15:38 +00:00
|
|
|
from openlp.plugins.songs.lib import VerseType
|
2010-07-18 19:27:27 +00:00
|
|
|
from openlp.plugins.songs.lib.songimport import SongImport
|
2011-04-18 16:46:22 +00:00
|
|
|
from openlp.plugins.songs.lib.ui import SongStrings
|
2010-06-30 20:05:43 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
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
|
|
|
"""
|
2011-02-18 17:34:43 +00:00
|
|
|
SongImport.__init__(self, manager, **kwargs)
|
2010-06-22 20:41:31 +00:00
|
|
|
|
2011-09-05 12:51:16 +00:00
|
|
|
def doImport(self):
|
|
|
|
self.importWizard.progressBar.setMaximum(len(self.importSource))
|
|
|
|
for filename in self.importSource:
|
|
|
|
if self.stopImportFlag:
|
2011-04-19 11:46:25 +00:00
|
|
|
return
|
2011-07-15 18:18:11 +00:00
|
|
|
song_file = open(filename)
|
2011-09-05 12:51:16 +00:00
|
|
|
self.doImportFile(song_file)
|
2011-07-15 18:18:11 +00:00
|
|
|
song_file.close()
|
2010-07-07 20:16:14 +00:00
|
|
|
|
2011-09-05 12:51:16 +00:00
|
|
|
def doImportFile(self, file):
|
2010-06-15 20:03:47 +00:00
|
|
|
"""
|
2011-04-17 15:47:02 +00:00
|
|
|
Process the OpenSong file - pass in a file-like object, not a file path.
|
2010-08-28 23:09:05 +00:00
|
|
|
"""
|
2011-09-05 12:51:16 +00:00
|
|
|
self.setDefaults()
|
2010-08-29 20:05:36 +00:00
|
|
|
try:
|
|
|
|
tree = objectify.parse(file)
|
2010-09-14 14:21:44 +00:00
|
|
|
except (Error, LxmlError):
|
2011-09-05 12:51:16 +00:00
|
|
|
self.logError(file.name, SongStrings.XMLSyntaxError)
|
2010-08-29 20:05:36 +00:00
|
|
|
log.exception(u'Error parsing XML')
|
2011-04-19 11:34:04 +00:00
|
|
|
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'
|
|
|
|
}
|
2010-08-29 20:05:36 +00:00
|
|
|
for attr, fn_or_string in decode.items():
|
2010-06-22 20:41:31 +00:00
|
|
|
if attr in fields:
|
2010-07-18 19:44:49 +00:00
|
|
|
ustring = unicode(root.__getattr__(attr))
|
2010-08-29 20:05:36 +00:00
|
|
|
if isinstance(fn_or_string, basestring):
|
|
|
|
setattr(self, fn_or_string, ustring)
|
2010-07-18 19:44:49 +00:00
|
|
|
else:
|
|
|
|
fn_or_string(ustring)
|
2010-08-28 23:09:05 +00:00
|
|
|
if u'theme' in fields and unicode(root.theme) not in self.topics:
|
|
|
|
self.topics.append(unicode(root.theme))
|
|
|
|
if u'alttheme' in fields and unicode(root.alttheme) not in self.topics:
|
|
|
|
self.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 = {}
|
2011-02-16 09:36:59 +00:00
|
|
|
# keep track of verses appearance order
|
2010-06-28 19:55:04 +00:00
|
|
|
our_verse_order = []
|
2011-02-18 07:53:40 +00:00
|
|
|
# default verse
|
2011-04-17 15:47:02 +00:00
|
|
|
verse_tag = VerseType.Tags[VerseType.Verse]
|
2011-02-18 07:53:40 +00:00
|
|
|
verse_num = u'1'
|
2011-02-16 09:36:59 +00:00
|
|
|
# for the case where song has several sections with same marker
|
|
|
|
inst = 1
|
2011-04-25 22:15:38 +00:00
|
|
|
if u'lyrics' in fields:
|
|
|
|
lyrics = unicode(root.lyrics)
|
|
|
|
else:
|
|
|
|
lyrics = u''
|
2011-02-18 07:53:40 +00:00
|
|
|
for this_line in lyrics.split(u'\n'):
|
2010-06-15 21:25:50 +00:00
|
|
|
# remove comments
|
2011-02-18 07:53:40 +00:00
|
|
|
semicolon = this_line.find(u';')
|
2010-06-15 21:25:50 +00:00
|
|
|
if semicolon >= 0:
|
2011-02-18 07:53:40 +00:00
|
|
|
this_line = this_line[:semicolon]
|
|
|
|
this_line = this_line.strip()
|
|
|
|
if not len(this_line):
|
2010-06-15 21:25:50 +00:00
|
|
|
continue
|
2011-02-16 09:36:59 +00:00
|
|
|
# skip guitar chords and page and column breaks
|
2011-02-18 07:53:40 +00:00
|
|
|
if this_line.startswith(u'.') or this_line.startswith(u'---') \
|
|
|
|
or this_line.startswith(u'-!!'):
|
2010-06-15 21:25:50 +00:00
|
|
|
continue
|
|
|
|
# verse/chorus/etc. marker
|
2011-02-18 07:53:40 +00:00
|
|
|
if this_line.startswith(u'['):
|
2010-09-02 20:21:31 +00:00
|
|
|
# drop the square brackets
|
2011-02-18 07:53:40 +00:00
|
|
|
right_bracket = this_line.find(u']')
|
|
|
|
content = this_line[1:right_bracket].lower()
|
2010-11-03 18:03:28 +00:00
|
|
|
# have we got any digits?
|
2011-02-18 07:53:40 +00:00
|
|
|
# If so, verse number is everything from the digits
|
2010-09-02 20:21:31 +00:00
|
|
|
# to the end (even if there are some alpha chars on the end)
|
2011-04-25 22:37:58 +00:00
|
|
|
match = re.match(u'(\D*)(\d+.*)', content)
|
2010-09-02 20:21:31 +00:00
|
|
|
if match is not None:
|
2011-02-18 07:53:40 +00:00
|
|
|
verse_tag = match.group(1)
|
|
|
|
verse_num = match.group(2)
|
2010-06-15 21:25:50 +00:00
|
|
|
else:
|
2010-11-03 18:03:28 +00:00
|
|
|
# otherwise we assume number 1 and take the whole prefix as
|
2011-02-18 07:53:40 +00:00
|
|
|
# the verse tag
|
|
|
|
verse_tag = content
|
|
|
|
verse_num = u'1'
|
2011-08-16 08:46:55 +00:00
|
|
|
if len(verse_tag) == 0:
|
|
|
|
verse_index = 0
|
|
|
|
else:
|
|
|
|
verse_index = VerseType.from_loose_input(verse_tag)
|
2011-04-25 22:15:38 +00:00
|
|
|
verse_tag = VerseType.Tags[verse_index]
|
2011-02-16 09:36:59 +00:00
|
|
|
inst = 1
|
2011-02-18 07:53:40 +00:00
|
|
|
if [verse_tag, verse_num, inst] in our_verse_order \
|
|
|
|
and verses.has_key(verse_tag) \
|
|
|
|
and verses[verse_tag].has_key(verse_num):
|
2011-04-17 15:47:02 +00:00
|
|
|
inst = len(verses[verse_tag][verse_num]) + 1
|
2010-06-15 21:25:50 +00:00
|
|
|
continue
|
2010-06-22 20:41:31 +00:00
|
|
|
# number at start of line.. it's verse number
|
2011-02-18 07:53:40 +00:00
|
|
|
if this_line[0].isdigit():
|
|
|
|
verse_num = this_line[0]
|
|
|
|
this_line = this_line[1:].strip()
|
|
|
|
our_verse_order.append([verse_tag, verse_num, inst])
|
|
|
|
if not verses.has_key(verse_tag):
|
|
|
|
verses[verse_tag] = {}
|
|
|
|
if not verses[verse_tag].has_key(verse_num):
|
|
|
|
verses[verse_tag][verse_num] = {}
|
|
|
|
if not verses[verse_tag][verse_num].has_key(inst):
|
|
|
|
verses[verse_tag][verse_num][inst] = []
|
2011-04-25 22:15:38 +00:00
|
|
|
our_verse_order.append([verse_tag, verse_num, inst])
|
2011-02-16 09:43:07 +00:00
|
|
|
# Tidy text and remove the ____s from extended words
|
2011-02-18 07:53:40 +00:00
|
|
|
this_line = self.tidy_text(this_line)
|
|
|
|
this_line = this_line.replace(u'_', u'')
|
|
|
|
this_line = this_line.replace(u'|', u'\n')
|
|
|
|
verses[verse_tag][verse_num][inst].append(this_line)
|
2010-06-15 21:25:50 +00:00
|
|
|
# done parsing
|
2011-02-16 09:36:59 +00:00
|
|
|
# add verses in original order
|
2011-02-18 07:53:40 +00:00
|
|
|
for (verse_tag, verse_num, inst) in our_verse_order:
|
|
|
|
verse_def = u'%s%s' % (verse_tag, verse_num)
|
|
|
|
lines = u'\n'.join(verses[verse_tag][verse_num][inst])
|
|
|
|
self.add_verse(lines, verse_def)
|
2011-04-25 22:15:38 +00:00
|
|
|
if not self.verses:
|
|
|
|
self.add_verse('')
|
2011-02-16 09:36:59 +00:00
|
|
|
# figure out the presentation order, if present
|
2011-04-18 18:03:41 +00:00
|
|
|
if u'presentation' in fields and root.presentation:
|
2010-06-28 19:55:04 +00:00
|
|
|
order = unicode(root.presentation)
|
2011-02-16 19:28:55 +00:00
|
|
|
# We make all the tags in the lyrics lower case, so match that here
|
2010-09-02 20:21:31 +00:00
|
|
|
# and then split into a list on the whitespace
|
2011-02-16 19:28:55 +00:00
|
|
|
order = order.lower().split()
|
2011-02-18 07:53:40 +00:00
|
|
|
for verse_def in order:
|
2011-04-25 22:37:58 +00:00
|
|
|
match = re.match(u'(\D*)(\d+.*)', verse_def)
|
2011-02-16 09:36:59 +00:00
|
|
|
if match is not None:
|
2011-02-18 07:53:40 +00:00
|
|
|
verse_tag = match.group(1)
|
|
|
|
verse_num = match.group(2)
|
|
|
|
if not len(verse_tag):
|
2011-04-18 18:03:41 +00:00
|
|
|
verse_tag = VerseType.Tags[VerseType.Verse]
|
2011-02-16 09:36:59 +00:00
|
|
|
else:
|
|
|
|
# Assume it's no.1 if there are no digits
|
2011-02-18 07:53:40 +00:00
|
|
|
verse_tag = verse_def
|
|
|
|
verse_num = u'1'
|
|
|
|
verse_def = u'%s%s' % (verse_tag, verse_num)
|
2011-04-18 16:46:22 +00:00
|
|
|
if verses.has_key(verse_tag) and \
|
|
|
|
verses[verse_tag].has_key(verse_num):
|
2011-02-18 07:53:40 +00:00
|
|
|
self.verse_order_list.append(verse_def)
|
2011-02-16 09:36:59 +00:00
|
|
|
else:
|
2011-02-18 07:53:40 +00:00
|
|
|
log.info(u'Got order %s but not in verse tags, dropping'
|
|
|
|
u'this item from presentation order', verse_def)
|
2011-04-18 16:46:22 +00:00
|
|
|
if not self.finish():
|
2011-09-05 12:51:16 +00:00
|
|
|
self.logError(file.name)
|