initial checkin of rspec object
[sfa.git] / util / rspec.py
1 import sys
2 import pprint
3 import os
4 from xml.dom import minidom
5
6 class Rspec():
7
8     def __init__(self, xml = None, xsd = None):
9         self.xsd = xsd
10         self.rootNode = None
11         if xml:
12             self.parse_string(xml)
13             
14
15     def _getText(self, nodelist):
16         rc = ""
17         for node in nodelist:
18             if node.nodeType == node.TEXT_NODE:
19                 rc = rc + node.data
20         return rc
21
22     # The rspec is comprised of 2 parts, and 1 reference:
23     # attributes/elements describe individual resources
24     # complexTypes are used to describe a set of attributes/elements
25     # complexTypes can include a reference to other complexTypes.
26
27
28     def _getName(self, node):
29         '''Gets name of node. If tag has no name, then return tag's localName'''
30         name = None
31         if not node.nodeName.startswith("#"):
32             if node.localName:
33                 name = node.localName
34             elif node.attributes.has_key("name"):
35                 name = node.attributes.get("name").value
36
37         return name     
38
39     # Attribute.  {name : nameofattribute, {items: values})
40     def _attributeDict(self, attributeDom):
41         '''Traverse single attribute node.  Create a dict {attributename : {name: value,}]}'''
42         node = {} # parsed dict
43         for attr in attributeDom.attributes.keys():
44             node[attr] = attributeDom.attributes.get(attr).value
45         return node
46
47
48     def toDict(self, nodeDom = None):
49         """
50         convert this rspec to a dict and return it.
51         """
52         node = {}
53         if not nodeDom:
54             nodeDom = self.rootNode
55
56         elementName = nodeDom.nodeName
57         if elementName and not elementName.startswith("#"):
58             # attributes have tags and values.  get {tag: value}, else {type: value} 
59             node.update(self._attributeDict(nodeDom))
60             # resolve the child nodes.
61             if nodeDom.hasChildNodes():
62                 for child in nodeDom.childNodes:
63                         childName = self._getName(child)
64                         if not childName:
65                             continue
66                         if not node.has_key(childName):
67                             node[childName] = []
68                         node[childName].append(self.toDict2(child))
69         return node
70
71     def toxml(self):
72         """
73         convert this rspec to an xml string and return it.
74         """
75         return self.rootNode.toxml()
76
77     def toprettyxml(self):
78         """
79         print this rspec in xml in a pretty format.
80         """
81         return self.rootNode.toprettyxml()
82
83     def parseFile(self, filename):
84         """
85         read a local xml file and store it as a dom object.
86         """
87         dom = minidom.parse(filename)
88         self.rootNode = dom.childNodes[0]
89
90
91     def parseString(self, xml):
92         """
93         read an xml string and store it as a dom object.
94         """
95         xml = xml.replace('\n', '').replace('\t', '').strip()
96         dom = minidom.parseString(xml)
97         self.rootNode = dom.childNodes[0]
98
99     def parseDict(self, d):
100         """
101         convert a dict object into a dom object.
102         """
103         pass
104
105