def Test(tester): tester.startGroup("Alexander Fayolle's encoding problems and variations") tester.startTest('XML output UTF-8 encoding') d = Sax2.FromXml(source_1) stream = cStringIO.StringIO() Print(d, stream=stream) result = stream.getvalue() if result != expected_1: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_1), repr(result))) tester.testDone() tester.startTest('XML output ISO-8859-1 encoding') d = Sax2.FromXml(source_1) stream = cStringIO.StringIO() Print(d, stream=stream, encoding='ISO-8859-1') result = stream.getvalue() if result != expected_2: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_2), repr(result))) tester.testDone() return tester.groupDone()
def test(): data = sys.stdin.read() doc = Sax2.FromXml(data) b1 = doc.createElementNS("http://foo.com", "foo:branch") c1 = doc.createElementNS("http://foo.com", "foo:child1") c2 = doc.createElementNS("http://foo.com", "foo:child2") b1.setAttributeNS("http://foo.com", "foo:a1", "value-1") a1 = b1.getAttributeNodeNS("http://foo.com", "a1") a1.value = "This shouldn't leak" b1.appendChild(c1) b1.appendChild(c2) doc.documentElement.appendChild(b1) r1 = doc.createElementNS("http://foo.com", "foo:replace") doc.documentElement.replaceChild(r1, b1) b1.removeChild(c2) import cStringIO s = cStringIO.StringIO() import xml.dom.ext xml.dom.ext.Print(doc, stream=s) ext.ReleaseNode(doc) ext.ReleaseNode(b1) doc = Sax2.FromXml(data) ext.ReleaseNode(doc)
def get_elements(file): elements = {} handle = open(file) reader = Sax2.Reader() doc = reader.fromStream(handle) handle.close() walker = doc.createTreeWalker(doc.documentElement, NodeFilter.SHOW_ELEMENT, None, 0) while walker.currentNode and walker.currentNode.tagName != 'elements': walker.nextNode() # we're at elements now el = walker.firstChild() while walker.currentNode: element = walker.firstChild() # loop over children of <element> name = None description = None while walker.currentNode: if walker.currentNode.tagName == 'name': name = walker.currentNode.firstChild.data.encode('UTF-8') if walker.currentNode.tagName == 'description': description = walker.currentNode.firstChild.data.encode( 'UTF-8') if not walker.nextSibling(): break # back up to <element> walker.parentNode() elements[name] = {'description': description} if not walker.nextSibling(): break return elements
def Test(tester): tester.startGroup("Testing PyExpat") reader = PyExpat.Reader() tester.startTest('Basic test') doc = reader.fromString(source_1) stream = cStringIO.StringIO() Print(doc, stream=stream) result = stream.getvalue() print result #if result != expected_1: # tester.error('Expected\n"""%s"""\ngot\n"""%s"""'%(repr(expected_1), repr(result))) reader.releaseNode(doc) tester.groupDone() tester.startGroup("Testing Sax2") reader = Sax2.Reader() tester.startTest('Basic test') doc = reader.fromString(source_1) stream = cStringIO.StringIO() Print(doc, stream=stream) result = stream.getvalue() print result #if result != expected_1: # tester.error('Expected\n"""%s"""\ngot\n"""%s"""'%(repr(expected_1), repr(result))) reader.releaseNode(doc) return tester.groupDone()
def makeXulPO(applicationDirectoryPath, applicationDomain=None, verbose=0): """Searches through xul files and appends to messages.pot""" if verbose: print "Importing xul templates..." path = Path(applicationDirectoryPath) messages = pot2dict('exe/locale/messages.pot') messageCommentTemplate = '\n#: %s:%s\nmsgid "' seq = len(messages) skipPaths = (applicationDirectoryPath / 'exe/webui/firefox', ) for fn in path.walkfiles(): if fn.ext.lower() == '.xul': for skipPath in skipPaths: if fn.startswith(skipPath): print 'IGNORING', fn break else: if verbose: print "template: ", fn reader = Sax2.Reader() doc = reader.fromStream(file(fn, 'rb')) xul2dict(doc, messages, seq, fn.relpath()) pot = Path('exe/locale/messages.pot') if pot.exists(): pot.remove() pot.touch() dict2pot(messages, 'exe/locale/messages.pot')
def getProperty_old(aFullPN): aPropertyDic = parseFullPN(aFullPN) reader = Sax2.Reader() doc = reader.fromStream(open('GP.eml', 'r')) system_names = doc.getElementsByTagName('system') for aSystem in system_names: if aSystem.getAttributeNS(EMPTY_NAMESPACE, 'id') == aPropertyDic['SystemPath']: entity_names = aSystem.getElementsByTagName( string.lower(aPropertyDic['EntityType'])) for aEntity in entity_names: if aEntity.getAttributeNS(EMPTY_NAMESPACE, 'id') == aPropertyDic['ID']: property_names = aEntity.getElementsByTagName('property') for aProperty in property_names: if aProperty.getAttributeNS( EMPTY_NAMESPACE, 'name') == aPropertyDic['property_name']: value_names = aProperty.getElementsByTagName( 'value') for aValue in value_names: return aValue.firstChild.data return 'None'
def map_to_array(file): f = open(file, 'r') reader = Sax2.Reader() xmldoc = reader.fromStream(f) array = [[None for y in range(12)] for x in range(999)] row = 0 for node in xmldoc.childNodes[1].childNodes: if node.nodeName == "name": levelname = node.firstChild.data if node.nodeName == "music": bg_music = node.firstChild.data if node.nodeName == "theme": theme = node.firstChild.data if node.nodeName == "row": cellc = 0 for cell in node.childNodes: if cell.nodeName == "cell": celldetails = {} for attr in ["map", "object", "enemy"]: temp = cell.attributes.getNamedItem(attr) if temp: celldetails[attr] = temp.value array[cellc][row] = celldetails cellc += 1 row += 1 mapinfo = levelname, theme, bg_music return array, mapinfo
def def_to_dict(file): f = open(file, 'r') reader = Sax2.Reader() xmldoc = reader.fromStream(f) mydict = {} myodict = {} myedict = {} for node in xmldoc.childNodes[1].childNodes: if node.nodeName == "image-dictionary": for defs in node.childNodes: if defs.nodeName == "map": for tiledef in defs.childNodes: if tiledef.attributes: mydict[tiledef.attributes.getNamedItem("name"). value] = tiledef.attributes.getNamedItem( "filename").value elif defs.nodeName == "objects": for objectdef in defs.childNodes: if objectdef.attributes: myodict[objectdef.attributes.getNamedItem("name"). value] = objectdef.attributes.getNamedItem( "filename").value elif defs.nodeName == "enemies": for enemydef in defs.childNodes: if enemydef.attributes: myedict[enemydef.attributes.getNamedItem("name"). value] = enemydef.attributes.getNamedItem( "prefix").value return mydict, myodict, myedict
def parseXMLString(self, xml): xml = xml.replace('\t', '') xml = xml.replace('\n', '') # create Reader object from xml.dom.ext.reader import Sax2 reader = Sax2.Reader() # parse the document and load it in a DOM tree domTree = reader.fromString(xml) walker = domTree.createTreeWalker(domTree.documentElement, NodeFilter.SHOW_ELEMENT, None, 0) #print 'walker.currentNode.tagName :', walker.currentNode.tagName rootNode = walker.currentNode if rootNode.hasAttribute('pythonXMLHandlerModule') \ and rootNode.hasAttribute('pythonXMLHandlerClass'): modulesName = rootNode.getAttribute('pythonXMLHandlerModule') ## print 'modulesName :', modulesName modulesName = modulesName.replace('xmlio.', 'xmliobak.') module = eval('__import__("' + modulesName + '", fromlist=[""])') ## print 'module : ', module className = rootNode.getAttribute('pythonXMLHandlerClass') xmlHandler = eval('module.' + className + '()') else: raise Exception('No Typed XML Handler declared in root tag!') return xmlHandler.rootTagDOMReader( walker) # An XML document always starts with the root tag
def run(self, params, args): (dryrun, host) = self.fillParams([('dryrun', None), ('host', None)]) if dryrun: dryrun = self.str2bool(dryrun) if not dryrun and host: self.abort("If you select a host you can't disable dryrun.") else: dryrun = True if not host: host = 'localhost' script = [] script.append('#!/bin/sh\n') script.append('yum clean all\n') rolls = [] for roll in args: rolls.append(roll) xml = self.command('list.host.xml', [host, 'roll=%s' % string.join(rolls, ',')]) reader = Sax2.Reader() gen = getattr(rocks.gen, 'Generator_%s' % self.os)() gen.setArch(self.arch) gen.setOS(self.os) gen.parse(xml) distPath = os.path.join( self.command('report.distro')[:-1], 'rocks-dist') tree = rocks.file.Tree(distPath) rpm_list = {} len_base_path = len('/export/rocks') base_url = "http://" + self.db.getHostAttr('localhost', 'Kickstart_PublicHostname') for file in tree.getFiles(os.path.join(self.arch, 'RedHat', 'RPMS')): if isinstance(file, rocks.file.RPMFile): rpm_url = base_url + file.getFullName()[len_base_path:] rpm_list[file.getBaseName()] = rpm_url rpm_list["%s.%s" % (file.getBaseName(), \ file.getPackageArch())] = rpm_url rpms = [] for line in gen.generate('packages'): if line.find('%package') == -1: rpms.append(line) for rpm in rpms: if rpm in rpm_list.keys(): script.append('yum install %s\n' % rpm) script.append(rpm_force_template % rpm_list[rpm]) script += gen.generate_config_script() if dryrun: self.addText(string.join(script, '')) else: os.system(string.join(script, ''))
def getID(fname): print fname # create Reader object reader = Sax2.Reader() # parse the document doc = reader.fromStream(fname) nodes = xpath.Evaluate('//*[local-name()="id"]', doc.documentElement) return nodes[0].firstChild.nodeValue
def loaddata(filename): try: fd = load(filename) except: raise IOError, 'Could not read file data' return None reader = Sax2.Reader() doc = reader.fromString(fd) return doc
def returnHP_EVA_ObjectFromXML(filename): from xml.dom.ext.reader import Sax2 from xml.dom.ext import PrettyPrint reader = Sax2.Reader(validate=0) xml_dump = open(filename, "r") document = reader.fromStream(xml_dump) xml_dump.close() result = HP_EVA_Object.fromXML(document.documentElement) return result
def fetchheadlinesfromurl(self): """ this fetches the headlines (title, link and description) from the url. Here the parsing of the xml is done """ headlines = [] # create Reader object reader = Sax2.Reader() popup = dialog.show_working_indicator(_('Fetching headlines...')) # parse the document try: myfile=urllib.urlopen(self.url) doc = reader.fromStream(myfile) items = doc.getElementsByTagName('item') for item in items: title = '' link = '' description = '' if item.hasChildNodes(): for c in item.childNodes: if c.localName == 'title': title = c.firstChild.data if c.localName == 'description': description = c.firstChild.data ################################# # Ajout pour identifier le lien de la video if self.mode == 'youtube': if c.localName == 'link': link='youtube:'+c.firstChild.data else: if c.localName == 'enclosure': attrs = c.attributes for attrName in attrs.keys(): attrNode = attrs.get(attrName) attrValue = attrNode.nodeValue if 'url' in attrName: link = attrValue if title: headlines.append((title, link, description)) except: #unreachable or url error logger.error('could not open %s', self.url) pass #write the file if len(headlines) > 0: pfile = os.path.join(self.cachedir, 'itv-%i' % self.location_index) util.save_pickle(headlines, pfile) popup.hide() return headlines
def openFile(self, filename): reader = Sax2.Reader(validate=1) stream = open(filename) doc = reader.fromStream(stream) dtd = reader.parser._parser.get_dtd() self.filename=filename self.updateTitle(filename) self.status("open of file %s succeeded" % filename) return (doc, dtd)
def ParseXML(xmltext): reader = Sax2.Reader() d = reader.fromString(xmltext) context = xml.xpath.Context.Context(d) #context.setNamespaces(namespaces) query = "/" e = xml.xpath.Compile(query) result = e.evaluate(context) node = result[0] return node
def test(tester): tester.startGroup('XML With Namespaces') tester.startTest('Namespaces multiply defined at 2nd level') from xml.dom.ext.reader import Sax2 import xml.dom.ext doc = Sax2.FromXml(source_1) xml.dom.ext.Print(doc) tester.testDone() return tester.groupDone()
def __init__(self, query): from xml.dom.ext.reader import Sax2 from comoonics.cluster.ComClusterRepository import ClusterRepository from comoonics.cluster.ComClusterInfo import ClusterInfo ClusterAssistantHelper.__init__(self, query) self.error=False # create Reader object try: reader = Sax2.Reader() _file = open("/etc/cluster/cluster.conf", "r") reader = Sax2.Reader() doc = reader.fromStream(_file) #create comclusterRepository Object clusterRepository = ClusterRepository(doc.documentElement, doc) #create comclusterinfo object self.clusterInfo = ClusterInfo(clusterRepository) except Exception, e: ComLog.getLogger(__logStrLevel__).error("Error parsing cluster.conf %s" %e) ComLog.errorTraceLog() self.error=True
def FetchXML(self): response = urllib.urlopen(self.url) xmltext = response.read() reader = Sax2.Reader() d = reader.fromString(xmltext) context = xml.xpath.Context.Context(d) context.setNamespaces(namespaces) query = "/" e = xml.xpath.Compile(query) result = e.evaluate(context) self.result = result[0]
def populateIsoList(): numIsos = 0 isoList = [] filelist = os.listdir(globals.ISOLISTDIR) filelist.sort() # for every file in the directory for filename in filelist: #~ print filename # find xml files if re.search('\.xml$', filename): # data about this iso will be stored in here: iso = Iso() # read the xml file reader = Sax2.Reader() doc = reader.fromStream('file://' + globals.ISOLISTDIR + filename) for node in doc.documentElement.childNodes: if node.nodeType == Node.ELEMENT_NODE: #~ print node.nodeName + ' -> ' + node.firstChild.nodeValue if node.firstChild: nodeValue = node.firstChild.nodeValue else: # this happens for an empty tag continue if node.nodeName == 'displayname': iso.displayname = nodeValue elif node.nodeName == 'description': iso.description = nodeValue elif node.nodeName == 'longdescription': iso.longdescription = nodeValue elif node.nodeName == 'picture': iso.picture = globals.ISOIMAGEPATH + nodeValue elif node.nodeName == 'filename': iso.filename = globals.ISOPATH + nodeValue elif node.nodeName == 'type': iso.type = nodeValue isoList.append(iso) numIsos += 1 if numIsos >= globals.MAXNUMISOS: break return isoList
def _get_contentDocument(self): if not self.__content: source = self._get_src() import os.path ext = os.path.splitext(source) if string.find(ext, 'htm') > 0: from xml.dom.ext.reader import HtmlLib self.__content = HtmlLib.FromHtmlUrl(source) elif string.lower(ext) == '.xml': from xml.dom.ext.reader import Sax2 self.__content = Sax2.FromXmlUrl(source) return self.__content
def format_xml(xml_string): if xml_string: reader = Sax2.Reader() doc = reader.fromString(xml_string) doc.normalize() f = StringIO.StringIO() PrettyPrint(doc, f) f.seek(0,0) formated_xml = f.read().decode('utf-8') return formated_xml else: return ''
def __init__(self, file=None, domnodelist=None): # EP Constructor a partir d'un llistat de nodes, caldra crear manualment l'arrel # (per passar la llista, millor xpath per seleccionar attributs i tal) if file is None: file = cStringIO.StringIO("<DescriptorsPool/>") self.doc = Sax2.Reader().fromStream(file) if domnodelist is not None: root = self.doc.getElementsByTagName("DescriptorsPool")[0] for node in domnodelist: cloned = self.doc.importNode(node, True) root.appendChild(cloned)
def Test(tester): tester.startGroup("Alexander Fayolle's Problems and variations") tester.startTest('Bad setAttNS test') d = Sax2.FromXml('<doc/>') e = d.createElementNS('', 'elt') d.documentElement.appendChild(e) try: e.setAttributeNS('http://logilab', 'att', 'value1') except DOMException, x: if x.code != NAMESPACE_ERR: name = getExceptionName(x.code) tester.error("Wrong exception '%s', expected NAMESPACE_ERR" % name)
def LastOutput2XML(self): """ Converts the last output to XML if possible returns None if not possible """ match = HP_EVA_SSSU.MATCH_XMLOBJECT.match(self.last_output) #mylogger.debug("%s, %s" %(self.last_output,match)) if match: from xml.dom.ext.reader import Sax2 reader = Sax2.Reader(validate=0) #mylogger.debug("xml: %s" %(match.group(1))) doc = reader.fromString(match.group(1)) return doc.documentElement else: return None
def __init__(self, xml_tmpl_file, value_def_file, xsl_file=None, validate=0, scan=False, xsl_path="/opt/atix/comoonics-cs/xsl"): self.xsl_file=xsl_file self.xml_tmpl_file=xml_tmpl_file self.xsl_path=xsl_path reader = Sax2.Reader(validate) self.doc = reader.fromStream(xml_tmpl_file) self.value_def_doc = reader.fromStream(value_def_file) if xsl_file == True: self.xsl_file=self._search_xsl_file() if self.xsl_file == None: raise FileNotFoundException("Could not find xsl file in %s for %s" %(self.xsl_path, self.xml_tmpl_file)) self._createInfoDict(scan)
def Test(tester): tester.startGroup( "Nicolas Chauvat <*****@*****.**>'s Printer Bug Report") tester.startTest('Attribute quote print') d = Sax2.FromXml(source_1) stream = cStringIO.StringIO() Print(d, stream=stream) result = stream.getvalue() if result != expected_1: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_1), repr(result))) return tester.groupDone()
def testHP_ObjectFromXML(filename, xml=True): from xml.dom.ext.reader import Sax2 from xml.dom.ext import PrettyPrint reader = Sax2.Reader(validate=0) xml_dump = open(filename, "r") print "From XML(%s):" % (filename) document = reader.fromStream(xml_dump) xml_dump.close() print "The HP_Oject:" result = HP_EVA_Object.fromXML(document.documentElement) print "Class: %s" % (result.__class__) if xml: PrettyPrint(result.toXML()) else: print result return result
def get_vm_infos(name): global vm_list global gmetric ret = '' c = 0 cpuinfos = '' meminfos = '' diskinfos = '' netinfos = '' vminfos = '' sep = '' for i in vm_list: cmd = 'virsh dumpxml ' + i[0] (stat, domxml) = commands.getstatusoutput(cmd) doc = Sax2.FromXml(domxml) c += 1 if c > 1: sep = '|' prefix = sep + "vmName:" + i[1] + ";" cpuinfos += prefix + get_vm_cpuinfos(doc, i[0]) meminfos += prefix + get_vm_meminfos(doc, i[0]) diskinfos += prefix + get_vm_diskinfos(doc, i[0]) netinfos += prefix + get_vm_netinfos(doc, i[0]) vminfos += sep + i[1] + ':' + i[2] if c >= 0: cmd = gmetric + ' -n "vm_cpu_infos" -v "' + cpuinfos + '"' #print cmd (stat, output) = commands.getstatusoutput(cmd) cmd = gmetric + ' -n "vm_mem_infos" -v "' + meminfos + '"' #print cmd (stat, output) = commands.getstatusoutput(cmd) cmd = gmetric + ' -n "vm_disk_infos" -v "' + diskinfos + '"' #print cmd #print len(cmd) (stat, output) = commands.getstatusoutput(cmd) cmd = gmetric + ' -n "vm_net_infos" -v "' + netinfos + '"' #print cmd (stat, output) = commands.getstatusoutput(cmd) cmd = gmetric + ' -n "vm_name_infos" -v "' + vminfos + '"' #print cmd #print len(cmd) (stat, output) = commands.getstatusoutput(cmd) return c # ret # Ganglia python module's string length limited
def to_xml_stream(self, stream, pretty=True, indent=' ', encoding='UTF-8', **kwargs): '''Dump object to a file object like stream.''' close = False if isinstance(stream, basestring): close = True if _xmldomext: stream = file(stream, 'w') else: stream = codecs.open(stream, mode='w', encoding=encoding, errors='replace') try: e = self.to_xml_elt(**kwargs) if pretty: if _xmldomext: PrettyPrint(Sax2.Reader().fromString( ElementTree.tostring(e)), stream=stream, encoding=encoding, indent=indent, preserveElements=None) else: # minidom.parseString( # ElementTree.tostring(e)).writexml( # stream, addindent=indent, newl='\n') pretty_indent(e) stream.write(ElementTree.tostring(e)) else: d = ElementTree.ElementTree(e) #d.write(stream, xml_declaration=True, method="xml") d.write(stream, encoding=encoding, xml_declaration=True, method="xml") finally: if close: stream.close() return e