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 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 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 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 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 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 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 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 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 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 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 __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 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 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 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 getEnterprisecopy(self): from xml.dom.ext.reader import Sax2 from XmlTools import clone_node import xml.dom from comoonics import odict import os.path result_doc = None source = None reader = Sax2.Reader(validate=self.xml_validate) ret_doc = None ret_element = None _sets = odict.Odict() for _templatefile in self.templatefiles: _file = open( os.path.join(self.sysreport_templatesbase, _templatefile), "r") doc = reader.fromStream(_file) if not ret_doc: _impl = xml.dom.getDOMImplementation() ret_doc = _impl.createDocument(None, doc.documentElement.tagName, None) ret_element = ret_doc.documentElement for _child in doc.documentElement.childNodes: if _child.nodeType == xml.dom.Node.ELEMENT_NODE: if _child.hasAttribute("name"): _sets[_child.getAttribute("name")] = clone_node( _child, ret_doc) elif _child.nodeType == xml.dom.Node.ATTRIBUTE_NODE: ret_element.appendChild(clone_node(_child, ret_doc)) # remove the save-sysreport and add it to the end _save_set = _sets[self.save_set] del _sets[self.save_set] _sets[self.save_set] = _save_set for _set in _sets.values(): Sysreport.logger.debug("getEnterprisecopy() adding child: %s" % _set.getAttribute("name")) ret_element.appendChild(_set) del _sets[self.save_set] del _sets[self.head_set] self.sets = _sets return EnterpriseCopy(ret_element, ret_doc)
def fetchheadlinesfromurl(self): headlines = [] # create Reader object reader = Sax2.Reader() popup = PopupBox(text=_('Fetching headlines...')) popup.show() # 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 == 'link': link = c.firstChild.data if c.localName == 'description': description = c.firstChild.data if title: headlines.append((title, link, description)) except: #unreachable or url error print 'HEADLINES ERROR: could not open %s' % self.url pass #write the file if len(headlines) > 0: pfile = os.path.join(self.cachedir, 'headlines-%i' % self.location_index) util.save_pickle(headlines, pfile) popup.destroy() return headlines
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
def POST(self, apiCall, apiPrefix, *args): endpoint = 'http://' + self._hostPrefix + '.alchemyapi.com/calls/' + apiPrefix + '/' + apiCall argText = 'apikey=' + self._apiKey + '&outputMode=xml' index = 0 while index < len(args): argText += "&" + args[index] index += 1 if index < len(args): argText += "=" + urllib.quote(args[index]) index += 1 handle = urllib.urlopen(endpoint, argText); result = handle.read(); handle.close(); reader = Sax2.Reader(); doc = reader.fromString(result); nodes = xpath.Evaluate('/results/status', doc.documentElement); if nodes[0].firstChild.nodeValue != "OK": raise 'Error making API call.' return result
def read_nlpp_file(self, filename): dom = getDOMImplementation() file = open (filename, "r") reader = Sax2.Reader() doc = reader.fromStream (file) file.close() headerList = xpath.Evaluate("pseudo/header", doc) if (headerList != []): header = headerList[0] elem = header.getAttribute("symbol") Z = header.getAttribute("atomic-number") charge = header.getAttribute("zval") self.Widgets[0].set_label(elem) self.Widgets[1].set_label(Z) self.Charge = int(float(charge)) self.emit("changed") return True else: self.Widgets[0].set_label("Invalid PP") self.Widgets[1].set_label("-1") return False
def ProcessXMLFile(self, xmlFileName): '''Returns a list of method information objects containing information about each method in the class. xmlFileName - The name of the XML file containing the class information. ''' reader = Sax2.Reader() xmlDocument = reader.fromStream(xmlFileName) documentElement = xmlDocument.documentElement self.ClassName = self.GetTextFromElement( documentElement, 'compounddef/compoundname/text()') self.MethodInformations = [] methodElements = self.GetMethodElements(documentElement) for methodElement in methodElements: methodInformation = self.GetMethodInformation(methodElement) self.MethodInformations.append(methodInformation) self.DocumentationStrings = {} for methodInfo in self.MethodInformations: self.DocumentationStrings[ methodInfo.Name] = '%feature ("docstring", "' for methodInfo in self.MethodInformations: self.DocumentationStrings[ methodInfo.Name] += '\n' + str(methodInfo) + '\n' if methodInfo.BriefDescription != '': self.DocumentationStrings[ methodInfo.Name] += methodInfo.BriefDescription + '\n' if methodInfo.DetailedDescription != '': self.DocumentationStrings[ methodInfo.Name] += methodInfo.DetailedDescription + '\n' keys = self.DocumentationStrings.keys() for key in keys: self.DocumentationStrings[ key] += '") ' + methodInfo.ClassName + '::' + key + ';\n'
def loadParadigm(self, p_filename): """ Load the given paradigm (XML file) Attributes are stored in self.attributes Data are stored in self.data They can be accessed as follows: self.attributes['gender'] # list of genders self.data[6]['gender'] # gender for the sixth data object self.data[6]['content'] # content for the sixth data object """ from nodebox_linguistics_extended.parser.nltk_lite.corpora import get_basedir basedir = get_basedir() # Look for the file try_filename = os.path.join(get_basedir(), "paradigms", p_filename) try: f = open(try_filename) p_filename = try_filename except IOError: print("Cannot find file") return None f.close() # These variables will be set by this method self.attributes = {} # A new dictionary self.data = [] # A new list # XML admin: create Reader object, parse document reader = Sax2.Reader() doc = reader.fromStream(p_filename) # Cycle through the given attributes and add them to self.attributes # for <name> in <attributes> attributes = doc.getElementsByTagName('attributes')[0] for name in attributes.getElementsByTagName('name'): # Setup a list of attribute values tmp_list = [] # for each value under name, store in list for value in name.getElementsByTagName('value'): tmp_list.append(value.getAttribute('value')) # Store list of values in dictionary self.attributes[name.getAttribute('name')] = tmp_list # Cycle through data objects and add them to self.data # for <form> in <paradigm> forms = doc.getElementsByTagName('paradigm')[0] for form in forms.getElementsByTagName('form'): # Initialise a temporary dictionary tmp_dict = {} for value in form.getElementsByTagName('attribute'): tmp_dict[value.getAttribute('name')] = value.getAttribute( 'value') # Add the new dictionary to the data list self.data.append(tmp_dict) # Talk to the user print("Paradigm information successfully loaded from file:", p_filename) # State the number and print out a list of attributes print(" " * 4 + str(len(self.attributes)) + " attributes imported:", end=' ') for att in self.attributes: print(att, end=' ') print() # State the number of paradigm objects imported print(" " * 4 + str(len(self.data)) + " paradigm objects imported.") return
from xml.dom.ext import PrettyPrint from xml.dom.ext.reader import Sax2 from xml import xpath import xml def printDom(doc): PrettyPrint(doc) filename = "./example_config.xml" if len(sys.argv) > 1: filename = sys.argv[1] # create Reader object reader = Sax2.Reader(validate=1) #parse the document file = os.fdopen(os.open(filename, os.O_RDONLY)) doc = reader.fromStream(file) printDom(doc) # $Log: testxml.py,v $ # Revision 1.2 2006-06-30 13:56:21 marc # changed default filename # # Revision 1.1 2006/06/29 16:36:35 marc # test for xml # # Revision 1.6 2006/06/28 12:28:55 mark