2009-04-06 18:45:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
2009-07-14 13:51:27 +00:00
|
|
|
|
2009-09-08 19:58:05 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2009-12-31 12:52:01 +00:00
|
|
|
# Copyright (c) 2008-2010 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
|
2010-03-21 23:58:01 +00:00
|
|
|
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
|
|
|
|
# Thompson, Jon Tibble, Carsten Tinggaard #
|
2009-09-08 19:58:05 +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-06-10 21:30:50 +00:00
|
|
|
"""
|
|
|
|
The :mod:`songxmlhandler` module provides the XML functionality for songs
|
|
|
|
|
|
|
|
The basic XML is of the format::
|
|
|
|
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<song version="1.0">
|
|
|
|
<lyrics language="en">
|
|
|
|
<verse type="chorus" label="1">
|
|
|
|
<![CDATA[ ... ]]>
|
|
|
|
</verse>
|
|
|
|
</lyrics>
|
|
|
|
</song>
|
|
|
|
"""
|
2009-07-14 13:51:27 +00:00
|
|
|
|
2009-06-21 16:26:33 +00:00
|
|
|
import logging
|
2010-05-27 20:56:34 +00:00
|
|
|
|
2009-07-10 13:16:15 +00:00
|
|
|
from xml.dom.minidom import Document
|
2009-04-06 18:45:45 +00:00
|
|
|
from xml.etree.ElementTree import ElementTree, XML, dump
|
2010-05-27 20:56:34 +00:00
|
|
|
from xml.parsers.expat import ExpatError
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2010-02-27 15:31:23 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2009-07-14 13:51:27 +00:00
|
|
|
class SongXMLBuilder(object):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
This class builds the XML used to describe songs.
|
|
|
|
"""
|
2009-10-31 10:19:30 +00:00
|
|
|
log.info(u'SongXMLBuilder Loaded')
|
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def __init__(self):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Set up the song builder.
|
|
|
|
"""
|
2009-04-06 18:45:45 +00:00
|
|
|
# Create the minidom document
|
2009-03-13 06:13:11 +00:00
|
|
|
self.song_xml = Document()
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def new_document(self):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Create a new song XML document.
|
|
|
|
"""
|
2009-03-13 06:13:11 +00:00
|
|
|
# Create the <song> base element
|
2009-03-14 07:08:15 +00:00
|
|
|
self.song = self.song_xml.createElement(u'song')
|
2009-03-13 06:13:11 +00:00
|
|
|
self.song_xml.appendChild(self.song)
|
2009-03-14 07:08:15 +00:00
|
|
|
self.song.setAttribute(u'version', u'1.0')
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def add_lyrics_to_song(self):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Set up and add a ``<lyrics>`` tag which contains the lyrics of the
|
|
|
|
song.
|
|
|
|
"""
|
2009-03-13 06:13:11 +00:00
|
|
|
# Create the main <lyrics> element
|
2009-03-14 07:08:15 +00:00
|
|
|
self.lyrics = self.song_xml.createElement(u'lyrics')
|
|
|
|
self.lyrics.setAttribute(u'language', u'en')
|
2009-03-13 06:13:11 +00:00
|
|
|
self.song.appendChild(self.lyrics)
|
2009-04-06 18:45:45 +00:00
|
|
|
|
|
|
|
def add_verse_to_lyrics(self, type, number, content):
|
2009-03-13 06:13:11 +00:00
|
|
|
"""
|
2009-07-10 13:16:15 +00:00
|
|
|
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.
|
2009-03-13 06:13:11 +00:00
|
|
|
"""
|
2009-10-31 10:19:30 +00:00
|
|
|
#log.debug(u'add_verse_to_lyrics %s, %s\n%s' % (type, number, content))
|
2009-03-14 07:08:15 +00:00
|
|
|
verse = self.song_xml.createElement(u'verse')
|
|
|
|
verse.setAttribute(u'type', type)
|
2009-04-06 18:45:45 +00:00
|
|
|
verse.setAttribute(u'label', number)
|
2009-03-13 06:13:11 +00:00
|
|
|
self.lyrics.appendChild(verse)
|
2009-07-10 13:16:15 +00:00
|
|
|
# add data as a CDATA section to protect the XML from special chars
|
2009-03-13 06:13:11 +00:00
|
|
|
cds = self.song_xml.createCDATASection(content)
|
|
|
|
verse.appendChild(cds)
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def dump_xml(self):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Debugging aid to dump XML so that we can see what we have.
|
|
|
|
"""
|
2009-09-26 18:22:10 +00:00
|
|
|
return self.song_xml.toprettyxml(indent=u' ')
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def extract_xml(self):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Extract our newly created XML song.
|
|
|
|
"""
|
2009-06-21 16:26:33 +00:00
|
|
|
return self.song_xml.toxml(u'utf-8')
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-07-14 13:51:27 +00:00
|
|
|
|
|
|
|
class SongXMLParser(object):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
A class to read in and parse a song's XML.
|
|
|
|
"""
|
2009-06-21 16:26:33 +00:00
|
|
|
log.info(u'SongXMLParser Loaded')
|
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def __init__(self, xml):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Set up our song XML parser.
|
|
|
|
|
|
|
|
``xml``
|
|
|
|
The XML of the song to be parsed.
|
|
|
|
"""
|
2009-12-16 17:54:45 +00:00
|
|
|
self.song_xml = None
|
2009-06-21 16:26:33 +00:00
|
|
|
try:
|
2009-11-12 00:44:26 +00:00
|
|
|
self.song_xml = ElementTree(
|
|
|
|
element=XML(unicode(xml).encode('unicode-escape')))
|
2010-05-27 14:41:47 +00:00
|
|
|
except ExpatError:
|
2009-09-20 18:47:18 +00:00
|
|
|
log.exception(u'Invalid xml %s', xml)
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def get_verses(self):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Iterates through the verses in the XML and returns a list of verses
|
|
|
|
and their attributes.
|
|
|
|
"""
|
2010-05-27 14:41:47 +00:00
|
|
|
xml_iter = self.song_xml.getiterator()
|
2009-03-13 06:13:11 +00:00
|
|
|
verse_list = []
|
2010-05-27 14:41:47 +00:00
|
|
|
for element in xml_iter:
|
2009-03-14 07:08:15 +00:00
|
|
|
if element.tag == u'verse':
|
2010-03-21 14:24:41 +00:00
|
|
|
if element.text is None:
|
|
|
|
element.text = u''
|
2009-11-12 00:44:26 +00:00
|
|
|
verse_list.append([element.attrib,
|
|
|
|
unicode(element.text).decode('unicode-escape')])
|
2009-03-13 06:13:11 +00:00
|
|
|
return verse_list
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def dump_xml(self):
|
2009-07-10 13:16:15 +00:00
|
|
|
"""
|
|
|
|
Debugging aid to dump XML so that we can see what we have.
|
|
|
|
"""
|
2010-02-27 15:31:23 +00:00
|
|
|
return dump(self.song_xml)
|