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 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 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 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 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 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 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 _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 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 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 _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 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 __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 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 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 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 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_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 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 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 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 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 _walkhtml(self, ip): '''Walks HTML input Arguments: ip -- HTML input path''' # Tidy the HTML and Feed into DOM tree try: html = nr.parseString(tidy(ip, output_xhtml=1)[2], 'file://file') # Don't embed if parsing errors except self._rex: return None # Extract only first child for cleanliness html = html.firstChild # Avoids issue with HTML docs made up of comments try: self._attdel(html, 'xmlns') except AttributeError: pass # Return HTML return html
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 Test(tester): tester.startGroup('Rich Salz reports default namespace munging') 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.startTest('Ft.Xml.Domlette.PrettyPrint') stream = cStringIO.StringIO() PrettyPrint(doc, stream=stream) result = stream.getvalue() tester.compare(EXPECTED1_PRETTY, result) tester.testDone() tester.groupDone() return
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 test12SuperflousNSdeclsRemoved(self): extraNS = "http://example.com/default" xml = \ """<?xml version="1.0" encoding="UTF-8"?> <doc xmlns:x="http://example.com/x" xmlns="%s"> <b y:a1='1' xmlns="%s" a3='"3"' xmlns:y='http://example.com/y' y:a2='2'/> </doc>""" % (extraNS, extraNS) ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() # Namespace should now only occur once... self.failUnless(c14n.find(extraNS) == c14n.rfind(extraNS), "Expecting removal of extra NS %s in output = %s" % \ (extraNS, c14n))
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 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 test01aPIsCommentsAndOutsideOfDocElem(self): # http://www.w3.org/TR/xml-c14n#Example-OutsideDoc - PIs, Comments, and # Outside of Document Element xml = \ '''<?xml version="1.0"?> <?xml-stylesheet href="doc.xsl" type="text/xsl" ?> <!DOCTYPE doc SYSTEM> <doc>Hello, world!<!-- Comment 1 --></doc> <?pi-without-data ?> <!-- Comment 2 --> <!-- Comment 3 -->''' #'''<?xml version="1.0"?> # #<?xml-stylesheet href="doc.xsl" # type="text/xsl" ?> # #<!DOCTYPE doc SYSTEM "doc.dtd"> # #<doc>Hello, world!<!-- Comment 1 --></doc> # #<?pi-without-data ?> # #<!-- Comment 2 --> # #<!-- Comment 3 -->''' exptdC14n = \ '''<?xml-stylesheet href="doc.xsl" type="text/xsl" ?> <doc>Hello, world!</doc> <?pi-without-data?>''' ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.assertEqual(c14n, exptdC14n)
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 RunListInDB(array, server, schema, dbname, folder, tag, channels): Array_runsInDB=[] if (len(array) != 0): for i in range(len(array)): runNumber=array[i] iovSince=ConvertedRunNumber(runNumber) iovUntil=ConvertedRunNumber(runNumber+1)-1 print "..checking for runs..." 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_runsInDB.append(runNumber) else: Array_runsInDB.append(-999) return Array_runsInDB
def test13DefAttrsAdded2EachElem(self): # Ref. http://www.w3.org/TR/xml-c14n#Example-SETags xml = '''<!DOCTYPE doc [<!ATTLIST e9 attr CDATA "default">]> <doc> <e1 /> <e2 ></e2> <e3 name = "elem3" id="elem3" /> <e4 name="elem4" id="elem4" ></e4> <e5 a:attr="out" b:attr="sorted" attr2="all" attr="I'm" xmlns:b="http://www.ietf.org" xmlns:a="http://www.w3.org" xmlns="http://example.org"/> <e6 xmlns="" xmlns:a="http://www.w3.org"> <e7 xmlns="http://www.ietf.org"> <e8 xmlns="" xmlns:a="http://www.w3.org"> <e9 xmlns="" xmlns:a="http://www.ietf.org"/> </e8> </e7> </e6> </doc>''' ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() exptdC14n = '''<doc> <e1></e1> <e2></e2> <e3 id="elem3" name="elem3"></e3> <e4 id="elem4" name="elem4"></e4> <e5 xmlns="http://example.org" xmlns:a="http://www.w3.org" xmlns:b="http://www.ietf.org" attr="I'm" attr2="all" b:attr="sorted" a:attr="out"></e5> <e6 xmlns:a="http://www.w3.org"> <e7 xmlns="http://www.ietf.org"> <e8 xmlns=""> <e9 xmlns:a="http://www.ietf.org" attr="default"></e9> </e8> </e7> </e6> </doc>''' self.assertEqual(c14n, exptdC14n)
def fetch(params, debug=None,nopost=None): if(not params.has_key('resultxpath')): return None xhtml=send(params,debug,nopost) if debug: print >> sys.stderr, 'checking results' # make it a 4suite document doc = NonvalidatingReader.parseString(xhtml,params['action']) context = Context(doc, processorNss={"h": XHTML_NS}) #Compute the XPath against the context results=Compile(params['resultxpath']) results=results.evaluate(context) res=[] for a in results: tf = cStringIO.StringIO() PrettyPrint(a,tf) t=tf.getvalue() res.append(t) tf.close() results=res if debug: print >> sys.stderr, 'done', params['action'] return (results, xhtml)
def fetch(params, debug=None, nopost=None): if (not params.has_key('resultxpath')): return None xhtml = send(params, debug, nopost) if debug: print >> sys.stderr, 'checking results' # make it a 4suite document doc = NonvalidatingReader.parseString(xhtml, params['action']) context = Context(doc, processorNss={"h": XHTML_NS}) #Compute the XPath against the context results = Compile(params['resultxpath']) results = results.evaluate(context) res = [] for a in results: tf = cStringIO.StringIO() PrettyPrint(a, tf) t = tf.getvalue() res.append(t) tf.close() results = res if debug: print >> sys.stderr, 'done', params['action'] return (results, xhtml)
def RunListInDB(array, server, schema, dbname, folder, tag, channels): Array_runsInDB = [] if (len(array) != 0): for i in range(len(array)): runNumber = array[i] iovSince = ConvertedRunNumber(runNumber) iovUntil = ConvertedRunNumber(runNumber + 1) - 1 print "..checking for runs..." 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_runsInDB.append(runNumber) else: Array_runsInDB.append(-999) return Array_runsInDB
def test06XMLDeclAndDTDRemoved(self): xmlDecl = '<?xml version="1.0" encoding="UTF-8"?>' dtd = \ """<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> """ xml = \ """%s %s<a/>""" % (xmlDecl, dtd) ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.failIf('<?xml version="1.0" encoding="UTF-8"?>' in c14n, "XML Declaration not removed") self.failIf(dtd in c14n, "DTD not removed")
def test04CharAndParsedEntityRefsReplaced(self): xml = '''<!DOCTYPE doc [ <!ATTLIST doc attrExtEnt ENTITY #IMPLIED> <!ENTITY ent1 "Hello"> <!ENTITY ent2 SYSTEM "world.txt"> <!ENTITY entExt SYSTEM "earth.gif" NDATA gif> <!NOTATION gif SYSTEM "viewgif.exe"> ]> <doc attrExtEnt="entExt"> &ent1;, &ent2;! </doc> <!-- Let world.txt contain "world" (excluding the quotes) -->''' exptdC14n = '''<doc attrExtEnt="entExt"> Hello, world! </doc>''' ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() self.assertEqual(c14n, exptdC14n)
def produce_entries(self): """Use xpaths to extract feed entries and entry attributes.""" # Fetch the HTML source, tidy it up, parse it. src = urlopen(self.SCRAPE_URL).read() tidy_src = tidy_string(src) doc = NonvalidatingReader.parseString(tidy_src, self.SCRAPE_URL) entries = [] # Iterate through the parts identified as feed entry nodes. for entry_node in doc.xpath(self.ENTRIES_XPATH, self.NSS): # For each entry attribute path, attempt to extract the value data = {} for k,v in self.ENTRY_XPATHS.items(): nodes = entry_node.xpath(v, self.NSS) vals = [x.nodeValue for x in nodes if x.nodeValue] data[k] = " ".join(vals) # Create and append the FeedEntryDict for this extraction entries.append(FeedEntryDict(data, self.date_fmt)) return entries
def test09WhitespaceInCharContentRetained(self): # http://www.w3.org/TR/xml-c14n#Example-WhitespaceInContent # Nb. excludes chars removed during line break normalization xml = \ '''<doc> <clean> </clean> <dirty> A B </dirty> <mixed> A <clean> </clean> B <dirty> A B </dirty> C </mixed> </doc>''' ftDoc = NonvalidatingReader.parseString(xml) f = StringIO() CanonicalPrint(ftDoc, f) c14n = f.getvalue() # In this case the canonicalized form should be identical to the # original self.assertEqual(c14n, xml)
def produce_entries(self): """Use xpaths to extract feed entries and entry attributes.""" # Fetch the HTML source, tidy it up, parse it. src = urlopen(self.SCRAPE_URL).read() tidy_src = tidy_string(src) doc = NonvalidatingReader.parseString(tidy_src, self.SCRAPE_URL) entries = [] # Iterate through the parts identified as feed entry nodes. for entry_node in doc.xpath(self.ENTRIES_XPATH, self.NSS): # For each entry attribute path, attempt to extract the value data = {} for k, v in self.ENTRY_XPATHS.items(): nodes = entry_node.xpath(v, self.NSS) vals = [x.nodeValue for x in nodes if x.nodeValue] data[k] = " ".join(vals) # Create and append the FeedEntryDict for this extraction entries.append(FeedEntryDict(data, self.date_fmt)) return entries
def test14DocumentSubsets(self): # Ref. http://www.w3.org/TR/xml-c14n#Example-DocSubsets xml = \ """<!DOCTYPE doc [ <!ATTLIST e2 xml:space (default|preserve) 'preserve'> <!ATTLIST e3 id ID #IMPLIED> ]> <doc xmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org"> <e1> <e2 xmlns=""> <e3 id="E3"/> </e2> </e1> </doc>""" #'''<!-- Evaluate with declaration xmlns:ietf="http://www.ietf.org" --> xpathExpr = \ ''' (//. | //@* | //namespace::*) [ self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2)) or count(id("E3")|ancestor-or-self::node()) = count(ancestor-or-self::node()) ]''' exptdC14n = \ '<e1 xmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org"><e3 xmlns="" id="E3" xml:space="preserve"></e3></e1>' ftDoc = NonvalidatingReader.parseString(xml) xpathExpression = XPath.Compile(xpathExpr) ctx = XPath.Context.Context(ftDoc) ftNode = xpathExpression.evaluate(ctx) f = StringIO() CanonicalPrint(ftNode, f) c14n = f.getvalue()
def read_string(my, xml_string, print_error=True): """ # skip snapshots if xml_string not in Xml.xmls: Xml.xmls.add(xml_string) else: print "already parsed!!!" #print xml_string[:150] #if xml_string.find("pipeline scale='49'") != -1: # sdafd #dfafds Xml.count += 1 print Xml.count """ # For whatever reason, parseString cannot be unicode??! if type(xml_string) != types.StringType: #xml_string = str(xml_string) xml_string = xml_string.encode('utf-8') #my.reader = PyExpat.Reader() #print xml_string #my.doc = my.reader.fromString(xml_string) try: # convert to utf-8 if type(xml_string) != types.StringType: xml_string = xml_string.encode("utf-8") my.doc = NonvalidatingReader.parseString(xml_string, my.uri) except Exception, e: if print_error: print "Error in xml: ", xml_string print str(e) raise XmlException(e)
def xml(s): """ parse the xml and return a Ft.Xml.Domlette """ return NonvalidatingReader.parseString(s)
contents = f.read() f.close() # quick and dirty fix for the multiword problem, we'll replace the back in the end contents = contents.replace('<b/>', '##b##') # another quick and diry fix for the enclitic join problem :( # this makes the <j/> and adjacent Bengali characters part of the node text, # right now this is the only way to preserve Bengali characters that appers # between the tags (mostly occurs in the R side) # we need to replace them back as well at the end contents = contents.replace('<j/>ই', '<s n="##jই##"/>').replace('<j/>ও', '<s n="##jও##"/>') doc = NonvalidatingReader.parseString(contents) #doc = NonvalidatingReader.parseUri('file:///' + dictionary); path = '/dictionary/pardefs/pardef' paradigms = {} # Convert from a dotted symbol list into a list of # symbols in XML: # # n.m.sg --> <s n="n"/><s n="m"/><s n="sg"/> # # The enclitic pardef # NOTE: If the dix was generated by expand-paradigms.py, this enclitic paradef # is not needed for the dix to work at that stage pardef_enclitic = u""" <pardef n="enclitic">
numNan = DummyNumberExpr(number.nan) numInf = DummyNumberExpr(number.inf) numNInf = DummyNumberExpr(-number.inf) strEmpty = DummyStringExpr(u'') str12345 = DummyStringExpr(u'12345') strPi = DummyStringExpr(u'3.14') strText = DummyStringExpr(u'Hi') strPiText = DummyStringExpr(u'3.14Hi') strSpace = DummyStringExpr(u'Ht \t There\t Mike') strHelloWorld = DummyStringExpr(u'hello world') STR_EGG1 = DummyStringExpr(u'egg1') STR_EGG2 = DummyStringExpr(u'egg2') DOC = "<spam><egg1>egg1</egg1><egg2>egg2</egg2></spam>" doc = NonvalidatingReader.parseString(DOC, 'http://foo/test/spam.xml') node1 = doc.documentElement.firstChild node2 = node1.nextSibling EMPTY_NODE_SET = DummyNodeSetExpr([]) ONE_NODE_SET = DummyNodeSetExpr([node1]) TWO_NODE_SET = DummyNodeSetExpr([node1, node2]) DOC2 = "<spam><egg0>0</egg0><egg1>1</egg1><egg0>0</egg0><egg1>1</egg1><egg0>0</egg0></spam>" doc = NonvalidatingReader.parseString(DOC2, 'http://foo/test/spam.xml') node1 = doc.documentElement.firstChild node2 = node1.nextSibling node3 = node2.nextSibling node4 = node3.nextSibling node5 = node4.nextSibling
</distribute-lists> </rip> """ ################################################################### # Here will be the content of edit-config() method of RIP_Module: # ################################################################### # First, instantiate a RIP manager ripManager = RIP_Manager("urn:loria:madynes:ensuite:yencap:module:RIP:1.0") # The table of commands that will have to be executed: commands = [] # Build an XML document which is a sub part of an edit-config: doc = NonvalidatingReader.parseString(m6, "madynes") # Print the edit-config, just for fun: PrettyPrint(doc) # Look for XML nodes having "operation" attribute: d = findEditOp() # Parse the running configure commands, building the RIP command objects: ec = ripManager.readCommands() for item in d: NS_RIP_Module = "urn:loria:madynes:ensuite:yencap:module:RIP:1.0" name = item.tagName operation = d[item]
def retrieveSensorData(self): logger().debug("VermontInstance.retrieveSensorData()") self.__sensorDataText = self._retrieveSensorData() if self.__parseXml: self.__sensorDataXml = NonvalidatingReader.parseString( self.__sensorDataText)
def retrieveSensorData(self): logger().debug("VermontInstance.retrieveSensorData()") self.__sensorDataText = self._retrieveSensorData() if self.__parseXml: self.__sensorDataXml = NonvalidatingReader.parseString(self.__sensorDataText)
if s == '': self.wfile.write( pf.getPage(self.path, httpSession, agent, error="Please fill all fields.")) return else: attr[C.SOURCE] = args[C.SOURCE] if args.has_key('inlineconfig'): inline = args['inlineconfig'] # Test if XML inline config is well-formed. try: inlineNode = NonvalidatingReader.parseString( inline, 'http://madynes.loria.fr').documentElement attr["inlineconfig"] = inlineNode except Exception, exp: self.wfile.write( pf.getPage( self.path, httpSession, agent, error="Inline config is not well formed : %s" % (str(exp)))) return elif operation == C.VALIDATE: attr = {} s = args[C.SOURCE] if s == '':
def dispatch(self, operation, args): """ Dispatch request to the appropriate treatment. @type operation : String @param operation: Request operation @type args : Dictionnary @param args : Dictionnary with the form args """ attr = {} pf = pageFactory.getInstance() # login operation at startup if (operation == 'login'): login = args['login'] password = args['password'] if login == "" or password == "": self.wfile.write( pf.getPage("", None, None, error="Login failed")) return user, session_id = self.auth.getSessionID(login, password) if (session_id == None or user == None): # if the couple login/password given is not correct # return index page another time self.wfile.write( pf.getPage("", None, None, error="Login failed")) return else: # if the couple login/password given is correct # create a new Session HTTP for this user ( session_id) self.sessionMGR.openSessionHTTP(session_id, user) self.send_response(200) self.send_header("Content-type", "text/html") # send a cookie with his session_id self.send_header("Set-cookie", 'session_id=%s;' % (session_id)) self.end_headers() # return the Manager home page self.wfile.write( pf.getPage("/main", self.sessionMGR.getSessionHTTP(session_id))) elif (operation == 'logout'): # close HTTP sessions opened by this user httpSession = self.sessionMGR.getSessionHTTP(args[C.COOKIE]) httpSession.closeHTTP() elif (operation == 'filter'): # get the current HTTP session httpSession = self.sessionMGR.getSessionHTTP(args[C.COOKIE]) # get the filter parameters self.wfile.write( pf.getPage("/main", httpSession, None, dictio=args)) elif (operation == 'Add'): # if the couple login/password given is not correct if (self.auth.isValid(args['login-add'], args['password-add'])): self.parser.addAgent(args['adr'], args['port'], args['ssh-login'], args['ssh-password']) self.htmlFactory.getPage('Manager/add-agent.html', self.wfile) elif (operation == 'Delete'): # if the couple login/password given is not correct if (self.auth.isValid(args['login-del'], args['password-del'])): agent = args['agents'] adr = agent[:agent.index(':')] port = agent[agent.index(':') + 1:] self.parser.removeAgent(adr, port) self.htmlFactory.getPage('Manager/delete-agent.html', self.wfile) elif (operation == 'connect'): # get the Session HTTP for this user httpSession = self.sessionMGR.getSessionHTTP(args[C.COOKIE]) if (not args.has_key('agentip')): self.wfile.write( pf.getPage("/main", httpSession, error="Please select at least one device.")) return agents = args['agentip'].split("', '") if len(agents) == 0: self.wfile.write(pf.getPage("/main", httpSession)) elif len(agents) == 1: agent = self.agentMGR.findAgentByIp(agents[0]) if agent.getStatus() == "unreachable": # Must be replaced by a specific page for unreachable agents with a specific menu self.wfile.write( pf.getPage( "/main", httpSession, agent=agent, error="Connection failed for Netconf agent " + agent.ip + " : unreachable<br/>")) # open a NetConf session to the given agent elif (httpSession.openNetconf(agent) != None): # return the desired agent home page httpSession.setCurrentNetconfSession(agent) self.wfile.write( pf.getPage("/agent", httpSession, agent=agent)) else: # Connection failed self.wfile.write( pf.getPage("/agentreachable", httpSession, agent=agent, error="Connection failed")) else: message = "" for a in agents: agent = self.agentMGR.findAgentByIp(a) if agent.getStatus() == "reachable": connected = httpSession.openNetconf(agent) if connected == None: message = message + "Connection failed for Netconf agent " + agent.ip + ".<br/>" else: message = message + "Connection failed for Netconf agent " + agent.ip + " : unreachable<br/>" if message != "": self.wfile.write( pf.getPage("/main", httpSession, error=message)) else: self.wfile.write(pf.getPage("/main", httpSession)) elif (operation == 'disconnect'): # get the Session HTTP for this user httpSession = self.sessionMGR.getSessionHTTP(args[C.COOKIE]) if (not args.has_key('agentip')): #if 'agentip' not in args.keys(): self.wfile.write( pf.getPage("/main", httpSession, error="Please select at least one device.")) return agents = args['agentip'].split("', '") if len(agents) == 0: self.wfile.write(pf.getPage("/main", httpSession)) else: message = "" for a in agents: agent = self.agentMGR.findAgentByIp(a) netconfSession = httpSession.getNetconf(agent) if netconfSession != None: httpSession.closeNetconfSession(netconfSession) else: message = message + "Agent " + agent.ip + " was not connected.<br/>" if message != "": self.wfile.write( pf.getPage("/main", httpSession, error=message)) else: self.wfile.write(pf.getPage("/main", httpSession)) elif operation in self.netconfOperations: # get the current HTTP session httpSession = self.sessionMGR.getSessionHTTP(args[C.COOKIE]) if httpSession == None: self.wfile.write(pf.getPage('/login')) return # get the NetConf session for this agent netconfSession = httpSession.getCurrentNetconfSession() if netconfSession == None: self.wfile.write(pf.getPage('/main', httpSession)) return agent = netconfSession.getAgent() # get all args of the http request to create the netconf request if operation == C.GET: attr = {} t = args[C.TYPE] f = args[C.FILTER] if t == '' and f == '': self.wfile.write( pf.getPage(self.path, httpSession, agent, error="Please fill all fields.")) return if t == C.SUBTREE: # Test if XML subtree is well-formed. try: if f != '': fNode = NonvalidatingReader.parseString( f, 'http://madynes.loria.fr').documentElement attr[C.FILTER] = fNode else: attr[C.FILTER] = '' except Exception, exp: self.wfile.write( pf.getPage( self.path, httpSession, agent, error="Subtree filter is not well formed : %s" % (str(exp)))) return elif t == C.XPATH: f = f.replace('\\r', '').replace('\\n', '') attr[C.FILTER] = f attr[C.TYPE] = t elif operation == C.GET_CONFIG: attr = {} if args.has_key(C.SOURCE): s = args[C.SOURCE] else: self.wfile.write( pf.getPage(self.path, httpSession, agent, error="Please fill all fields.")) return t = args[C.TYPE] f = args[C.FILTER] if t == '' and f == '': self.wfile.write( pf.getPage(self.path, httpSession, agent, error="Please fill all fields.")) return if t == C.SUBTREE: # Test if XML subtree is well-formed. try: if f != '': fNode = NonvalidatingReader.parseString( f, 'http://madynes.loria.fr').documentElement attr[C.FILTER] = fNode else: attr[C.FILTER] = f except Exception, exp: self.wfile.write( pf.getPage( self.path, httpSession, agent, error="Subtree filter is not well formed : %s" % (str(exp)))) return elif t == C.XPATH: f = f.replace('\\r', '').replace('\\n', '') attr[C.FILTER] = f attr[C.TYPE] = t attr[C.SOURCE] = s