forked from openlp/openlp
Removed opensong code from songxml
This commit is contained in:
parent
b3559a993d
commit
860d3e2923
@ -60,175 +60,6 @@ class SongFeatureError(SongException):
|
||||
# TODO: Song: Import ChangingSong
|
||||
# TODO: Song: Export ChangingSong
|
||||
|
||||
_BLANK_OPENSONG_XML = \
|
||||
'''<?xml version="1.0" encoding="UTF-8"?>
|
||||
<song>
|
||||
<title></title>
|
||||
<author></author>
|
||||
<copyright></copyright>
|
||||
<presentation></presentation>
|
||||
<ccli></ccli>
|
||||
<lyrics></lyrics>
|
||||
<theme></theme>
|
||||
<alttheme></alttheme>
|
||||
</song>
|
||||
'''
|
||||
|
||||
class _OpenSong(object):
|
||||
"""
|
||||
Class for import of OpenSong
|
||||
"""
|
||||
def __init__(self, xmlContent = None):
|
||||
"""
|
||||
Initialize from given xml content
|
||||
"""
|
||||
self._set_from_xml(_BLANK_OPENSONG_XML, 'song')
|
||||
if xmlContent:
|
||||
self._set_from_xml(xmlContent, 'song')
|
||||
|
||||
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 get_author_list(self):
|
||||
"""Convert author field to an authorlist
|
||||
|
||||
in OpenSong an author list may be separated by '/'
|
||||
return as a string
|
||||
"""
|
||||
if self.author:
|
||||
list = self.author.split(u' and ')
|
||||
res = [item.strip() for item in list]
|
||||
return u', '.join(res)
|
||||
|
||||
def get_category_array(self):
|
||||
"""Convert theme and alttheme into category_array
|
||||
|
||||
return as a string
|
||||
"""
|
||||
res = []
|
||||
if self.theme:
|
||||
res.append(self.theme)
|
||||
if self.alttheme:
|
||||
res.append(self.alttheme)
|
||||
return u', u'.join(res)
|
||||
|
||||
def _reorder_verse(self, tag, tmpVerse):
|
||||
"""
|
||||
Reorder the verse in case of first char is a number
|
||||
tag -- the tag of this verse / verse group
|
||||
tmpVerse -- list of strings
|
||||
"""
|
||||
res = []
|
||||
for digit in '1234567890 ':
|
||||
tagPending = True
|
||||
for line in tmpVerse:
|
||||
if line.startswith(digit):
|
||||
if tagPending:
|
||||
tagPending = False
|
||||
tagChar = tag.strip(u'[]').lower()
|
||||
if 'v' == tagChar:
|
||||
newtag = "Verse"
|
||||
elif 'c' == tagChar:
|
||||
newtag = "Chorus"
|
||||
elif 'b' == tagChar:
|
||||
newtag = "Bridge"
|
||||
elif 'p' == tagChar:
|
||||
newtag = "Pre-chorus"
|
||||
else:
|
||||
newtag = tagChar
|
||||
tagString = (u'# %s %s' % (newtag, digit)).rstrip()
|
||||
res.append(tagString)
|
||||
res.append(line[1:])
|
||||
if (len(line) == 0) and (not tagPending):
|
||||
res.append(line)
|
||||
return res
|
||||
|
||||
def get_lyrics(self):
|
||||
"""
|
||||
Convert the lyrics to openlp lyrics format
|
||||
return as list of strings
|
||||
"""
|
||||
lyrics = self.lyrics.split(u'\n')
|
||||
tmpVerse = []
|
||||
finalLyrics = []
|
||||
tag = ""
|
||||
for lyric in lyrics:
|
||||
line = lyric.rstrip()
|
||||
if not line.startswith(u'.'):
|
||||
# drop all chords
|
||||
tmpVerse.append(line)
|
||||
if line:
|
||||
if line.startswith(u'['):
|
||||
tag = line
|
||||
else:
|
||||
reorderedVerse = self._reorder_verse(tag, tmpVerse)
|
||||
finalLyrics.extend(reorderedVerse)
|
||||
tag = ""
|
||||
tmpVerse = []
|
||||
# catch up final verse
|
||||
reorderedVerse = self._reorder_verse(tag, tmpVerse)
|
||||
finalLyrics.extend(reorderedVerse)
|
||||
return finalLyrics
|
||||
|
||||
|
||||
class Song(object):
|
||||
"""Handling song properties and methods
|
||||
|
||||
@ -307,40 +138,6 @@ class Song(object):
|
||||
self.set_lyrics(u'')
|
||||
return
|
||||
|
||||
def from_opensong_buffer(self, xmlcontent):
|
||||
"""Initialize from buffer(string) of xml lines in opensong format"""
|
||||
self._reset()
|
||||
opensong = _OpenSong(xmlcontent)
|
||||
if opensong.title:
|
||||
self.set_title(opensong.title)
|
||||
if opensong.copyright:
|
||||
self.set_copyright(opensong.copyright)
|
||||
if opensong.presentation:
|
||||
self.set_verse_order(opensong.presentation)
|
||||
if opensong.ccli:
|
||||
self.set_song_cclino(opensong.ccli)
|
||||
self.set_author_list(opensong.get_author_list())
|
||||
self.set_category_array(opensong.get_category_array())
|
||||
self.set_lyrics(opensong.get_lyrics())
|
||||
|
||||
def from_opensong_file(self, xmlfilename):
|
||||
"""
|
||||
Initialize from file containing xml
|
||||
xmlfilename -- path to xml file
|
||||
"""
|
||||
osfile = None
|
||||
try:
|
||||
osfile = open(xmlfilename, 'r')
|
||||
list = [line for line in osfile]
|
||||
osfile.close()
|
||||
xml = "".join(list)
|
||||
self.from_opensong_buffer(xml)
|
||||
except IOError:
|
||||
log.exception(u'Failed to load opensong xml file')
|
||||
finally:
|
||||
if osfile:
|
||||
osfile.close()
|
||||
|
||||
def _remove_punctuation(self, title):
|
||||
"""Remove the puntuation chars from title
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user