def save(self): # Generate List in RAM root = None services = self.services controllers = self.controllers # Build Header from plugin import NAME, VERSION root = Element(NAME) root.set('version', VERSION) root.append(Comment(_("Don't edit this manually unless you really know what you are doing"))) # Build Body def build(root, instances, typ): for instance in instances: # Add module element = SubElement( root, typ, name = stringToXML(instance.getName()), enable = stringToXML(instance.getStringEnable()) ) # Add options options = instance.getStringOptions() if options: for key, value, description in options: SubElement( element, OPTION, key = stringToXML(key) ).text = stringToXML(value) return root if services: root = build( root, services, SERVICE) if controllers: root = build( root, controllers, CONTROLLER) self.writeXML( root )
def makerss(titletext, top, root, files): print 'Building %s' % os.path.join(root, 'photos.rss') dirlen = len(top) + len(os.path.sep) rootpath = root[dirlen:] rootlen = len(rootpath) > 0 and len(rootpath) + len(os.path.sep) or 0 rss = Element('rss', version='2.0') rss.attrib['xmlns:atom'] = NS_ATOM rss.attrib['xmlns:media'] = NS_MEDIA channel = SubElement(rss, 'channel') title = SubElement(channel, 'title') title.text = titletext description = SubElement(channel, 'description') description.text = _( 'This feed enables cooliris http://www.cooliris.com/ in your web browser' ) channel.append(Comment('must have a link for rss')) link = SubElement(channel, 'link') link.text = 'http://localhost/' result = [] for image in files: imagepath = util.www_image_path(image, '.images') if not options.noimages: util.www_image_create(os.path.join(top, image), force=options.force, verbose=options.verbose) thumbpath = util.www_image_path(image, '.thumbs') if not options.nothumbs: util.www_thumb_create(os.path.join(top, image), force=options.force, verbose=options.verbose) if options.verbose >= 3: print 'rootpath:', rootpath, 'image:', image, 'imagepath:', imagepath, 'thumbpath:', thumbpath print 'imagepath[rootlen:]:', imagepath[ rootlen:], 'thumbpath[rootlen:]:', thumbpath[rootlen:] result.append(os.path.join(rootpath, image)) image = image[rootlen:] item = SubElement(channel, 'item') title = SubElement(item, 'title') title.text = image link = SubElement(item, 'link') link.text = urllib.quote(imagepath) description = SubElement(item, 'media:description') description.text = convert_entities( os.path.splitext(os.path.basename(image))[0]) # need to double quote special characters thumbnail = SubElement(item, 'media:thumbnail', url=urllib.quote( urllib.quote(thumbpath[rootlen:]))) content = SubElement(item, 'media:content', url=urllib.quote(urllib.quote( imagepath[rootlen:]))) indent(rss) file = open(os.path.join(root, 'photos.rss'), 'w') print >> file, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' print >> file, tostring(rss) file.close() return result
def adapt(src_tree, return_root=True, **kw): src_root = src_tree.getroot() dest_root = convert_elem(src_root) stack = [(src_root, dest_root)] while stack: src, dest = stack.pop() for src_child in src.iterchildren(): if isinstance(src_child, _Comment): dest_child = Comment(src_child.text) dest_child.tail = src_child.tail dest.append(dest_child) else: dest_child = convert_elem(src_child, dest) dest_child.text, dest_child.tail = src_child.text, src_child.tail stack.append((src_child, dest_child)) return dest_root if return_root else ElementTree(dest_root)
def writeXMLTVConfig(self): if self.epgimport is None and self.xmltvimport is None and self.crossepg is None: return if int(self.epgimportversion[0]) >= 5 and int( self.xmltvimportversion[0]) >= 5 and int( self.crossepgversion[0]) >= 5: return if config.plugins.seriesplugin.epgimport.value == False and config.plugins.seriesplugin.xmltvimport.value == False and config.plugins.seriesplugin.crossepg.value == False: return # Build Header from plugin import NAME, VERSION root = Element("sources") root.set('version', VERSION) root.set('created_by', NAME) root.append( Comment( _("Don't edit this manually unless you really know what you are doing" ))) element = SubElement(root, "source", type="gen_xmltv", channels="wunschliste.channels.xml") SubElement(element, "description").text = "Wunschliste XMLTV" SubElement(element, "url").text = config.plugins.seriesplugin.xmltv_url.value etree = ElementTree(root) indent(etree.getroot()) if config.plugins.seriesplugin.epgimport.value: log.debug("Write: xml channels for epgimport") if self.epgimport: try: self.epgimport.writeXML(etree) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.xmltvimport.value: log.debug("Write: xml channels for xmltvimport") if self.xmltvimport: try: self.xmltvimport.writeXML(etree) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.crossepg.value: log.debug("Write: xml channels for crossepg") if self.crossepg: try: self.crossepg.writeXML(etree) except Exception as e: log.exception("Exception in write XML: " + str(e))
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 saveXML(self): try: if ChannelsBase.channels_changed: ChannelsBase.channels_changed = False channels = ChannelsBase.channels # Generate List in RAM root = None #logDebug("saveXML channels", channels) logDebug("SP saveXML channels", len(channels)) # Build Header from plugin import NAME, VERSION root = Element(NAME) root.set('version', VERSION) root.append( Comment( _("Don't edit this manually unless you really know what you are doing" ))) # Build Body def build(root, channels): if channels: for reference, namealternatives in channels.iteritems( ): name, alternatives = namealternatives # Add channel element = SubElement( root, "Channel", name=stringToXML(name), reference=stringToXML(reference)) # Add alternatives if alternatives: for name in alternatives: SubElement( element, "Alternative").text = stringToXML(name) return root root = build(root, channels) self.writeXML(root) except Exception as e: logDebug("Exception in writeXML: " + str(e))
def outxml(self): """ Returns XML with all the items found """ self.resetposition() result = self.readnextitem() xmlroot = Element("PMRdata") xmlroot.set('version', __version__) xmlroot.set('generator', 'MDVx:PMRReader') xmlroot.append(Comment("Generated with MDVx by DJFio[DB] http://djfio.ru/mdv/")) while result[0] != None: item = Element('item') item.set('matUUID', unicode(result[0])) item.set('name', unicode(result[1].decode(__module_encodind__))) item.set('project', unicode(result[2].decode(__module_encodind__))) item.set('srcUUID', unicode(result[3])) item.set('date', unicode(result[4])) xmlroot.append(item) result = self.readnextitem() return xmlroot
def saveXML(self): try: if ChannelsBase.channels_changed: ChannelsBase.channels_changed = False channels = ChannelsBase.channels # Generate List in RAM etree = None #log.debug("saveXML channels", channels) log.debug("SP saveXML channels", len(channels)) # XMLTV compatible channels file #TEST Do we need to write the xml header node # Build Header from plugin import NAME, VERSION root = Element("channels") root.set('version', VERSION) root.set('created_by', NAME) root.append(Comment(_("Don't edit this manually unless you really know what you are doing"))) # Build Body def build(root, channels): if channels: for reference, namealternatives in channels.iteritems(): name, alternatives = namealternatives[:] if alternatives: # Add channel web = alternatives[0] element = SubElement( root, "channel", name = stringToXML(name), id = stringToXML(web) ) element.text = stringToXML(reference) del alternatives[0] if alternatives: for web in alternatives: SubElement( element, "web" ).text = stringToXML(web) return root etree = ElementTree( build( root, channels ) ) indent(etree.getroot()) self.writeXML( etree ) if config.plugins.seriesplugin.epgimport.value: log.debug("Write: xml channels for epgimport") try: path = "/etc/epgimport/wunschliste.channels.xml" etree.write(path, encoding='utf-8', xml_declaration=True) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.xmltvimport.value: log.debug("Write: xml channels for xmltvimport") try: path = "/etc/xmltvimport/wunschliste.channels.xml" etree.write(path, encoding='utf-8', xml_declaration=True) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.crossepg.value: log.debug("Write: xml channels for crossepg") try: path = "/etc/crossepg/wunschliste.channels.xml" etree.write(path, encoding='utf-8', xml_declaration=True) except Exception as e: log.exception("Exception in write XML: " + str(e)) except Exception as e: log.exception("Exception in writeXML: " + str(e))
import xlrd from xml.etree.cElementTree import Element, ElementTree, SubElement, Comment wb = xlrd.open_workbook('./Docs/numbers.xls') ws = wb.sheet_by_index(0) content = list() for xr in range(ws.nrows): row = ws.row(xr) num_list = list() for i in row: value = i.value num_list.append(value) content.append(num_list) print(content) root = Element('root') comment = Comment('数字信息') root.append(comment) child = SubElement(root, 'numbers') child.text = str(content) tree = ElementTree(root) tree.write('./Docs/numbers.xml', encoding='utf8')
import xlrd from xml.etree.cElementTree import Element, ElementTree, Comment, SubElement wb = xlrd.open_workbook('./Docs/city.xls') ws = wb.sheet_by_index(0) data = dict() for rx in range(ws.nrows): row = ws.row(rx) key = row[0].value value = row[1].value data[key] = value root = Element('root') comment = Comment('城市信息') root.append(comment) child = SubElement(root, 'citys') child.text = str(data) tree = ElementTree(root) tree.write('./Docs/city.xml', encoding='utf8')