def __init__(self, uri=None, xmlString=None): global doc if uri is not None: doc = NonvalidatingReader.parseUri(uri) elif xmlString is not None: uri = 'file:bogusFile.txt' # Required by Domlette or it issues a warning. doc = NonvalidatingReader.parseString(xmlString, uri)
def read_file(my, file_path, cache=True): #my.reader = PyExpat.Reader() #my.doc = my.reader.fromUri(file_path) # the xml library does not like windows style separators try: file_path = file_path.replace("\\", "/") if not cache: my.doc = NonvalidatingReader.parseUri("file://%s" % file_path, my.uri) else: cur_mtime = os.path.getmtime(file_path) cache_mtime = my.XML_FILE_MTIME.get(file_path) if cur_mtime == cache_mtime: my.doc = my.XML_FILE_CACHE.get(file_path) else: my.doc = NonvalidatingReader.parseUri("file://%s" % file_path, my.uri) my.cache_xml(file_path, my.doc, cur_mtime) except Exception, e: #print "Error in xml file: ", file_path raise XmlException(e)
def parseFile(file): """File can be an open handle or filesystem path""" from Ft.Xml.Domlette import NonvalidatingReader if isinstance(file, basestring): dom = NonvalidatingReader.parseUri("file:%s" % self._file) else: dom = NonvalidatingReader.parseStream(file, **kwargs) return McmLogFile(dom)
def setConfig(self, text): """ sets new original configuration xml in this instances, recreates dynamic configuration """ logger().debug("VermontInstance.setConfig()") self.__cfgModified = True self.__cfgText = text if self.__parseXml: self.__cfgXml = NonvalidatingReader.parseString(text) self.__dynCfgModified = True self.__dynCfgText = self.cfgText if self.__parseXml: self.__dynCfgXml = NonvalidatingReader.parseString(self.__cfgText)
def doPost(self, args): if args["object"] == "as-number": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><bgp xmlns='urn:loria:madynes:ensuite:yencap:module:BGP:1.0'><bgprouter><%s xc:operation='%s'>%s</%s></bgprouter></bgp></routing></netconf>" % ( args['object'], args['bgpoperation'], args['ASnumber'], args['object']) elif args["object"] == "neighbors": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><bgp xmlns='urn:loria:madynes:ensuite:yencap:module:BGP:1.0'><bgprouter><%s><%s xc:operation='%s'><ip-address>%s</ip-address><remote-as>%s</remote-as></%s></%s></bgprouter></bgp></routing></netconf>" % ( args['object'], 'neighbor', args['bgpoperation'], args['neighborIpAddress'], args['neighborRemoteAs'], 'neighbor', args['object']) elif args["object"] == "route-maps": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><bgp xmlns='urn:loria:madynes:ensuite:yencap:module:BGP:1.0'><filters> <route-map xc:operation='%s'><map-tag>%s</map-tag><sequences><seq-number>%s</seq-number><state>%s</state><match><as-path><as-path-name>%s</as-path-name></as-path></match></sequences></route-map></filters></bgp></routing></netconf>" % ( args['bgpoperation'], args['maptag'], args['seqnumber'], args['state'], args['aspathname']) elif args["object"] == "afneighbors": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><bgp xmlns='urn:loria:madynes:ensuite:yencap:module:BGP:1.0'><bgprouter><address-families><ipv4-address-family><neighbors><neighbor><ip-address>%s</ip-address><bind-filters xc:operation='%s'><route-map><name>%s</name><direct>%s</direct></route-map></bind-filters></neighbor></neighbors></ipv4-address-family></address-families></bgprouter></bgp></routing></netconf>" % ( args['neighborIpAddress'], args['bgpoperation'], args['name'], args['direct']) else: return None, 0 cNode = NonvalidatingReader.parseString( config, 'http://madynes.loria.fr').documentElement attr = {'target': 'running', 'config': cNode} tstart = time.time() netconfReply = self.netconfSession.sendRequest('edit-config', attr) tend = time.time() tdiff = tend - tstart return netconfReply, tdiff
def _parseResults(self): if self.resultDOM is not None: from Ft.Xml.Domlette import NonvalidatingReader self.resultDOM = NonvalidatingReader.parseString(self.result) self.askAnswer=self.resultDOM.xpath( 'string(/sparql:sparql/sparql:boolean)', explicitNss=sparqlNsBindings)
def xml(xmlin, forced=False): """ Parse some XML. Argument xmlin can be a string, the filename of some XML; or an open file, from which xml is read. forced to True to skip caching check The return value is the parsed XML as DOM nodes. """ filename = None # A string argument is a file name. if isinstance(xmlin, (str, )): filename = _findFile(xmlin) if not filename: raise "Couldn't find XML to parse: %s" % xmlin if filename: if filename in _xmlcache and not forced: return _xmlcache[filename] xmlin = open(filename) xmldata = xmlin.read() if bDomlette: doc = NonvalidatingReader.parseString(xmldata, filename or ' ') else: doc = PyExpat.Reader().fromString(xmldata) parsedxml = HandyXmlWrapper(doc.documentElement) if filename: _xmlcache[filename] = parsedxml return parsedxml
def show_modulegraph(req, host): try: statxml = remotevm.getSensorData(host) except: return show_error(req, "failed to contact manager", traceback.format_exc()) doc = NonvalidatingReader.parseString(statxml) g = "digraph G {\n" g += "\tnode[fontsize=8,shape=box,fontname=Helvetica,height=.3]\n" g += "\trankdir=LR;\n" for m in doc.xpath('/vermont/sensorData/sensor[@type=\'module\']'): mid = m.xpath('@id')[0].nodeValue mname = "%s(%s)" % (m.xpath('@name')[0].nodeValue, mid) g += "\t%s [label=\"%s\"];\n" % (mid, mname) for n in m.xpath('next'): nid = n.childNodes[0].nodeValue g += "\t%s -> %s;\n" % (mid, nid) g += "}\n" fn = "/tmp/graph-%s.dot.tmp" % host gfn = "/tmp/graph-%s.png.tmp" % host fh = open(fn, "w") fh.write(g) fh.close() err = os.system("cat %s | dot -Tpng -o %s" % (fn, gfn)) if err != 0: raise Exception("failed to execute dot (error code %d)" % err) fh = open(gfn, "r") req.content_type = "image/png" req.write(fh.read()) fh.close()
def __init__(self,iptFile,ctFile,truncTime): # Read epidemic and truncate to truncTime self.infectives = [] self.labels = [] epiFile = open(iptFile,'r') for line in epiFile: toks = line.split() label = atoi(toks[0]) I = atof(toks[1]) N = atof(toks[2]) R = atof(toks[3]) if N <= truncTime: # Take individuals who have been notified by truncTime if R > truncTime: # If R > truncTime, set R = truncTime R = truncTime self.infectives.append(Infective(label,I,N,R)) self.labels.append(label) epiFile.close() # Read in XML conFile = Uri.OsPathToUri(ctFile) xmlSrc = DefaultFactory.fromUri(conFile,stripElements=[(EMPTY_NAMESPACE,'*',1)]) self.doc = NonvalidatingReader.parse(xmlSrc) # Remove from the contact DOM any contact info # for individuals that are not present in labels self.labels = set(self.labels) for contact in self.doc.documentElement.xpath(u'tc:contact',explicitNss={u'tc':u'tracedcontacts'}): contactLabel = atoi(contact.getAttributeNS(None,u'id')) if contactLabel not in self.labels: self.doc.documentElement.removeChild(contact)
def get(self, configDatastore): dataFile = C.YENCAP_HOME + '/Modules/VERMONT_Module/' + configDatastore + '.xml' doc = NonvalidatingReader.parseUri("file:" + dataFile) modulereply = ModuleReply(replynode=doc.documentElement) return modulereply
def test05CDATASectionsReplaced(self): xml = \ """<?xml version="1.0" encoding="UTF-8"?> <script> <![CDATA[ function matchwo(a,b) { if (a < b && a > 0) then { print("Match"); return 1; } else { print('Different'); return 0; } } ]]> </script> """ ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.failIf('CDATA' in c14n, "CDATA not removed, c14n = %s" % c14n) self.failUnless('<' in c14n, "Less than not converted, c14n = %s" % c14n) self.failUnless('>' in c14n, "Greater than not converted, c14n = %s" % c14n) self.failUnless('&' in c14n, "Ampersand not converted, c14n = %s" % c14n)
def doPost(self, args): if args["object"] == "redistribute": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><rip xmlns='urn:loria:madynes:ensuite:yencap:module:RIP:1.0'><redistribute><%s xc:operation='%s' metric='%s' route-map='%s'/></redistribute></rip></routing></netconf>" % ( args['redistribute'], args['ripoperation'], args['metric'], args['route-map']) elif args["object"] == "networks": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><rip xmlns='urn:loria:madynes:ensuite:yencap:module:RIP:1.0'><%s><%s xc:operation='%s'>%s</%s></%s></rip></routing></netconf>" % ( args['object'], "network", args['ripoperation'], args['network'], 'network', args['object']) elif args["object"] == "neighbors": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><rip xmlns='urn:loria:madynes:ensuite:yencap:module:RIP:1.0'><%s><%s xc:operation='%s'>%s</%s></%s></rip></routing></netconf>" % ( args['object'], "neighbor", args['ripoperation'], args['neighbor'], 'neighbor', args['object']) elif args["object"] == "passive-interfaces": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><rip xmlns='urn:loria:madynes:ensuite:yencap:module:RIP:1.0'><%s><%s xc:operation='%s'>%s</%s></%s></rip></routing></netconf>" % ( args['object'], "passive-interface", args['ripoperation'], args['passive-interface'], 'passive-interface', args['object']) elif args["object"] == "distribute-lists": config = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><routing><rip xmlns='urn:loria:madynes:ensuite:yencap:module:RIP:1.0'><%s><%s xc:operation='%s' direct='%s' name='%s'>%s</%s></%s></rip></routing></netconf>" % ( args['object'], "distribute-list", args['ripoperation'], args['direct'], args['name'], args['distribute-list'], 'distribute-list', args['object']) cNode = NonvalidatingReader.parseString( config, 'http://madynes.loria.fr').documentElement attr = {'target': 'running', 'config': cNode} tstart = time.time() netconfReply = self.netconfSession.sendRequest(C.EDIT_CONFIG, attr) tend = time.time() tdiff = tend - tstart return netconfReply, tdiff
def GetNumberOfNoisyStripsInDB(array, server, schema, dbname, folder, tag, channels): Array_numNoisyStrips = [] if (array[0] != -999): for i in range(len(array)): runNumber=array[i] iovSince=ConvertedRunNumber(runNumber) iovUntil=ConvertedRunNumber(runNumber+1)-1 derived_string=channelValueQuery(server, schema, dbname, folder, iovSince, iovUntil, tag, channels) derived=NonvalidatingReader.parseString(derived_string,uri="dummy") numNoisyModules=derived.xpath(u'count(//channel)') if numNoisyModules !=0.0: allDefectStripsList=(derived.xpath(u"//channel/value[@name='DefectList']")) numNoisyStrips=0 numNoisyStripsAdds=0 for strips in allDefectStripsList: words=strips.firstChild.data.split() for j in range(len(words)): jk=words[j] if jk.find("-")>=0: sep=jk.replace ( '-', ' ' ) sep1=sep.split() numNoisyStripsAdds=numNoisyStripsAdds+int(sep1[1])-int(sep1[0]) numNoisyStrips=numNoisyStrips+len(strips.firstChild.data.split()) Array_numNoisyStrips.append(numNoisyStrips + numNoisyStripsAdds) else: Array_numNoisyStrips.append(-999) return Array_numNoisyStrips
def main(argv=[__name__]): #Ideas borrowed from # http://www.artima.com/forums/flat.jsp?forum=106&thread=4829 if argv is None: argv = sys.argv try: try: optparser = command_line(argv) dtll_fname = ARGS[1] except KeyboardInterrupt: pass except: raise Usage(optparser.format_help()) enc, dec, inwrap, outwrap = codecs.lookup('utf-8') output_stem = OPTIONS.dt_modname_stem if not output_stem: output_stem = os.path.splitext(dtll_fname)[0] + '-datatypes' if dtll_fname == '-': dtllf = sys.stdin else: dtllf = open(dtll_fname, 'r') dtll_doc = NonvalidatingReader.parseStream(dtllf, 'http://example.com') run(dtll_doc, output_stem, OPTIONS.test_ready) except Usage, err: print >>sys.stderr, err.msg return 2
def Test(tester): tester.startGroup('Exercise namespace nodes') isrc = InputSource.DefaultFactory.fromString(SRC_1, Uri.OsPathToUri(os.getcwd())) doc = NonvalidatingReader.parse(isrc) con = Context.Context(doc, 1, 1) EXPR = '//namespace::node()' expr = Compile(EXPR) #expr is <AbbreviatedAbsoluteLocationPath: /descendant-or-self::node()/namespace::node()> #expr._rel is <Step: namespace::node()> #expr._step is <Step: descendant-or-self::node()> tester.startTest(EXPR) actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//node()/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '/*/*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(6, len(actual)) tester.testDone() EXPR = '/*/namespace::node()|/*/*/namespace::node()' expr = Compile(EXPR) tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(7, len(actual)) tester.testDone() EXPR = '//*' expr = Compile(EXPR) #expr is <AbbreviatedAbsoluteLocationPath: /descendant-or-self::node()/child::*> tester.startTest(EXPR) EXPECTED = [] actual = expr.evaluate(con) tester.compare(4, len(actual)) tester.testDone() return tester.groupDone()
def xml(xmlin, forced=False): """ Parse some XML. Argument xmlin can be a string, the filename of some XML; or an open file, from which xml is read. forced to True to skip caching check The return value is the parsed XML as DOM nodes. """ filename = None # A string argument is a file name. if isinstance(xmlin, types.StringTypes): filename = _findFile(xmlin) if not filename: raise "Couldn't find XML to parse: %s" % xmlin if filename: if _xmlcache.has_key(filename) and not forced: return _xmlcache[filename] xmlin = open(filename) xmldata = xmlin.read() if bDomlette: doc = NonvalidatingReader.parseString(xmldata, filename or ' ') else: doc = PyExpat.Reader().fromString(xmldata) parsedxml = HandyXmlWrapper(doc.documentElement) if filename: _xmlcache[filename] = parsedxml return parsedxml
def test08WhitespaceNormalized(self): # ...outside the document element and within start and end tags dat = \ ''' 1 2 3''' xml = \ '''<?xml version="1.0" encoding="UTF-8"?> <doc xmlns="http://example.com/default"> <a a2="2" a1="1" >%s</a> </doc> ''' % dat ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.failUnless('a1="1" a2="2"' in c14n, "Expecting single space between attributes") self.failUnless(dat in c14n, "Expecting element content to be preserved") sub = c14n[c14n.find('<a'):c14n.find('>')] self.failIf('\n' in sub, "Expecting removal of line breaks for 'a' element")
def test07EmptyElemsConvertedStartEndPairs(self): xml = '<?xml version="1.0" encoding="UTF-8"?><a/>' ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.failUnless(c14n == '<a></a>', "C14N = %s" % c14n)
def test02NormalizeLineBreaks(self): xml = '<?xml version="1.0" encoding="UTF-8"?>\r\n<a/>\r\n' ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.failIf('\r' in c14n, "Carriage return \r char found in c14n")
def main(argv=[__name__]): #Ideas borrowed from # http://www.artima.com/forums/flat.jsp?forum=106&thread=4829 if argv is None: argv = sys.argv try: try: optparser = command_line(argv) dtll_fname = ARGS[1] except KeyboardInterrupt: pass except: raise Usage(optparser.format_help()) enc, dec, inwrap, outwrap = codecs.lookup('utf-8') output_stem = OPTIONS.dt_modname_stem if not output_stem: output_stem = os.path.splitext(dtll_fname)[0] + '-datatypes' if dtll_fname == '-': dtllf = sys.stdin else: dtllf = open(dtll_fname, 'r') dtll_doc = NonvalidatingReader.parseStream(dtllf, 'http://example.com') run(dtll_doc, output_stem, OPTIONS.test_ready) except Usage, err: print >> sys.stderr, err.msg return 2
def _workerThread(self): while True: logger().info("VermontController._workerThread: collecting monitoring data now") self.rInterface.retrieveStatus() if self.rInterface.running: # try to read in statistics data several times ... xml = None trycount = 0 while xml is None: #if trycount>=100: #raise RuntimeError("Failed to read sensor data!") try: logger().debug("trying to read sensor data ...") self.rInterface.retrieveSensorData() xml = NonvalidatingReader.parseString(self.rInterface.sensorDataText) except: logger().error(traceback.format_exc()) logger().info("failed to read sensor data xml, trying again ...") time.sleep(1) trycount += 1 self.vMonitor.collect_data(xml) else: logger().info("VermontController._workerThread: skipping stat recording, as instance is not running") time.sleep(self.moninterval)
def __init__(self): """ Creating new modules for Netconf This somewhat define the set of capabilities Building the Module Register Table (MRT) """ self.logFile = C.YENCAP_HOME + "/Modules/LogModule/log.xml" self.doc = NonvalidatingReader.parseUri("file:"+self.logFile)
def __init__(self): """ Creating new modules for Netconf This somewhat define the set of capabilities Building the Module Register Table (MRT) """ self.logFile = C.YENCAP_HOME + "/Modules/LogModule/log.xml" self.doc = NonvalidatingReader.parseUri("file:" + self.logFile)
def exchangeCapabilities(self, clientsock): """ Exchange the capabilities with the manager. First sends the agent capabilities. Then waits for the remote manager capabilities. Creates a Netconf session and returns it. @type clientsock: socket @param clientsock: The socket for the current client @rtype: session @return: The session created by the SessionManager. """ # Loading hello element along with static capabilities from hello.xml file helloRoot = NonvalidatingReader.parseUri(C.HELLO_URI) helloNode = helloRoot.documentElement # Finding a unique session-id for that session self.session = sessionManager.getInstance().createSession( clientsock, self.username) sessionId = self.session.getSessionId() LogManager.getInstance().logInfo( "opening Netconf session: (sessionId=%s)" % (self.session.getSessionId())) # Setup the session-id value within session-id node sessionIdNode = helloRoot.createElementNS(C.NETCONF_XMLNS, C.SESSION_ID) helloNode.appendChild(sessionIdNode) sessionIdNode.appendChild(helloRoot.createTextNode(str(sessionId))) # Get the unique instance of the singleton ModuleManager: moduleManager = ModuleManager.getInstance() # Add the capabilities related to the modules to the hello message: for node in helloNode.childNodes: if (node.nodeType == Node.ELEMENT_NODE and node.tagName == C.CAPABILITIES): for module in moduleManager.getModules(): capabNode = helloRoot.createElementNS( C.NETCONF_XMLNS, C.CAPABILITY) capabText = helloRoot.createTextNode(module.namespace) capabNode.appendChild(capabText) node.appendChild(capabNode) # Convert the hello element to String before sending hellostr = util.convertNodeToString(helloNode) # Sending capabilities along with a new session-id self.send(hellostr) # Receiving capabilities of the manager data = self.receive() # Printing Manager capabilities LogManager.getInstance().logInfo( "Manager capabilities received: (sessionId=%s)" % (self.session.getSessionId()))
def getConfig(self, configDatastore): #xmlFile = open(C.YENCAP_HOME + '/Modules/VERMONT_Module/running.xml') #doc = parse(xmlFile) dataFile = C.YENCAP_HOME + '/Modules/VERMONT_Module/' + configDatastore + '.xml' doc = NonvalidatingReader.parseUri("file:" + dataFile) modulereply = ModuleReply(replynode=doc.documentElement) return modulereply
def Parse(source): """ Convenience function for parsing XML. Use this function with a single argument, which must either be a string (not Unicode object), file-like object (stream), file path or URI. Returns a Domlette node. Only pass strings or streams to this function if the XML is self-contained XML (i.e. not requiring access to any other resource such as external entities or includes). If you get URI resolution errors, do not use this function: use the lower-level APIs instead. As an example, if you want such resolution to use the current working directory as a base, parse as follows for strings: from Ft.Xml.Domlette import NonvalidatingReader from Ft.Lib import Uri XML = "<!DOCTYPE a [ <!ENTITY b "b.xml"> ]><a>&b;</a>" base = Uri.OsPathToUri('') #Turn CWD into a file: URL doc = NonvalidatingReader.parseString(XML, base) # during parsing, the replacement text for &b; # will be obtained from b.xml in the CWD For streams, use "parseStream" rather than "parseString" in the above. """ #do the imports within the function: a tad bit less efficient, but #avoid circular crap from Ft.Xml.Domlette import NonvalidatingReader from Ft.Lib import Uri, Uuid from Ft.Xml.Lib.XmlString import IsXml if hasattr(source, 'read'): #Create dummy Uri to use as base dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) return NonvalidatingReader.parseStream(source, dummy_uri) elif IsXml(source): dummy_uri = 'urn:uuid:' + Uuid.UuidAsString(Uuid.GenerateUuid()) return NonvalidatingReader.parseString(source, dummy_uri) elif Uri.IsAbsolute(source): #or not os.path.isfile(source): return NonvalidatingReader.parseUri(source) else: return NonvalidatingReader.parseUri(Uri.OsPathToUri(source))
def test01UTF8DocEncoding(self): # http://www.w3.org/TR/xml-c14n#Example-UTF8 xml = '<?xml version="1.0" encoding="ISO-8859-1"?><doc>©</doc>' ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() #self.assertEqual(c14n, '<doc>#xC2#xA9</doc>') self.assertEqual(c14n, '<doc>\xC2\xA9</doc>')
def __init__(self, uri=None, xmlString=None): global doc self.hierarchy = {} self.levelMap = LevelMap() if uri is not None: doc = NonvalidatingReader.parseUri(uri) self.parse() self.highestLevel = 0 self.createLevelMap() print "levelMap:", self.levelMap
def test15CmpZSIc14n(self): ftDoc=NonvalidatingReader.parseUri('file://'+mkPath('windows-ac.xml')) ftOut = StringIO() CanonicalPrint(ftDoc, ftOut) ftC14n = ftOut.getvalue() reader = PyExpat.Reader() dom = reader.fromStream(open('./windows-ac.xml')) zsiC14n = Canonicalize(dom) self.failUnless(ftC14n == zsiC14n, "ZSI C14N output differs")
def Test(tester): tester.startGroup('Idan Elfassy finds well-formedness bug in printer') doc = NonvalidatingReader.parseString(SRC1, __name__) tester.startTest('Ft.Xml.Domlette.Print') stream = cStringIO.StringIO() Print(doc, stream=stream) result = stream.getvalue() tester.compare(EXPECTED1_PRINTED, result) tester.testDone() tester.groupDone() return
def get(self, configDatastore): #status = getVERMONTStatus() #xmlFile = open(C.YENCAP_HOME + '/Modules/VERMONT_Module/running.xml') #doc = parse(xmlFile) dataFile = C.YENCAP_HOME + '/Modules/VERMONT_Module/' + configDatastore + '.xml' doc = NonvalidatingReader.parseUri("file:" + dataFile) modulereply = ModuleReply(replynode=doc.documentElement) return modulereply
def _get_result(self): doc = NonvalidatingReader.parseString(self.output, self.URI_P % self.name) ctxt = Context(doc, processorNss=NSS) nodes = Evaluate("//mml:math", context=ctxt) assert len(nodes) == 1 node = nodes[0] node.removeAttributeNS(None, u'begin-id') node.removeAttributeNS(None, u'end-id') o = StringIO.StringIO() Print(node, stream=o) return o.getvalue()
def run(self, iSrc): """ Given an InputSource, reads the document, processing XLinks therein. Warning: The document will be modified in place. """ document = NonvalidatingReader.parse(iSrc) xlinks = document.xpath("/descendant-or-self::*[@xlink:type]", explicitNss={"xlink": XLINK_NAMESPACE}) for link in xlinks: xlink = XLinkElements.Create(link, iSrc) xlink.process() return document
def test19ExclC14nWithXPathAndInclusiveNSPfx(self): # Exclusive C14N applied to portions of a SOAP message by extracting # using XPath inputFile = mkPath('soapGetAttCertResponse.xml') from xml.xpath.Context import Context from xml import xpath from xml.dom.ext.reader import PyExpat reader = PyExpat.Reader() dom = reader.fromStream(open(inputFile)) processorNss = \ { 'wsu': ("http://docs.oasis-open.org/wss/2004/01/" "oasis-200401-wss-wssecurity-utility-1.0.xsd"), } ctxt = Context(dom, processorNss=processorNss) zsiRefNodes = xpath.Evaluate('//*[@wsu:Id]', contextNode=dom, context=ctxt) # 4Suite ftDoc=NonvalidatingReader.parseUri('file://'+inputFile) ftOut = StringIO() # Extract nodes for signing xpathExpression = XPath.Compile('//*[@wsu:Id]') ctx = XPath.Context.Context(ftDoc, processorNss=processorNss) ftRefNodes = xpathExpression.evaluate(ctx) nsPfx = ['SOAP-ENV', 'ds'] for zsiRefNode, ftRefNode in zip(zsiRefNodes, ftRefNodes): # Get ref node and all it's children refSubsetList = getChildNodes(zsiRefNode) zsiRefC14n = Canonicalize(dom, None, subset=refSubsetList, unsuppressedPrefixes=nsPfx) print("_"*80) print("4Suite C14N with Prefixes %s:\n", zsiRefNode.nodeName) print(zsiRefC14n) # 4Suite equivalent ftOut = StringIO() CanonicalPrint(ftRefNode, stream=ftOut, exclusive=True, inclusivePrefixes=nsPfx) ftRefC14n = ftOut.getvalue() print("_"*80) print("4Suite Exclusive C14N %s:\n", ftRefNode.nodeName) print(ftRefC14n) self.assertEqual(zsiRefC14n, ftRefC14n)
def test10AttrValDelimitersSet2DblQuotes(self): xml = \ """<?xml version="1.0" encoding="UTF-8"?> <b y:a1='1' a3='"3"' xmlns:y='http://example.com/y' y:a2='2'/> """ ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.failIf("'" in c14n, "Expecting removal of apostrophes C14N = %s" % c14n)
def tostatic(self, file=None): self._exports = {} if file: self._doc = nr.parseUri(file) stripws(self._doc) names = [getname(i) for i in tags(self._doc, psins, rsrc)] classes = [getclass(i) for i in tags(self._doc, psins, rsrc)] self._unexpanded = [i for i in tags(self._doc, psins, rsrc) if getclass(i) in names] self._templates = [i for i in tags(self._doc, psins, rsrc) if getname(i) in classes] self._expandTemplates() self._expandResources() self._exportResources()
def run(self, iSrc): """ Given an InputSource, reads the document, processing XLinks therein. Warning: The document will be modified in place. """ document = NonvalidatingReader.parse(iSrc) xlinks = document.xpath('/descendant-or-self::*[@xlink:type]', explicitNss={'xlink': XLINK_NAMESPACE}) for link in xlinks: xlink = XLinkElements.Create(link, iSrc) xlink.process() return document
def _convertXML(self): """ Convert an XML result into a Python dom tree. This method can be overwritten in a subclass for a different conversion method. :return: converted result :rtype: 4Suite DOMlette instance (http://infinitesque.net/projects/4Suite/docs/CoreManual.html#domlette_API) """ try: from Ft.Xml import InputSource from Ft.Xml.Domlette import NonvalidatingReader except ImportError: raise Exception("4Suite-XML needs to be installed (http://pypi.python.org/pypi/4Suite-XML/1.0.2)") return NonvalidatingReader.parseStream(self.response, MEANINGLESS_URI)
def __init__ (self, sheet='/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/html/docbook.xsl' ): """ By default i use docbook stylesheet. I want to create processor and feed stylesheet before client connection, so i can save response time """ #instantiate processor self.processor=Processor.Processor() # configure stylesheet (like domlette object) sheet_uri = Uri.OsPathToUri(sheet,1) transform = NonvalidatingReader.parseUri(sheet_uri) self.processor.appendStylesheetNode(transform, sheet_uri)
def get(self, configName): if self.files.has_key(configName): doc = NonvalidatingReader.parseUri("file:" + self.files[configName]) moduleReply = ModuleReply(replynode=doc.documentElement) else: moduleReply = ModuleReply( error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.OPERATION_FAILED, error_severity=ModuleReply.ERROR, error_message="Unknown source: " + configName) return moduleReply
def get(self, configName): if self.files.has_key(configName): doc = NonvalidatingReader.parseUri("file:" + self.files[configName]) moduleReply = ModuleReply(replynode=doc.documentElement) else: moduleReply = ModuleReply(error_type=ModuleReply.APPLICATION, error_tag=ModuleReply.OPERATION_FAILED, error_severity=ModuleReply.ERROR, error_message="Unknown source: " + configName) return moduleReply
def test16Cmplxmlc14n(self): ftDoc=NonvalidatingReader.parseUri('file://'+mkPath('windows-ac.xml')) ftOut = StringIO() CanonicalPrint(ftDoc, ftOut) ftC14n = ftOut.getvalue() from lxml import etree as lxmlET lxmlElem = lxmlET.parse('./windows-ac.xml') lxmlETf = StringIO() lxmlElem.write_c14n(lxmlETf) lxmlETC14n = lxmlETf.getvalue() self.failUnless(ftC14n == lxmlETC14n, "lxml C14N output differs")
def doPost(self, args): baseop = args['rbacoperation'] start = "<netconf xmlns='urn:loria:madynes:ensuite:yencap:1.0' xmlns:xc='urn:ietf:params:xml:ns:netconf:base:1.0'><security>" end = "</security></netconf>" if args['object'] == 'Role': if baseop == 'Delete': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><roles><role id='%s' xc:operation='delete'><name>%s</name></role></roles></rbac>%s" % ( start, args['roleId'], args['name'], end) elif baseop == 'Create': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><roles><role xc:operation='create'><name>%s</name></role></roles></rbac>%s" % ( start, args['name'], end) elif baseop == 'Update': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><roles><role id='%s'><name xc:operation='replace'>%s</name></role></roles></rbac>%s" % ( start, args['roleId'], args['name'], end) elif args['object'] == 'User': if baseop == 'Delete': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><users><user id='%s' xc:operation='delete'><login>%s</login></user></users></rbac>%s" % ( start, args['userId'], args['login'], end) elif baseop == 'Create': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><users><user xc:operation='create'><login>%s</login></user></users></rbac>%s" % ( start, args['login'], end) elif baseop == 'Update': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><users><user id='%s'><login xc:operation='replace'>%s</login></user></users></rbac>%s" % ( start, args['userId'], args['login'], end) elif args['object'] == 'URA': if baseop == 'Delete': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><user-assignements><user-assignement id='%s' xc:operation='delete' userRef='%s' roleRef='%s'/></user-assignements></rbac>%s" % ( start, args['uraId'], args['userRef'], args['roleRef'], end) elif baseop == 'Create': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><user-assignements><user-assignement xc:operation='create' userRef='%s' roleRef='%s'></user-assignement></user-assignements></rbac>%s" % ( start, args['userRef'], args['roleRef'], end) elif baseop == 'Update': config = "%s<rbac xmlns='urn:loria:madynes:ensuite:yencap:module:RBAC:1.0'><user-assignements><user-assignement xc:operation='replace' id='%s' userRef='%s' roleRef='%s'></user-assignement></user-assignements></rbac>%s" % ( start, args['uraId'], args['userRef'], args['roleRef'], end) #print config cNode = NonvalidatingReader.parseString( config, 'http://madynes.loria.fr').documentElement attr = {'target': 'running', 'config': cNode} tstart = time.time() netconfReply = self.netconfSession.sendRequest('edit-config', attr) tend = time.time() tdiff = tend - tstart return netconfReply, tdiff
def processrequest(self, data): """ Forward the received message to the requestScanner and returns its reply @type data: string @param data: The message received from the client socket @rtype: string @return: The serialized XML reply to be sent back to the Netconf Manager """ self.netconfLock.acquire() try: # try to build the DOM, checking well-formness # http://madynes.loria.fr is there to avoid a warning... doc = NonvalidatingReader.parseString(data, 'http://madynes.loria.fr') # XML Schema validation #self.valid = util.validate(data,[C.NETCONF_SCHEMA_URI]) mainNode = doc.documentElement if mainNode.tagName == C.RPC: rpcRequest = Rpc(mainNode, self.session) response = rpcRequest.execute() elif mainNode.tagName == C.HELLO: response = '' else: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message= "An element is not known. It should be an rpc or hello tag." ) moduleReply.addErrorInfo("bad-element", mainNode.tagName) nodeReply = moduleReply.getXMLNodeReply() response = util.convertNodeToString(nodeReply) except Exception, exp: moduleReply = ModuleReply( error_type=ModuleReply.PROTOCOL, error_tag=ModuleReply.UNKNOWN_ELEMENT, error_severity=ModuleReply.ERROR, error_message="The Netconf message is not well-formed." + str(exp)) nodeReply = moduleReply.getXMLNodeReply() response = util.convertNodeToString(nodeReply)
def populate(self): #{ print self.name + ' populate()' for dictionary in self.parent.xpath('.//dictionary'): #{ current_dict = dictionary.getAttributeNS(None, 'n') side = dictionary.getAttributeNS(None, 'side') filename = dictionary.getAttributeNS(None, 'file') filename = self.working + '/cache/' + self.name + '/' + filename print ' % (' + current_dict + ') ' + side + ', ' + filename doc = NonvalidatingReader.parseUri('file:///' + filename) self.dictionary[side] = Dictionary(side, current_dict, filename, doc, self.tags, self.templates) #} self.dictionary['bidix'].hashes_left = self.dictionary['left'].hashes self.dictionary['bidix'].hashes_right = self.dictionary['right'].hashes
def entry_exists(existing, new): #{ existing = str('<doc>' + existing.encode('ascii', 'ignore') + '</doc>'); new = str('<doc>' + new.encode('ascii', 'ignore') + '</doc>'); print >> sys.stderr, ' %%%%%%%%%%%%%% '; print >> sys.stderr, ' %% existing %% '; print >> sys.stderr, ' ' , existing; print >> sys.stderr, ' %% new %% '; print >> sys.stderr, ' ' , new; print >> sys.stderr, ' %%%%%%%%%%%%%% '; existing_doc = NonvalidatingReader.parseString(existing); new_doc = NonvalidatingReader.parseString(new); for node in existing_doc.xpath('.//e'): #{ for new_node in new_doc.xpath('.//e'): #{ if equal_entries(node, new_node) == True: #{ return True; #} #} #} return False;
def Test(tester): tester.startGroup('CDATA sections in doc') isrc = InputSource.DefaultFactory.fromString(SRC_1, Uri.OsPathToUri(os.getcwd())) doc = NonvalidatingReader.parse(isrc) con = Context.Context(doc, 1, 1) EXPR = '/doc/elem/text()' expr = Compile(EXPR) tester.startTest(EXPR) actual = [node.data for node in expr.evaluate(con)] tester.compare(actual, ["abc"] * 3) tester.testDone() return tester.groupDone()
def refreshData(self, sourceName): """ This method must be called when the RBAC data has been modified using Netconf for instance. It parses the rbac.xml file and build a DOM document """ self.users = [] self.roles = [] self.permissions = [] sourcefilePath = "%s/%s-%s.xml" % (C.YENCAP_CONF_HOME, "RBAC", sourceName) destfilePath = "%s/%s-%s.xml" % (C.YENCAP_CONF_HOME, "RBAC", C.RUNNING) os.system("cp %s %s" % (sourcefilePath, destfilePath)) self.rbacdoc = NonvalidatingReader.parseUri("file:"+sourcefilePath).documentElement self.buildRbac()
def append(self, _entrada): #{ print >> sys.stderr, '> ', self.file print >> sys.stderr, self.side + ' append(' print >> sys.stderr, _entrada print >> sys.stderr, ')' for section in self.doc.xpath('.//section'): #{ print >> sys.stderr, '+ section : ' + section.getAttributeNS( None, 'id') if section.getAttributeNS(None, 'id') == 'main': #{ print >> sys.stderr, 'Appending to section....' insertion_point = section child_doc = NonvalidatingReader.parseString( _entrada.encode('utf-8'), 'urn:bogus:dummy') child_node = child_doc.xpath('.//e')[0] insertion_point.appendChild(child_node) print >> sys.stderr, 'Appended.'
def GetNumberOfNoisyModulesInDB(array, server, schema, dbname, folder, tag, channels): Array_numNoisyModules=[] if (array[0] != -999): for i in range(len(array)): runNumber=array[i] iovSince=ConvertedRunNumber(runNumber) iovUntil=ConvertedRunNumber(runNumber+1)-1 derived_string=channelValueQuery(server, schema, dbname, folder, iovSince, iovUntil, tag, channels) derived=NonvalidatingReader.parseString(derived_string,uri="dummy") numNoisyModules=derived.xpath(u'count(//channel)') if numNoisyModules !=0.0: Array_numNoisyModules.append(numNoisyModules) else: Array_numNoisyModules.append(-999) return Array_numNoisyModules
def setCapabilities(self, capabilities): self.capabilities = [] doc = NonvalidatingReader.parseString(capabilities.strip(), 'http://madynes.loria.fr') helloNode = doc.documentElement if helloNode.tagName == C.HELLO: for capabilitiesNode in helloNode.childNodes: if (capabilitiesNode.nodeType == Node.ELEMENT_NODE and capabilitiesNode.tagName == C.CAPABILITIES): for capabilityNode in capabilitiesNode.childNodes: if (capabilityNode.nodeType == Node.ELEMENT_NODE and capabilityNode.tagName == C.CAPABILITY): txtvalue = string.strip( str(capabilityNode.childNodes[0].nodeValue)) if not txtvalue in self.capabilities: self.capabilities.append(txtvalue) self.setYangModules()
def svn_log(self): """ Make a log query to the Subversion repository, return parsed XML document of query output. """ # Calculate the start and end times for log query now = time.time() then = now - self.LOG_PERIOD # Format the start/end times for use in svn command start_time = time.strftime("%Y-%m-%d", time.localtime(then)) end_time = time.strftime("%Y-%m-%d", time.localtime(now)) # Build the svn command invocation, execute it, and return # the XML results in a parsed document. cmd = '%s log --xml -v -r "{%s}:{%s}" %s' % \ (self.SVN_BIN, start_time, end_time, self.url) (sout, sin) = popen4(cmd) return NonvalidatingReader.parseStream(sout, self.url)