forked from openlp/openlp
XML: merge song XML handling
This commit is contained in:
parent
765cc6ddbe
commit
e2a2500a1a
@ -137,7 +137,7 @@ class VerseType(object):
|
||||
unicode(VerseType.to_string(VerseType.Other)).lower():
|
||||
return VerseType.Other
|
||||
|
||||
from songxmlhandler import SongXMLBuilder, SongXMLParser
|
||||
from xml import LyricsXML, SongXMLBuilder, SongXMLParser
|
||||
from songstab import SongsTab
|
||||
from mediaitem import SongMediaItem
|
||||
from songimport import SongImport
|
||||
@ -146,4 +146,3 @@ try:
|
||||
from oooimport import OooImport
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
@ -1,136 +0,0 @@
|
||||
# -*- 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 #
|
||||
###############################################################################
|
||||
"""
|
||||
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>
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from lxml import etree, objectify
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class SongXMLBuilder(object):
|
||||
"""
|
||||
This class builds the XML used to describe songs.
|
||||
"""
|
||||
log.info(u'SongXMLBuilder Loaded')
|
||||
|
||||
def __init__(self, song_language=None):
|
||||
"""
|
||||
Set up the song builder.
|
||||
|
||||
``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.
|
||||
"""
|
||||
#log.debug(u'add_verse_to_lyrics %s, %s\n%s' % (type, number, content))
|
||||
verse = etree.SubElement(self.lyrics, u'verse', type=type, label=number)
|
||||
verse.text = etree.CDATA(content)
|
||||
|
||||
def dump_xml(self):
|
||||
"""
|
||||
Debugging aid to dump XML so that we can see what we have.
|
||||
"""
|
||||
return etree.tostring(self.song_xml, encoding=u'UTF-8',
|
||||
xml_declaration=True, pretty_print=True)
|
||||
|
||||
def extract_xml(self):
|
||||
"""
|
||||
Extract our newly created XML song.
|
||||
"""
|
||||
return etree.tostring(self.song_xml, encoding=u'UTF-8',
|
||||
xml_declaration=True)
|
||||
|
||||
|
||||
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
|
||||
try:
|
||||
self.song_xml = objectify.fromstring(str(xml))
|
||||
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''
|
||||
verse_list.append([element.attrib,
|
||||
unicode(element.text).decode('unicode-escape')])
|
||||
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)
|
@ -22,24 +22,123 @@
|
||||
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
||||
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
||||
###############################################################################
|
||||
"""
|
||||
The :mod:`xml` 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>
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from lxml import etree, objectify
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class SongXMLBuilder(object):
|
||||
"""
|
||||
This class builds the XML used to describe songs.
|
||||
"""
|
||||
log.info(u'SongXMLBuilder Loaded')
|
||||
|
||||
def __init__(self, song_language=None):
|
||||
"""
|
||||
Set up the song builder.
|
||||
|
||||
``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.
|
||||
"""
|
||||
#log.debug(u'add_verse_to_lyrics %s, %s\n%s' % (type, number, content))
|
||||
verse = etree.SubElement(self.lyrics, u'verse', type=type, label=number)
|
||||
verse.text = etree.CDATA(content)
|
||||
|
||||
def dump_xml(self):
|
||||
"""
|
||||
Debugging aid to dump XML so that we can see what we have.
|
||||
"""
|
||||
return etree.tostring(self.song_xml, encoding=u'UTF-8',
|
||||
xml_declaration=True, pretty_print=True)
|
||||
|
||||
def extract_xml(self):
|
||||
"""
|
||||
Extract our newly created XML song.
|
||||
"""
|
||||
return etree.tostring(self.song_xml, encoding=u'UTF-8',
|
||||
xml_declaration=True)
|
||||
|
||||
|
||||
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
|
||||
try:
|
||||
self.song_xml = objectify.fromstring(str(xml))
|
||||
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''
|
||||
verse_list.append([element.attrib,
|
||||
unicode(element.text).decode('unicode-escape')])
|
||||
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)
|
||||
|
||||
from lxml import objectify
|
||||
from lxml.etree import XMLSyntaxError
|
||||
|
||||
class LyricsXML(object):
|
||||
"""
|
||||
This class represents the XML in the ``lyrics`` field of a song.
|
||||
|
||||
The basic XML looks like this::
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<song version="1.0">
|
||||
<lyrics language="en">
|
||||
<verse type="chorus" label="1">
|
||||
<![CDATA[ ... ]]>
|
||||
</verse>
|
||||
</lyrics>
|
||||
</song>
|
||||
"""
|
||||
def __init__(self, song=None):
|
||||
if song:
|
||||
@ -74,7 +173,7 @@ class LyricsXML(object):
|
||||
})
|
||||
self.lyrics.append(language)
|
||||
return True
|
||||
except XMLSyntaxError:
|
||||
except etree.XMLSyntaxError:
|
||||
return False
|
||||
|
||||
def extract(self, text):
|
||||
|
Loading…
Reference in New Issue
Block a user