XML: Put it where it should be

This commit is contained in:
Jon Tibble 2010-07-03 02:33:40 +01:00
parent 0c184d5e60
commit 497d1c9dc9
11 changed files with 174 additions and 27 deletions

View File

@ -60,18 +60,6 @@
.. autoclass:: openlp.core.lib.settingstab.SettingsTab .. autoclass:: openlp.core.lib.settingstab.SettingsTab
:members: :members:
:mod:`SongXMLBuilder`
---------------------
.. autoclass:: openlp.core.lib.songxmlhandler.SongXMLBuilder
:members:
:mod:`SongXMLParser`
--------------------
.. autoclass:: openlp.core.lib.songxmlhandler.SongXMLParser
:members:
:mod:`ThemeXML` :mod:`ThemeXML`
--------------- ---------------
@ -83,4 +71,3 @@
.. autoclass:: openlp.core.lib.toolbar.OpenLPToolbar .. autoclass:: openlp.core.lib.toolbar.OpenLPToolbar
:members: :members:

View File

@ -221,7 +221,6 @@ from serviceitem import ServiceItemType
from serviceitem import ItemCapabilities from serviceitem import ItemCapabilities
from toolbar import OpenLPToolbar from toolbar import OpenLPToolbar
from dockwidget import OpenLPDockWidget from dockwidget import OpenLPDockWidget
from songxmlhandler import SongXMLBuilder, SongXMLParser
from themexmlhandler import ThemeXML from themexmlhandler import ThemeXML
from renderer import Renderer from renderer import Renderer
from rendermanager import RenderManager from rendermanager import RenderManager

View File

@ -28,7 +28,8 @@ import logging
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from editcustomdialog import Ui_customEditDialog from editcustomdialog import Ui_customEditDialog
from openlp.core.lib import SongXMLBuilder, SongXMLParser, Receiver, translate from openlp.core.lib import Receiver, translate
from openlp.plugins.custom.lib import CustomXMLBuilder, CustomXMLParser
from openlp.plugins.custom.lib.db import CustomSlide from openlp.plugins.custom.lib.db import CustomSlide
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -119,8 +120,8 @@ class EditCustomForm(QtGui.QDialog, Ui_customEditDialog):
self.customSlide = self.custommanager.get_object(CustomSlide, id) self.customSlide = self.custommanager.get_object(CustomSlide, id)
self.TitleEdit.setText(self.customSlide.title) self.TitleEdit.setText(self.customSlide.title)
self.CreditEdit.setText(self.customSlide.credits) self.CreditEdit.setText(self.customSlide.credits)
songXML = SongXMLParser(self.customSlide.text) customXML = CustomXMLParser(self.customSlide.text)
verseList = songXML.get_verses() verseList = customXML.get_verses()
for verse in verseList: for verse in verseList:
self.VerseListView.addItem(verse[1]) self.VerseListView.addItem(verse[1])
theme = self.customSlide.theme_name theme = self.customSlide.theme_name
@ -152,7 +153,7 @@ class EditCustomForm(QtGui.QDialog, Ui_customEditDialog):
translate('CustomPlugin.EditCustomForm', 'Error'), message, translate('CustomPlugin.EditCustomForm', 'Error'), message,
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok)) QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok))
return False return False
sxml = SongXMLBuilder() sxml = CustomXMLBuilder()
sxml.new_document() sxml.new_document()
sxml.add_lyrics_to_song() sxml.add_lyrics_to_song()
count = 1 count = 1

View File

@ -23,5 +23,6 @@
# Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Temple Place, Suite 330, Boston, MA 02111-1307 USA #
############################################################################### ###############################################################################
from customxmlhandler import CustomXMLBuilder, CustomXMLParser
from mediaitem import CustomMediaItem from mediaitem import CustomMediaItem
from customtab import CustomTab from customtab import CustomTab

View File

@ -0,0 +1,156 @@
# -*- 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:`customxmlhandler` module provides the XML functionality for custom
slides
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 xml.dom.minidom import Document
from xml.etree.ElementTree import ElementTree, XML, dump
from xml.parsers.expat import ExpatError
log = logging.getLogger(__name__)
class CustomXMLBuilder(object):
"""
This class builds the XML used to describe songs.
"""
log.info(u'CustomXMLBuilder Loaded')
def __init__(self):
"""
Set up the song builder.
"""
# Create the minidom document
self.song_xml = Document()
def new_document(self):
"""
Create a new song XML document.
"""
# Create the <song> base element
self.song = self.song_xml.createElement(u'song')
self.song_xml.appendChild(self.song)
self.song.setAttribute(u'version', u'1.0')
def add_lyrics_to_song(self):
"""
Set up and add a ``<lyrics>`` tag which contains the lyrics of the
song.
"""
# Create the main <lyrics> element
self.lyrics = self.song_xml.createElement(u'lyrics')
self.lyrics.setAttribute(u'language', u'en')
self.song.appendChild(self.lyrics)
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 = self.song_xml.createElement(u'verse')
verse.setAttribute(u'type', type)
verse.setAttribute(u'label', number)
self.lyrics.appendChild(verse)
# add data as a CDATA section to protect the XML from special chars
cds = self.song_xml.createCDATASection(content)
verse.appendChild(cds)
def dump_xml(self):
"""
Debugging aid to dump XML so that we can see what we have.
"""
return self.song_xml.toprettyxml(indent=u' ')
def extract_xml(self):
"""
Extract our newly created XML song.
"""
return self.song_xml.toxml(u'utf-8')
class CustomXMLParser(object):
"""
A class to read in and parse a song's XML.
"""
log.info(u'CustomXMLParser 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 = ElementTree(
element=XML(unicode(xml).encode('unicode-escape')))
except ExpatError:
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 dump(self.song_xml)

View File

@ -27,8 +27,9 @@ import logging
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, SongXMLParser, BaseListWithDnD, \ from openlp.core.lib import MediaManagerItem, BaseListWithDnD, \
Receiver, ItemCapabilities, translate, check_item_selected Receiver, ItemCapabilities, translate, check_item_selected
from openlp.plugins.custom.lib import CustomXMLParser
from openlp.plugins.custom.lib.db import CustomSlide from openlp.plugins.custom.lib.db import CustomSlide
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -170,8 +171,8 @@ class CustomMediaItem(MediaManagerItem):
theme = customSlide.theme_name theme = customSlide.theme_name
if theme: if theme:
service_item.theme = theme service_item.theme = theme
songXML = SongXMLParser(customSlide.text) customXML = CustomXMLParser(customSlide.text)
verseList = songXML.get_verses() verseList = customXML.get_verses()
for verse in verseList: for verse in verseList:
raw_slides.append(verse[1]) raw_slides.append(verse[1])
service_item.title = title service_item.title = title

View File

@ -28,9 +28,9 @@ import re
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import SongXMLBuilder, SongXMLParser, Receiver, translate from openlp.core.lib import Receiver, translate
from openlp.plugins.songs.forms import EditVerseForm from openlp.plugins.songs.forms import EditVerseForm
from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib import SongXMLBuilder, SongXMLParser, VerseType
from openlp.plugins.songs.lib.db import Book, Song, Author, Topic from openlp.plugins.songs.lib.db import Book, Song, Author, Topic
from editsongdialog import Ui_EditSongDialog from editsongdialog import Ui_EditSongDialog

View File

@ -137,6 +137,7 @@ class VerseType(object):
unicode(VerseType.to_string(VerseType.Other)).lower(): unicode(VerseType.to_string(VerseType.Other)).lower():
return VerseType.Other return VerseType.Other
from songxmlhandler import SongXMLBuilder, SongXMLParser
from songstab import SongsTab from songstab import SongsTab
from mediaitem import SongMediaItem from mediaitem import SongMediaItem
from songimport import SongImport from songimport import SongImport

View File

@ -27,10 +27,11 @@ import logging
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, SongXMLParser, \ from openlp.core.lib import MediaManagerItem, BaseListWithDnD, Receiver, \
BaseListWithDnD, Receiver, ItemCapabilities, translate, check_item_selected ItemCapabilities, translate, check_item_selected
from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \ from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \
ImportWizardForm ImportWizardForm
from openlp.plugins.songs.lib import SongXMLParser
from openlp.plugins.songs.lib.db import Song from openlp.plugins.songs.lib.db import Song
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@ -25,8 +25,8 @@
import re import re
from openlp.core.lib import SongXMLBuilder, translate from openlp.core.lib import translate
from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib import SongXMLBuilder, VerseType
from openlp.plugins.songs.lib.db import Song, Author, Topic, Book from openlp.plugins.songs.lib.db import Song, Author, Topic, Book
class SongImport(object): class SongImport(object):