openlp/openlp/plugins/custom/lib/customxmlhandler.py

151 lines
5.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-06 09:10:17 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2010-06-10 21:30:50 +00:00
"""
2010-07-03 01:33:40 +00:00
The :mod:`customxmlhandler` module provides the XML functionality for custom
slides
2010-06-10 21:30:50 +00:00
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
2009-07-10 13:16:15 +00:00
from xml.dom.minidom import Document
2010-12-06 23:59:01 +00:00
from xml.etree.ElementTree import dump
2017-12-28 08:08:12 +00:00
2010-11-29 07:32:08 +00:00
from lxml import etree, objectify
2018-10-02 04:39:42 +00:00
2010-02-27 15:31:23 +00:00
log = logging.getLogger(__name__)
2014-01-01 10:56:23 +00:00
2014-04-12 20:19:22 +00:00
# TODO: These classes need to be refactored into a single class.
2010-07-03 01:33:40 +00:00
class CustomXMLBuilder(object):
2009-07-10 13:16:15 +00:00
"""
This class builds the XML used to describe songs.
"""
2013-08-31 18:17:38 +00:00
log.info('CustomXMLBuilder Loaded')
def __init__(self):
2009-07-10 13:16:15 +00:00
"""
2010-11-29 14:04:37 +00:00
Set up the custom builder.
2009-07-10 13:16:15 +00:00
"""
# Create the minidom document
2010-07-04 13:42:01 +00:00
self.custom_xml = Document()
self.new_document()
self.add_lyrics_to_song()
def new_document(self):
2009-07-10 13:16:15 +00:00
"""
2010-11-29 14:04:37 +00:00
Create a new custom XML document.
2009-07-10 13:16:15 +00:00
"""
# Create the <song> base element
2013-08-31 18:17:38 +00:00
self.song = self.custom_xml.createElement('song')
2010-07-04 13:42:01 +00:00
self.custom_xml.appendChild(self.song)
2013-08-31 18:17:38 +00:00
self.song.setAttribute('version', '1.0')
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
2010-11-29 14:04:37 +00:00
custom item.
2009-07-10 13:16:15 +00:00
"""
# Create the main <lyrics> element
2013-08-31 18:17:38 +00:00
self.lyrics = self.custom_xml.createElement('lyrics')
self.lyrics.setAttribute('language', 'en')
self.song.appendChild(self.lyrics)
def add_verse_to_lyrics(self, verse_type, number, content):
"""
2009-07-10 13:16:15 +00:00
Add a verse to the ``<lyrics>`` tag.
2014-01-01 10:56:23 +00:00
:param verse_type: A string denoting the type of verse. Possible values are "Chorus", "Verse", "Bridge",
2015-09-08 19:13:59 +00:00
and "Custom".
2014-01-01 10:56:23 +00:00
:param number: An integer denoting the number of the item, for example: verse 1.
:param content: The actual text of the verse to be stored.
2009-07-10 13:16:15 +00:00
"""
2013-08-31 18:17:38 +00:00
verse = self.custom_xml.createElement('verse')
verse.setAttribute('type', verse_type)
verse.setAttribute('label', number)
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
2010-07-04 13:42:01 +00:00
cds = self.custom_xml.createCDATASection(content)
verse.appendChild(cds)
2010-11-29 14:04:37 +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.
"""
2013-08-31 18:17:38 +00:00
return self.custom_xml.toprettyxml(indent=' ')
def extract_xml(self):
2009-07-10 13:16:15 +00:00
"""
2010-11-29 14:04:37 +00:00
Extract our newly created XML custom.
2009-07-10 13:16:15 +00:00
"""
2013-08-31 18:17:38 +00:00
return self.custom_xml.toxml('utf-8')
2010-07-03 01:33:40 +00:00
class CustomXMLParser(object):
2009-07-10 13:16:15 +00:00
"""
2010-11-29 14:04:37 +00:00
A class to read in and parse a custom's XML.
2009-07-10 13:16:15 +00:00
"""
2013-08-31 18:17:38 +00:00
log.info('CustomXMLParser Loaded')
def __init__(self, xml):
2009-07-10 13:16:15 +00:00
"""
2010-11-29 14:04:37 +00:00
Set up our custom XML parser.
2009-07-10 13:16:15 +00:00
2014-03-17 19:05:55 +00:00
:param xml: The XML of the custom to be parsed.
2009-07-10 13:16:15 +00:00
"""
2010-07-04 13:42:01 +00:00
self.custom_xml = None
2013-08-31 18:17:38 +00:00
if xml[:5] == '<?xml':
2010-11-29 07:32:08 +00:00
xml = xml[38:]
try:
2010-11-29 07:32:08 +00:00
self.custom_xml = objectify.fromstring(xml)
except etree.XMLSyntaxError:
2016-05-21 18:19:18 +00:00
log.exception('Invalid xml {xml}'.format(xml=xml))
def get_verses(self):
2009-07-10 13:16:15 +00:00
"""
2014-03-17 19:05:55 +00:00
Iterates through the verses in the XML and returns a list of verses and their attributes.
2009-07-10 13:16:15 +00:00
"""
2010-07-04 13:42:01 +00:00
xml_iter = self.custom_xml.getiterator()
verse_list = []
2010-05-27 14:41:47 +00:00
for element in xml_iter:
2013-08-31 18:17:38 +00:00
if element.tag == 'verse':
if element.text is None:
2013-08-31 18:17:38 +00:00
element.text = ''
verse_list.append([element.attrib, str(element.text)])
return verse_list
2010-11-29 14:04:37 +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.
"""
return dump(self.custom_xml)