def addChannel(self, channel): """ add a single XMLTV 'channel' Arguments: 'channel' -- A dict representing XMLTV data """ if DEBUG >= 0: print "in addChannel(self, channel)" c = SubElement(self.root, 'channel') self.setattr(c, 'id', channel['id']) # Display Name for display_name in channel['display-name']: dn = SubElement(c, 'display-name') self.settext(dn, display_name) # Icon if channel.has_key('icon'): self.seticons(c, channel['icon']) # URL if channel.has_key('url'): for url in channel['url']: u = SubElement(c, 'url') self.settext(u, url, with_lang=False)
def generateWay (self, attr): featureMember = Element("gml:featureMember") feature = SubElement(featureMember, "way") FID = SubElement(feature, "FID") FID.text = str(attr["id"]) geometryProperty = SubElement(feature, "gml:geometryProperty") lineString = SubElement(geometryProperty, "gml:LineString") coordinates = SubElement(lineString, "gml:coordinates") coordinates.text = " ".join(map(lambda x: "%s,%s" % x, attr['segments'])) for k, v in attr.iteritems(): if k != "segments": SubElement(feature, "" + k).text = v ElementTree(featureMember).write(self.fh, "utf-8") self.fh.write("\n")
def generateWay(self, attr): featureMember = Element("gml:featureMember") feature = SubElement(featureMember, "way") FID = SubElement(feature, "FID") FID.text = str(attr["id"]) geometryProperty = SubElement(feature, "gml:geometryProperty") lineString = SubElement(geometryProperty, "gml:LineString") coordinates = SubElement(lineString, "gml:coordinates") coordinates.text = " ".join( map(lambda x: "%s,%s" % x, attr['segments'])) for k, v in attr.iteritems(): if k != "segments": SubElement(feature, "" + k).text = v ElementTree(featureMember).write(self.fh, "utf-8") self.fh.write("\n")
def test_writer(self): a = Element('a') b = SubElement(a, 'b') b.append(Comment('a comment')) c = SubElement(b, 'c', d = 'e') f = SubElement(c, 'f') f.text = 'g' h = SubElement(c, 'h') h.text = 'i << >> << &&' b.append(ProcessingInstruction('pdk', 'processing instruction')) tree = ElementTree(a) output = stringio() write_pretty_xml(tree, output) self.assert_equals_long(expected_xml_output, output.getvalue())
def set_zero_orone(self, programme, element, p): """ set_zero_ormore(programme, element, p) -> None Add nodes under p for the element 'element', which occurs zero times or once with PCDATA and a 'lang' attribute """ if DEBUG >= 0: print "in set_zero_orone(self, programme, element, p)" if programme.has_key(element): e = SubElement(p, element) self.settext(e, programme[element])
def seticons(self, node, icons): """ seticon(node, icons) -> None Create 'icons' under 'node' """ if DEBUG >= 0: print "in seticons(self, node, icons)" for icon in icons: if not icon.has_key('src'): raise ValueError("'icon' element requires 'src' attribute") i = SubElement(node, 'icon') for attr in ('src', 'width', 'height'): if icon.has_key(attr): self.setattr(i, attr, icon[attr])
def addProgramme(self, programme): """ Add a single XMLTV 'programme' Arguments: 'programme' -- A dict representing XMLTV data """ if DEBUG >= 0: print "in addProgramme(self, programme)" p = SubElement(self.root, 'programme') # programme attributes for attr in ('start', 'channel'): if programme.has_key(attr): self.setattr(p, attr, programme[attr]) else: raise ValueError("'programme' must contain '%s' attribute" % attr) for attr in ('stop', 'pdc-start', 'vps-start', 'showview', 'videoplus', 'clumpidx'): if programme.has_key(attr): self.setattr(p, attr, programme[attr]) for title in programme['title']: t = SubElement(p, 'title') self.settext(t, title) # Sub-title and description for element in ('sub-title', 'desc'): self.set_zero_ormore(programme, element, p) # Credits if programme.has_key('credits'): c = SubElement(p, 'credits') for credtype in ('director', 'actor', 'writer', 'adapter', 'producer', 'presenter', 'commentator', 'guest'): if programme['credits'].has_key(credtype): for name in programme['credits'][credtype]: cred = SubElement(c, credtype) self.settext(cred, name, with_lang=False) # Date if programme.has_key('date'): d = SubElement(p, 'date') self.settext(d, programme['date'], with_lang=False) # Category self.set_zero_ormore(programme, 'category', p) # Language and original language for element in ('language', 'orig-language'): self.set_zero_orone(programme, element, p) # Length if programme.has_key('length'): l = SubElement(p, 'length') self.setattr(l, 'units', programme['length']['units']) self.settext(l, programme['length']['length'], with_lang=False) # Icon if programme.has_key('icon'): self.seticons(p, programme['icon']) # URL if programme.has_key('url'): for url in programme['url']: u = SubElement(p, 'url') self.settext(u, url, with_lang=False) # Country self.set_zero_ormore(programme, 'country', p) # Episode-num if programme.has_key('episode-num'): for epnum in programme['episode-num']: e = SubElement(p, 'episode-num') self.setattr(e, 'system', epnum[1]) self.settext(e, epnum[0], with_lang=False) # Video details if programme.has_key('video'): e = SubElement(p, 'video') for videlem in ('aspect', 'quality'): if programme['video'].has_key(videlem): v = SubElement(e, videlem) self.settext(v, programme['video'][videlem], with_lang=False) for attr in ('present', 'colour'): if programme['video'].has_key(attr): a = SubElement(e, attr) if programme['video'][attr]: self.settext(a, 'yes', with_lang=False) else: self.settext(a, 'no', with_lang=False) # Audio details if programme.has_key('audio'): a = SubElement(p, 'audio') if programme['audio'].has_key('stereo'): s = SubElement(a, 'stereo') self.settext(s, programme['audio']['stereo'], with_lang=False) if programme['audio'].has_key('present'): p = SubElement(a, 'present') if programme['audio']['present']: self.settext(p, 'yes', with_lang=False) else: self.settext(p, 'no', with_lang=False) # Previously shown if programme.has_key('previously-shown'): ps = SubElement(p, 'previously-shown') for attr in ('start', 'channel'): if programme['previously-shown'].has_key(attr): self.setattr(ps, attr, programme['previously-shown'][attr]) # Premiere / last chance for element in ('premiere', 'last-chance'): self.set_zero_orone(programme, element, p) # New if programme.has_key('new'): n = SubElement(p, 'new') # Subtitles if programme.has_key('subtitles'): for subtitles in programme['subtitles']: s = SubElement(p, 'subtitles') if subtitles.has_key('type'): self.setattr(s, 'type', subtitles['type']) if subtitles.has_key('language'): l = SubElement(s, 'language') self.settext(l, subtitles['language']) # Rating if programme.has_key('rating'): for rating in programme['rating']: r = SubElement(p, 'rating') if rating.has_key('system'): self.setattr(r, 'system', rating['system']) v = SubElement(r, 'value') self.settext(v, rating['value'], with_lang=False) if rating.has_key('icon'): self.seticons(r, rating['icon']) # Star rating if programme.has_key('star-rating'): sr = SubElement(p, 'star-rating') v = SubElement(sr, 'value') self.settext(v, programme['star-rating']['value'], with_lang=False) if programme['star-rating'].has_key('icon'): self.seticons(sr, programme['star-rating']['icon'])