2010-04-03 00:13:07 +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-04-03 00:13:07 +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-04 16:40:53 +00:00
|
|
|
"""
|
|
|
|
The :mod:`xml` module provides the XML functionality for songs
|
2010-04-03 00:13:07 +00:00
|
|
|
|
2010-07-04 16:40:53 +00:00
|
|
|
The basic XML is of the format::
|
2010-04-03 00:13:07 +00:00
|
|
|
|
2010-07-04 16:40:53 +00:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<song version="1.0">
|
|
|
|
<lyrics language="en">
|
|
|
|
<verse type="chorus" label="1">
|
|
|
|
<![CDATA[ ... ]]>
|
|
|
|
</verse>
|
|
|
|
</lyrics>
|
|
|
|
</song>
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from lxml import etree, objectify
|
2010-11-27 20:51:54 +00:00
|
|
|
from openlp.plugins.songs.lib.db import Author, Song
|
2010-07-04 16:40:53 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class SongXMLBuilder(object):
|
2010-04-03 00:13:07 +00:00
|
|
|
"""
|
2010-07-04 16:40:53 +00:00
|
|
|
This class builds the XML used to describe songs.
|
|
|
|
"""
|
|
|
|
log.info(u'SongXMLBuilder Loaded')
|
2010-04-03 00:13:07 +00:00
|
|
|
|
2010-07-15 20:26:57 +00:00
|
|
|
def __init__(self, song_language=None):
|
2010-07-04 16:40:53 +00:00
|
|
|
"""
|
|
|
|
Set up the song builder.
|
2010-04-03 00:13:07 +00:00
|
|
|
|
2010-07-04 16:40:53 +00:00
|
|
|
``song_language``
|
|
|
|
The language used in this song
|
|
|
|
"""
|
|
|
|
lang = u'en'
|
|
|
|
if song_language:
|
|
|
|
lang = song_language
|
|
|
|
self.song_xml = objectify.fromstring(u'<song version="1.0" />')
|
|
|
|
self.lyrics = etree.SubElement(self.song_xml, u'lyrics', language=lang)
|
|
|
|
|
|
|
|
def add_verse_to_lyrics(self, type, number, content):
|
|
|
|
"""
|
|
|
|
Add a verse to the ``<lyrics>`` tag.
|
|
|
|
|
|
|
|
``type``
|
|
|
|
A string denoting the type of verse. Possible values are "Chorus",
|
|
|
|
"Verse", "Bridge", and "Custom".
|
|
|
|
|
|
|
|
``number``
|
|
|
|
An integer denoting the number of the item, for example: verse 1.
|
|
|
|
|
|
|
|
``content``
|
|
|
|
The actual text of the verse to be stored.
|
|
|
|
"""
|
2010-07-07 20:17:58 +00:00
|
|
|
# log.debug(u'add_verse_to_lyrics %s, %s\n%s' % (type, number, content))
|
2010-07-18 19:27:27 +00:00
|
|
|
verse = etree.Element(u'verse', type = unicode(type),
|
|
|
|
label = unicode(number))
|
2010-07-04 16:40:53 +00:00
|
|
|
verse.text = etree.CDATA(content)
|
2010-07-05 21:23:39 +00:00
|
|
|
self.lyrics.append(verse)
|
2010-07-04 16:40:53 +00:00
|
|
|
|
|
|
|
def dump_xml(self):
|
|
|
|
"""
|
|
|
|
Debugging aid to dump XML so that we can see what we have.
|
|
|
|
"""
|
2010-07-15 20:26:57 +00:00
|
|
|
return etree.tostring(self.song_xml, encoding=u'UTF-8',
|
2010-07-04 16:40:53 +00:00
|
|
|
xml_declaration=True, pretty_print=True)
|
|
|
|
|
|
|
|
def extract_xml(self):
|
|
|
|
"""
|
|
|
|
Extract our newly created XML song.
|
|
|
|
"""
|
2010-07-15 20:26:57 +00:00
|
|
|
return etree.tostring(self.song_xml, encoding=u'UTF-8',
|
2010-07-04 16:40:53 +00:00
|
|
|
xml_declaration=True)
|
|
|
|
|
2010-11-27 20:51:54 +00:00
|
|
|
|
2010-07-04 16:40:53 +00:00
|
|
|
class SongXMLParser(object):
|
|
|
|
"""
|
|
|
|
A class to read in and parse a song's XML.
|
|
|
|
"""
|
|
|
|
log.info(u'SongXMLParser Loaded')
|
|
|
|
|
|
|
|
def __init__(self, xml):
|
|
|
|
"""
|
|
|
|
Set up our song XML parser.
|
|
|
|
|
|
|
|
``xml``
|
|
|
|
The XML of the song to be parsed.
|
|
|
|
"""
|
|
|
|
self.song_xml = None
|
2010-07-05 11:39:48 +00:00
|
|
|
if xml[:5] == u'<?xml':
|
|
|
|
xml = xml[38:]
|
2010-07-04 16:40:53 +00:00
|
|
|
try:
|
2010-07-05 11:39:48 +00:00
|
|
|
self.song_xml = objectify.fromstring(xml)
|
2010-07-04 16:40:53 +00:00
|
|
|
except etree.XMLSyntaxError:
|
|
|
|
log.exception(u'Invalid xml %s', xml)
|
|
|
|
|
|
|
|
def get_verses(self):
|
|
|
|
"""
|
|
|
|
Iterates through the verses in the XML and returns a list of verses
|
|
|
|
and their attributes.
|
|
|
|
"""
|
|
|
|
xml_iter = self.song_xml.getiterator()
|
|
|
|
verse_list = []
|
|
|
|
for element in xml_iter:
|
|
|
|
if element.tag == u'verse':
|
|
|
|
if element.text is None:
|
|
|
|
element.text = u''
|
2010-07-05 11:39:48 +00:00
|
|
|
verse_list.append([element.attrib, unicode(element.text)])
|
2010-07-04 16:40:53 +00:00
|
|
|
return verse_list
|
|
|
|
|
|
|
|
def dump_xml(self):
|
|
|
|
"""
|
|
|
|
Debugging aid to dump XML so that we can see what we have.
|
|
|
|
"""
|
|
|
|
return etree.dump(self.song_xml)
|
|
|
|
|
|
|
|
|
|
|
|
class LyricsXML(object):
|
|
|
|
"""
|
|
|
|
This class represents the XML in the ``lyrics`` field of a song.
|
2010-04-03 00:13:07 +00:00
|
|
|
"""
|
|
|
|
def __init__(self, song=None):
|
|
|
|
if song:
|
|
|
|
if song.lyrics.startswith(u'<?xml'):
|
|
|
|
self.parse(song.lyrics)
|
|
|
|
else:
|
|
|
|
self.extract(song.lyrics)
|
|
|
|
else:
|
2010-04-03 16:43:29 +00:00
|
|
|
self.languages = []
|
2010-04-03 00:13:07 +00:00
|
|
|
|
|
|
|
def parse(self, xml):
|
|
|
|
"""
|
|
|
|
Parse XML from the ``lyrics`` field in the database, and set the list
|
|
|
|
of verses from it.
|
|
|
|
|
|
|
|
``xml``
|
|
|
|
The XML to parse.
|
|
|
|
"""
|
|
|
|
try:
|
2010-04-03 16:43:29 +00:00
|
|
|
self.languages = []
|
2010-04-03 00:13:07 +00:00
|
|
|
song = objectify.fromstring(xml)
|
|
|
|
for lyrics in song.lyrics:
|
2010-04-03 16:43:29 +00:00
|
|
|
language = {
|
2010-04-03 00:13:07 +00:00
|
|
|
u'language': lyrics.attrib[u'language'],
|
|
|
|
u'verses': []
|
|
|
|
}
|
|
|
|
for verse in lyrics.verse:
|
2010-04-03 16:43:29 +00:00
|
|
|
language[u'verses'].append({
|
2010-04-03 00:13:07 +00:00
|
|
|
u'type': verse.attrib[u'type'],
|
|
|
|
u'label': verse.attrib[u'label'],
|
|
|
|
u'text': unicode(verse.text)
|
|
|
|
})
|
2010-04-03 16:43:29 +00:00
|
|
|
self.lyrics.append(language)
|
2010-04-03 00:13:07 +00:00
|
|
|
return True
|
2010-07-04 16:40:53 +00:00
|
|
|
except etree.XMLSyntaxError:
|
2010-04-03 00:13:07 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def extract(self, text):
|
|
|
|
"""
|
|
|
|
If the ``lyrics`` field in the database is not XML, this method is
|
|
|
|
called and used to construct the verse structure similar to the output
|
|
|
|
of the ``parse`` function.
|
|
|
|
|
|
|
|
``text``
|
|
|
|
The text to pull verses out of.
|
|
|
|
"""
|
|
|
|
text = text.replace('\r\n', '\n')
|
|
|
|
verses = text.split('\n\n')
|
2010-04-03 16:43:29 +00:00
|
|
|
self.languages = [{u'language': u'en', u'verses': []}]
|
2010-04-03 00:13:07 +00:00
|
|
|
counter = 0
|
|
|
|
for verse in verses:
|
|
|
|
counter = counter + 1
|
2010-04-03 16:43:29 +00:00
|
|
|
self.languages[0][u'verses'].append({
|
2010-04-03 00:13:07 +00:00
|
|
|
u'type': u'verse',
|
|
|
|
u'label': unicode(counter),
|
|
|
|
u'text': verse
|
|
|
|
})
|
|
|
|
return True
|
|
|
|
|
|
|
|
def add_verse(self, type, label, text):
|
|
|
|
"""
|
|
|
|
Add a verse to the list of verses.
|
|
|
|
|
|
|
|
``type``
|
|
|
|
The type of list, one of "verse", "chorus", "bridge", "pre-chorus",
|
|
|
|
"intro", "outtro".
|
|
|
|
|
|
|
|
``label``
|
|
|
|
The number associated with this verse, like 1 or 2.
|
|
|
|
|
|
|
|
``text``
|
|
|
|
The text of the verse.
|
|
|
|
"""
|
|
|
|
self.verses.append({
|
|
|
|
u'type': type,
|
|
|
|
u'label': label,
|
|
|
|
u'text': text
|
|
|
|
})
|
|
|
|
|
|
|
|
def export(self):
|
|
|
|
"""
|
|
|
|
Build up the XML for the verse structure.
|
|
|
|
"""
|
|
|
|
lyrics_output = u''
|
2010-04-03 16:43:29 +00:00
|
|
|
for language in self.languages:
|
2010-04-03 00:13:07 +00:00
|
|
|
verse_output = u''
|
2010-04-03 16:43:29 +00:00
|
|
|
for verse in language[u'verses']:
|
2010-04-03 00:13:07 +00:00
|
|
|
verse_output = verse_output + \
|
|
|
|
u'<verse type="%s" label="%s"><![CDATA[%s]]></verse>' % \
|
|
|
|
(verse[u'type'], verse[u'label'], verse[u'text'])
|
|
|
|
lyrics_output = lyrics_output + \
|
|
|
|
u'<lyrics language="%s">%s</lyrics>' % \
|
2010-04-03 16:43:29 +00:00
|
|
|
(language[u'language'], verse_output)
|
2010-04-03 00:13:07 +00:00
|
|
|
song_output = u'<?xml version="1.0" encoding="UTF-8"?>' + \
|
|
|
|
u'<song version="1.0">%s</song>' % lyrics_output
|
2010-07-27 09:32:52 +00:00
|
|
|
return song_output
|
2010-11-27 15:25:00 +00:00
|
|
|
|
2010-11-27 20:51:54 +00:00
|
|
|
|
2010-11-27 15:25:00 +00:00
|
|
|
class OpenLyricsParser(object):
|
|
|
|
"""
|
|
|
|
This class represents the converter for Song to/from OpenLyrics XML.
|
|
|
|
"""
|
2010-11-27 20:51:54 +00:00
|
|
|
def song_to_xml(self, song):
|
2010-11-27 15:25:00 +00:00
|
|
|
"""
|
|
|
|
Convert the song to OpenLyrics Format
|
|
|
|
"""
|
|
|
|
songXML = SongXMLParser(song.lyrics)
|
|
|
|
verseList = songXML.get_verses()
|
|
|
|
song_xml = objectify.fromstring(
|
|
|
|
u'<song version="0.7" createdIn="OpenLP 2.0"/>')
|
|
|
|
properties = etree.SubElement(song_xml, u'properties')
|
|
|
|
titles = etree.SubElement(properties, u'titles')
|
|
|
|
self.add_text_to_element(u'title', titles, song.title)
|
|
|
|
if song.alternate_title:
|
|
|
|
self.add_text_to_element(u'title', titles, song.alternate_title)
|
|
|
|
if song.theme_name:
|
|
|
|
themes = etree.SubElement(properties, u'themes')
|
|
|
|
self.add_text_to_element(u'theme', themes, song.theme_name)
|
|
|
|
self.add_text_to_element(u'copyright', properties, song.copyright)
|
|
|
|
self.add_text_to_element(u'verseOrder', properties, song.verse_order)
|
|
|
|
if song.ccli_number:
|
|
|
|
self.add_text_to_element(u'ccliNo', properties, song.ccli_number)
|
|
|
|
authors = etree.SubElement(properties, u'authors')
|
|
|
|
for author in song.authors:
|
|
|
|
self.add_text_to_element(u'author', authors, author.display_name)
|
|
|
|
lyrics = etree.SubElement(song_xml, u'lyrics')
|
|
|
|
for verse in verseList:
|
|
|
|
verseTag = u'%s%s' % (
|
|
|
|
verse[0][u'type'][0].lower(), verse[0][u'label'])
|
|
|
|
element = self.add_text_to_element(u'verse', lyrics, None, verseTag)
|
|
|
|
element = self.add_text_to_element(u'lines', element)
|
|
|
|
for line in unicode(verse[1]).split(u'\n'):
|
|
|
|
self.add_text_to_element(u'line', element, line)
|
2010-11-27 20:51:54 +00:00
|
|
|
self.xml_to_song(self.extract_xml(song_xml))
|
|
|
|
return u'' #self.xml_to_song(self.extract_xml(song_xml))
|
2010-11-27 15:25:00 +00:00
|
|
|
|
|
|
|
def add_text_to_element(self, tag, parent, text=None, label=None):
|
|
|
|
if label:
|
|
|
|
element = etree.Element(tag, name = unicode(label))
|
|
|
|
else:
|
|
|
|
element = etree.Element(tag)
|
|
|
|
if text:
|
|
|
|
element.text = unicode(text)
|
|
|
|
parent.append(element)
|
|
|
|
return element
|
|
|
|
|
2010-11-27 20:51:54 +00:00
|
|
|
def xml_to_song(self, xml):
|
2010-11-27 15:25:00 +00:00
|
|
|
"""
|
|
|
|
Create a Song from OpenLyrics format xml
|
|
|
|
"""
|
|
|
|
return 0
|
2010-11-27 20:51:54 +00:00
|
|
|
song = Song()
|
|
|
|
if xml[:5] == u'<?xml':
|
|
|
|
xml = xml[38:]
|
|
|
|
song = objectify.fromstring(xml)
|
|
|
|
print objectify.dump(song)
|
|
|
|
for properties in song.properties:
|
|
|
|
song.copyright = properties.copyright.text
|
|
|
|
song.verse_order = verseOrder.text
|
|
|
|
try:
|
|
|
|
song.ccli_number = properties.ccliNo.text
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
song.theme_name = properties.themes.theme
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
for title in properties.titles.title:
|
|
|
|
if not song.title:
|
|
|
|
song.title = "aa" + title.text
|
|
|
|
else:
|
|
|
|
song.alternate_title = title.text
|
|
|
|
for author in properties.authors.author:
|
|
|
|
print author.text
|
|
|
|
for lyrics in song.lyrics:
|
|
|
|
for verse in song.lyrics.verse:
|
|
|
|
print "verse", verse.attrib
|
|
|
|
for line in lyrics.verse.lines.line:
|
|
|
|
print line
|
|
|
|
return 0
|
2010-11-27 15:25:00 +00:00
|
|
|
|
|
|
|
def dump_xml(self, xml):
|
|
|
|
"""
|
|
|
|
Debugging aid to dump XML so that we can see what we have.
|
|
|
|
"""
|
|
|
|
return etree.tostring(xml, encoding=u'UTF-8',
|
|
|
|
xml_declaration=True, pretty_print=True)
|
|
|
|
|
|
|
|
def extract_xml(self, xml):
|
|
|
|
"""
|
|
|
|
Extract our newly created XML song.
|
|
|
|
"""
|
|
|
|
return etree.tostring(xml, encoding=u'UTF-8',
|
|
|
|
xml_declaration=True)
|