+++ /dev/null
-from xml.dom.minidom import *
-
-
-sample_xml_file = 'sample_rspec.xml'
-f = open(sample_xml_file, 'r')
-lines = f.readlines()
-xml = ""
-for line in lines:
- xml += line.replace('\n', '',).replace('\t', '').strip()
-
-dom = parseString(xml)
-
-def getText(nodelist):
- rc = ""
- for node in nodelist:
- if node.nodeType == node.TEXT_NODE:
- rc = rc + node.data
- return rc
-
-def handleRspec(rspec):
- # create rspec dict
- rdict = {}
- tempdic = []
- # loop through each network element
- for i in rspec.getElementsByTagName("NetSpec"):
- # handle networks call
- temp = handleNetworks(i)
- tempdic.append(temp)
- # append the temp dict
- rdict['networks'] = tempdic
- return rdict
-
-def handleIfs(interf):
- # create if dict
- ifdict = {}
- # loop through attribs and put key value pair into array
- for i in interf.attributes:
- a = node.attributes[i]
- ifdict[a.name] = a.value
-
- return ifdict
-
-def handleNodes(node):
- # create node dict
- nodict = {}
- # loop through attribs and put key value pair into array
- for i in node.attributes:
- a = node.attributes[i]
- nodict[a.name] = a.value
- tempd = []
- # loop through each IF element
- for i in node.getElementsByTagName("IfSpec"):
- # handle ifs
- tempd.append(handleIfs(i))
- # append temp dict
- nodict['ifs'] = tempd
- return nodict
-
-def handleNetworks(network):
- # create network dict
- ndict = {'name':network.nodeName}
- tempdict = []
- # loop through each node element
- for i in network.getElementsByTagName("NodeSpec"):
- # handle nodes
- tempdict.append(handleNodes(i))
- # append temp dict
- ndict['nodes'] = tempdict
- return ndict
-
-def handleTest(slices):
- for slide in slices:
- sdict = slice.getElementsByTagName("slices")[0]
- print "<p>%s</p>" % getText(slice.childNodes)
-
-handleRspec(dom)
-
+++ /dev/null
-from xml.dom.minidom import Document
-
-#rspec = {}
-#rspec['netspec'] = {'name':'planetlab.us'}
-#rspec['netspec']['node'] = {'name':'planetlab-1.cs.princeton.edu', 'type':'std'}
-#rspec['netspec']['node']['ifspec'] = {'addr':'128.112.139.71', 'type':'ipv4', 'min_rate':'0', 'max_rate':'10000000'}
-#rspec['netspec']['node'] = {'name':'planetlab-2.cs.princeton.edu', 'type':'std'}
-#rspec['netspec']['node']['ifspec'] = {'addr':'128.112.139.72', 'type':'ipv4', 'min_rate':'0', 'max_rate':'10000000'}
-#rspec['netspec']['node']['ifspec'] = {'addr':'128.112.139.73', 'type':'proxy', 'min_rate':'0', 'max_rate':'10000000'}
-#rspec['netspec']['node']['ifspec'] = {'addr':'128.112.139.74', 'type':'proxy', 'min_rate':'0', 'max_rate':'10000000'}
-#rspec['netspec'] = {'name':'planetlab.eu'}
-#rspec['netspec']['node'] = {'name':'onelab03.onelab.eu', 'type':'std'}
-#rspec['netspec']['node']['ifspec'] = {'addr':'128.112.139.321', 'type':'ipv4', 'min_rate':'0', 'max_rate':'10000000'}
-
-
-
-# Create the minidom document
-doc = Document()
-
-# <rspec> base element
-rspec = doc.createElement("RSpec")
-rspec.setAttribute("start_time", "1235696400")
-rspec.setAttribute("duration", "2419200")
-doc.appendChild(rspec)
-
-# networks
-networks = doc.createElement("networks")
-rspec.appendChild(networks)
-
-# netspec
-NetSpec = doc.createElement("NetSpec")
-NetSpec.setAttribute("name", "plc.us")
-networks.appendChild(NetSpec)
-
-# nodes
-nodes = doc.createElement("nodes")
-NetSpec.appendChild(nodes)
-
-# NodeSpec
-NodeSpec = doc.createElement("NodeSpec")
-NodeSpec.setAttribute("name", "planetlab-1.cs.princeton.edu")
-nodes.appendChild(NodeSpec)
-
-# Create a <p> element
-paragraph1 = doc.createElement("p")
-maincard.appendChild(paragraph1)
-
-## Give the <p> elemenet some text
-#ptext = doc.createTextNode("This is a test!")
-#paragraph1.appendChild(ptext)
-
-# Print our newly created XML
-print doc.toprettyxml(indent=" ")
-
-
+++ /dev/null
-"""Read in a DOM instance, convert it to a Python object
-"""
-from xml.dom.utils import FileReader
-
-class PyObject: pass
-
-def pyobj_printer(py_obj, level=0):
- """Return a "deep" string description of a Python object"""
- from string import join, split
- import types
- descript = ''
- for membname in dir(py_obj):
- member = getattr(py_obj,membname)
- if type(member) == types.InstanceType:
- descript = descript + (' '*level) + '{'+membname+'}\n'
- descript = descript + pyobj_printer(member, level+3)
- elif type(member) == types.ListType:
- descript = descript + (' '*level) + '['+membname+']\n'
- for i in range(len(member)):
- descript = descript+(' '*level)+str(i+1)+': '+ \
- pyobj_printer(member[i],level+3)
- else:
- descript = descript + membname+'='
- descript = descript + join(split(str(member)[:50]))+'...\n'
- return descript
-
-def pyobj_from_dom(dom_node):
- """Converts a DOM tree to a "native" Python object"""
- py_obj = PyObject()
- py_obj.PCDATA = ''
- for node in dom_node.get_childNodes():
- if node.name == '#text':
- py_obj.PCDATA = py_obj.PCDATA + node.value
- elif hasattr(py_obj, node.name):
- getattr(py_obj, node.name).append(pyobj_from_dom(node))
- else:
- setattr(py_obj, node.name, [pyobj_from_dom(node)])
- return py_obj
-
-# Main test
-dom_obj = FileReader("sample_rspec.xml").document
-py_obj = pyobj_from_dom(dom_obj)
-if __name__ == "__main__":
- print pyobj_printer(py_obj)
\ No newline at end of file