2009-04-06 18:45:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
"""
|
|
|
|
OpenLP - Open Source Lyrics Projection
|
|
|
|
Copyright (c) 2008 Raoul Snyman
|
|
|
|
Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley, 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
|
2009-03-13 06:13:11 +00:00
|
|
|
from xml.dom.minidom import Document
|
|
|
|
from xml.etree.ElementTree import ElementTree, XML, dump
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<song version="1.0">
|
|
|
|
<lyrics language="en">
|
|
|
|
<verse type="chorus" label="1">
|
|
|
|
<![CDATA[ ... ]]>
|
|
|
|
</verse>
|
|
|
|
</lyrics>
|
|
|
|
</song>
|
|
|
|
|
|
|
|
"""
|
2009-04-06 18:45:45 +00:00
|
|
|
from xml.dom.minidom import Document
|
|
|
|
from xml.etree.ElementTree import ElementTree, XML, dump
|
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
class SongXMLBuilder():
|
|
|
|
def __init__(self):
|
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):
|
|
|
|
# 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):
|
|
|
|
# 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
|
|
|
"""
|
|
|
|
type - type of verse (Chorus, Verse , Bridge, Custom etc
|
|
|
|
number - number of item eg verse 1
|
|
|
|
content - the text to be stored
|
|
|
|
"""
|
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-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
# add data as a CDATA section
|
|
|
|
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):
|
|
|
|
# Debugging aid to see what we have
|
2009-06-16 18:21:24 +00:00
|
|
|
print 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):
|
|
|
|
# Print our newly created XML
|
|
|
|
return self.song_xml.toxml()
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
class SongXMLParser():
|
|
|
|
def __init__(self, xml):
|
|
|
|
self.song_xml = ElementTree(element=XML(xml))
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def get_verses(self):
|
|
|
|
#return a list of verse's and attributes
|
|
|
|
iter=self.song_xml.getiterator()
|
|
|
|
verse_list = []
|
2009-04-06 18:45:45 +00:00
|
|
|
for element in iter:
|
2009-04-30 21:02:28 +00:00
|
|
|
#print element.tag, element.attrib, element.text
|
2009-03-14 07:08:15 +00:00
|
|
|
if element.tag == u'verse':
|
2009-03-13 06:13:11 +00:00
|
|
|
verse_list.append([element.attrib, element.text])
|
|
|
|
return verse_list
|
2009-04-06 18:45:45 +00:00
|
|
|
|
2009-03-13 06:13:11 +00:00
|
|
|
def dump_xml(self):
|
|
|
|
# Debugging aid to see what we have
|
|
|
|
print dump(self.song_xml)
|