forked from openlp/openlp
XML: Start refactoring
This commit is contained in:
parent
32702d48a2
commit
3447c7fed6
@ -84,9 +84,3 @@
|
||||
.. autoclass:: openlp.core.lib.toolbar.OpenLPToolbar
|
||||
:members:
|
||||
|
||||
:mod:`XmlRootClass`
|
||||
-------------------
|
||||
|
||||
.. autoclass:: openlp.core.lib.xmlrootclass.XmlRootClass
|
||||
:members:
|
||||
|
||||
|
@ -216,7 +216,6 @@ from settingsmanager import SettingsManager
|
||||
from plugin import PluginStatus, Plugin
|
||||
from pluginmanager import PluginManager
|
||||
from settingstab import SettingsTab
|
||||
from xmlrootclass import XmlRootClass
|
||||
from serviceitem import ServiceItem
|
||||
from serviceitem import ServiceItemType
|
||||
from serviceitem import ItemCapabilities
|
||||
|
@ -1,104 +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 #
|
||||
###############################################################################
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
from xml.etree.ElementTree import ElementTree, XML
|
||||
|
||||
sys.path.append(os.path.abspath(os.path.join(u'.', u'..', u'..')))
|
||||
|
||||
class XmlRootClass(object):
|
||||
"""
|
||||
Root class for themes, songs etc
|
||||
|
||||
This class provides interface for parsing xml files into object attributes.
|
||||
|
||||
If you overload this class and provide a function called `post_tag_hook`,
|
||||
it will be called thusly for each `tag, value` pair::
|
||||
|
||||
(element.tag, val) = self.post_tag_hook(element.tag, val)
|
||||
"""
|
||||
def _set_from_xml(self, xml, root_tag):
|
||||
"""
|
||||
Set song properties from given xml content.
|
||||
|
||||
``xml``
|
||||
Formatted xml tags and values.
|
||||
``root_tag``
|
||||
The root tag of the xml.
|
||||
"""
|
||||
root = ElementTree(element=XML(xml))
|
||||
xml_iter = root.getiterator()
|
||||
for element in xml_iter:
|
||||
if element.tag != root_tag:
|
||||
text = element.text
|
||||
if text is None:
|
||||
val = text
|
||||
elif isinstance(text, basestring):
|
||||
# Strings need special handling to sort the colours out
|
||||
if text[0] == u'$':
|
||||
# This might be a hex number, let's try to convert it.
|
||||
try:
|
||||
val = int(text[1:], 16)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
# Let's just see if it's a integer.
|
||||
try:
|
||||
val = int(text)
|
||||
except ValueError:
|
||||
# Ok, it seems to be a string.
|
||||
val = text
|
||||
if hasattr(self, u'post_tag_hook'):
|
||||
(element.tag, val) = \
|
||||
self.post_tag_hook(element.tag, val)
|
||||
setattr(self, element.tag, val)
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Return string with all public attributes
|
||||
|
||||
The string is formatted with one attribute per line
|
||||
If the string is split on newline then the length of the
|
||||
list is equal to the number of attributes
|
||||
"""
|
||||
attributes = []
|
||||
for attrib in dir(self):
|
||||
if not attrib.startswith(u'_'):
|
||||
attributes.append(
|
||||
u'%30s : %s' % (attrib, getattr(self, attrib)))
|
||||
return u'\n'.join(attributes)
|
||||
|
||||
def _get_as_string(self):
|
||||
"""
|
||||
Return one string with all public attributes
|
||||
"""
|
||||
result = u''
|
||||
for attrib in dir(self):
|
||||
if not attrib.startswith(u'_'):
|
||||
result += u'_%s_' % getattr(self, attrib)
|
||||
return result
|
||||
|
@ -28,10 +28,12 @@ import sys
|
||||
import os
|
||||
|
||||
from types import ListType
|
||||
from xml.etree.ElementTree import ElementTree, XML
|
||||
|
||||
sys.path.append(os.path.abspath(u'./../../../..'))
|
||||
# Do we need these two lines?
|
||||
#sys.path.append(os.path.abspath(u'./../../../..'))
|
||||
#sys.path.append(os.path.abspath(os.path.join(u'.', u'..', u'..')))
|
||||
|
||||
from openlp.core.lib import XmlRootClass
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@ -74,14 +76,77 @@ _BLANK_OPENSONG_XML = \
|
||||
</song>
|
||||
'''
|
||||
|
||||
class _OpenSong(XmlRootClass):
|
||||
"""Class for import of OpenSong"""
|
||||
|
||||
class _OpenSong(object):
|
||||
"""
|
||||
Class for import of OpenSong
|
||||
"""
|
||||
def __init__(self, xmlContent = None):
|
||||
"""Initialize from given xml content"""
|
||||
super(_OpenSong, self).__init__()
|
||||
"""
|
||||
Initialize from given xml content
|
||||
"""
|
||||
self.from_buffer(xmlContent)
|
||||
|
||||
def _set_from_xml(self, xml, root_tag):
|
||||
"""
|
||||
Set song properties from given xml content.
|
||||
|
||||
``xml``
|
||||
Formatted xml tags and values.
|
||||
``root_tag``
|
||||
The root tag of the xml.
|
||||
"""
|
||||
root = ElementTree(element=XML(xml))
|
||||
xml_iter = root.getiterator()
|
||||
for element in xml_iter:
|
||||
if element.tag != root_tag:
|
||||
text = element.text
|
||||
if text is None:
|
||||
val = text
|
||||
elif isinstance(text, basestring):
|
||||
# Strings need special handling to sort the colours out
|
||||
if text[0] == u'$':
|
||||
# This might be a hex number, let's try to convert it.
|
||||
try:
|
||||
val = int(text[1:], 16)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
# Let's just see if it's a integer.
|
||||
try:
|
||||
val = int(text)
|
||||
except ValueError:
|
||||
# Ok, it seems to be a string.
|
||||
val = text
|
||||
if hasattr(self, u'post_tag_hook'):
|
||||
(element.tag, val) = \
|
||||
self.post_tag_hook(element.tag, val)
|
||||
setattr(self, element.tag, val)
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Return string with all public attributes
|
||||
|
||||
The string is formatted with one attribute per line
|
||||
If the string is split on newline then the length of the
|
||||
list is equal to the number of attributes
|
||||
"""
|
||||
attributes = []
|
||||
for attrib in dir(self):
|
||||
if not attrib.startswith(u'_'):
|
||||
attributes.append(
|
||||
u'%30s : %s' % (attrib, getattr(self, attrib)))
|
||||
return u'\n'.join(attributes)
|
||||
|
||||
def _get_as_string(self):
|
||||
"""
|
||||
Return one string with all public attributes
|
||||
"""
|
||||
result = u''
|
||||
for attrib in dir(self):
|
||||
if not attrib.startswith(u'_'):
|
||||
result += u'_%s_' % getattr(self, attrib)
|
||||
return result
|
||||
|
||||
def _reset(self):
|
||||
"""Reset all song attributes"""
|
||||
self._setFromXml(_BLANK_OPENSONG_XML, 'song')
|
||||
|
Loading…
Reference in New Issue
Block a user