1 from osrf.net_obj import osrfNetworkRegisterHint
3 from osrf.set import osrfSettingsValue
5 import sys, string, xml.dom.minidom
6 from oils.const import OILS_NS_OBJ, OILS_NS_PERSIST, OILS_NS_REPORTER
11 global __global_parser
12 idlParser = oilsIDLParser();
13 idlParser.setIDL(osrfSettingsValue('IDL'))
15 __global_parser = idlParser
17 def oilsGetIDLParser():
18 global __global_parser
19 return __global_parser
21 class oilsIDLParser(object):
26 def setIDL(self, file):
27 osrfLogInfo("setting IDL file to " + str(file))
30 def __getAttr(self, node, name, ns=None):
31 """ Find the attribute value on a given node
32 Namespace is ignored for now..
33 not sure if minidom has namespace support.
35 for (k, v) in node.attributes.items():
41 """Parses the IDL file and builds class objects"""
43 doc = xml.dom.minidom.parse(self.idlFile)
44 root = doc.childNodes[0]
46 for child in root.childNodes:
48 if child.nodeType == child.ELEMENT_NODE:
50 # -----------------------------------------------------------------------
51 # 'child' is the main class node for a fieldmapper class.
52 # It has 'fields' and 'links' nodes as children.
53 # -----------------------------------------------------------------------
55 id = self.__getAttr(child, 'id')
56 self.IDLObject[id] = {}
57 obj = self.IDLObject[id]
60 obj['controller'] = self.__getAttr(child, 'controller')
61 obj['fieldmapper'] = self.__getAttr(child, 'oils_obj:fieldmapper', OILS_NS_OBJ)
62 obj['virtual'] = self.__getAttr(child, 'oils_perist:virtual', OILS_NS_PERSIST)
63 obj['rpt_label'] = self.__getAttr(child, 'reporter:label', OILS_NS_REPORTER)
64 obj['tablename'] = self.__getAttr(child, 'oils_persist:tablename', OILS_NS_REPORTER)
67 for classNode in child.childNodes:
68 if classNode.nodeType == classNode.ELEMENT_NODE:
69 if classNode.nodeName == 'fields':
70 keys = self.parseFields(id, classNode)
72 osrfNetworkRegisterHint(id, keys, 'array')
77 def parseFields(self, cls, fields):
78 """Takes the fields node and parses the included field elements"""
81 idlobj = self.IDLObject[cls]
83 for field in fields.childNodes:
84 if field.nodeType == field.ELEMENT_NODE:
87 for field in fields.childNodes:
89 if field.nodeType == fields.ELEMENT_NODE:
90 name = self.__getAttr(field, 'name')
91 position = int(self.__getAttr(field, 'oils_obj:array_position', OILS_NS_OBJ))
97 osrfLogErr("parseFields(): position out of range. pos=%d : key-size=%d" % (position, len(keys)))
100 virtual = self.__getAttr(field, 'oils_persist:virtual', OILS_NS_PERSIST)
101 obj['rpt_label'] = self.__getAttr(field, 'reporter:label', OILS_NS_REPORTER)
102 obj['rpt_dtype'] = self.__getAttr(field, 'reporter:datatype', OILS_NS_REPORTER)
103 obj['rpt_select'] = self.__getAttr(field, 'reporter:selector', OILS_NS_REPORTER)
105 if virtual == string.lower('true'):
106 obj['virtual'] = True
108 obj['virtual'] = False
110 idlobj['fields'].append(obj)