sa/ma credentials include the rights authority+sa or authority+ma, authorities includ...
[sfa.git] / rspec / parse2.py
1 from xml.dom.minidom import *
2
3
4 sample_xml_file = 'sample_rspec.xml'
5 f = open(sample_xml_file, 'r')
6 lines = f.readlines()
7 xml = ""
8 for line in lines:
9     xml += line.replace('\n', '',).replace('\t', '').strip()
10     
11 dom = parseString(xml)
12
13 def getText(nodelist):
14     rc = ""
15     for node in nodelist:
16         if node.nodeType == node.TEXT_NODE:
17             rc = rc + node.data
18     return rc
19
20 def handleRspec(rspec):
21     # create rspec dict
22     rdict = {}
23     tempdic = []
24     # loop through each network element 
25     for i in rspec.getElementsByTagName("NetSpec"):
26         # handle networks call
27         temp = handleNetworks(i)
28         tempdic.append(temp)
29     # append the temp dict
30     rdict['networks'] = tempdic
31     return rdict
32
33 def handleIfs(interf):
34     # create if dict
35     ifdict = {}
36     # loop through attribs and put key value pair into array
37     for i in interf.attributes:
38         a = node.attributes[i]
39         ifdict[a.name] = a.value
40         
41     return ifdict
42
43 def handleNodes(node):
44     # create node dict
45     nodict = {}
46     # loop through attribs and put key value pair into array
47     for i in node.attributes:
48         a = node.attributes[i]
49         nodict[a.name] = a.value
50     tempd = []
51     # loop through each IF element
52     for i in node.getElementsByTagName("IfSpec"):
53         # handle ifs
54         tempd.append(handleIfs(i))
55     # append temp dict
56     nodict['ifs'] = tempd
57     return nodict
58
59 def handleNetworks(network):
60     # create network dict
61     ndict = {'name':network.nodeName}
62     tempdict = []
63     # loop through each node element
64     for i in network.getElementsByTagName("NodeSpec"):
65         # handle nodes
66         tempdict.append(handleNodes(i))
67     # append temp dict
68     ndict['nodes'] = tempdict
69     return ndict
70
71 def handleTest(slices):
72     for slide in slices:
73         sdict = slice.getElementsByTagName("slices")[0]
74         print "<p>%s</p>" % getText(slice.childNodes)
75
76 handleRspec(dom)
77