~raoul-snyman/openlp/python3

« back to all changes in this revision

Viewing changes to openlp/plugins/custom/lib/customxmlhandler.py

  • Committer: Raoul Snyman
  • Date: 2013-04-03 06:51:39 UTC
  • Revision ID: raoul.snyman@saturnlaboratories.co.za-20130403065139-9qhs0xmlrcef4n2h
trying to migrate to py3k

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
    """
56
56
    This class builds the XML used to describe songs.
57
57
    """
58
 
    log.info(u'CustomXMLBuilder Loaded')
 
58
    log.info('CustomXMLBuilder Loaded')
59
59
 
60
60
    def __init__(self):
61
61
        """
71
71
        Create a new custom XML document.
72
72
        """
73
73
        # Create the <song> base element
74
 
        self.song = self.custom_xml.createElement(u'song')
 
74
        self.song = self.custom_xml.createElement('song')
75
75
        self.custom_xml.appendChild(self.song)
76
 
        self.song.setAttribute(u'version', u'1.0')
 
76
        self.song.setAttribute('version', '1.0')
77
77
 
78
78
    def add_lyrics_to_song(self):
79
79
        """
81
81
        custom item.
82
82
        """
83
83
        # Create the main <lyrics> element
84
 
        self.lyrics = self.custom_xml.createElement(u'lyrics')
85
 
        self.lyrics.setAttribute(u'language', u'en')
 
84
        self.lyrics = self.custom_xml.createElement('lyrics')
 
85
        self.lyrics.setAttribute('language', 'en')
86
86
        self.song.appendChild(self.lyrics)
87
87
 
88
88
    def add_verse_to_lyrics(self, verse_type, number, content):
99
99
        ``content``
100
100
            The actual text of the verse to be stored.
101
101
        """
102
 
        verse = self.custom_xml.createElement(u'verse')
103
 
        verse.setAttribute(u'type', verse_type)
104
 
        verse.setAttribute(u'label', number)
 
102
        verse = self.custom_xml.createElement('verse')
 
103
        verse.setAttribute('type', verse_type)
 
104
        verse.setAttribute('label', number)
105
105
        self.lyrics.appendChild(verse)
106
106
        # add data as a CDATA section to protect the XML from special chars
107
107
        cds = self.custom_xml.createCDATASection(content)
111
111
        """
112
112
        Debugging aid to dump XML so that we can see what we have.
113
113
        """
114
 
        return self.custom_xml.toprettyxml(indent=u'  ')
 
114
        return self.custom_xml.toprettyxml(indent='  ')
115
115
 
116
116
    def extract_xml(self):
117
117
        """
118
118
        Extract our newly created XML custom.
119
119
        """
120
 
        return self.custom_xml.toxml(u'utf-8')
 
120
        return self.custom_xml.toxml('utf-8')
121
121
 
122
122
 
123
123
class CustomXMLParser(object):
124
124
    """
125
125
    A class to read in and parse a custom's XML.
126
126
    """
127
 
    log.info(u'CustomXMLParser Loaded')
 
127
    log.info('CustomXMLParser Loaded')
128
128
 
129
129
    def __init__(self, xml):
130
130
        """
134
134
            The XML of the custom to be parsed.
135
135
        """
136
136
        self.custom_xml = None
137
 
        if xml[:5] == u'<?xml':
 
137
        if xml[:5] == '<?xml':
138
138
            xml = xml[38:]
139
139
        try:
140
140
            self.custom_xml = objectify.fromstring(xml)
141
141
        except etree.XMLSyntaxError:
142
 
            log.exception(u'Invalid xml %s', xml)
 
142
            log.exception('Invalid xml %s', xml)
143
143
 
144
144
    def get_verses(self):
145
145
        """
149
149
        xml_iter = self.custom_xml.getiterator()
150
150
        verse_list = []
151
151
        for element in xml_iter:
152
 
            if element.tag == u'verse':
 
152
            if element.tag == 'verse':
153
153
                if element.text is None:
154
 
                    element.text = u''
155
 
                verse_list.append([element.attrib, unicode(element.text)])
 
154
                    element.text = ''
 
155
                verse_list.append([element.attrib, str(element.text)])
156
156
        return verse_list
157
157
 
158
158
    def _dump_xml(self):