]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/python/oils/utils/idl.py
Fix typo that prevents parsing virtual fields
[Evergreen.git] / Open-ILS / src / python / oils / utils / idl.py
1 import osrf.net_obj
2 import osrf.log
3 import osrf.set
4
5 import sys, string, xml.dom.minidom
6 from oils.const import OILS_NS_OBJ, OILS_NS_PERSIST, OILS_NS_REPORTER
7
8 __global_parser = None
9
10 def oilsParseIDL():
11     global __global_parser
12     if __global_parser: return # no need to re-parse the IDL
13     idlParser = oilsIDLParser();
14     idlParser.setIDL(osrf.set.get('IDL'))
15     idlParser.parseIDL()
16     __global_parser = idlParser
17
18 def oilsGetIDLParser():
19     global __global_parser
20     return __global_parser
21
22 class oilsIDLParser(object):
23
24     def __init__(self):
25         self.IDLObject = {}
26
27     def setIDL(self, file):
28         osrf.log.log_info("setting IDL file to " + str(file))
29         self.idlFile = file
30
31     def __getAttr(self, node, name, ns=None):
32         """ Find the attribute value on a given node 
33             Namespace is ignored for now.. 
34             not sure if minidom has namespace support.
35             """
36         for (k, v) in node.attributes.items():
37             if k == name:
38                 return v
39         return None
40
41     def parseIDL(self):
42         """Parses the IDL file and builds class objects"""
43
44         doc = xml.dom.minidom.parse(self.idlFile)
45         root = doc.childNodes[0]
46
47         for child in root.childNodes:
48         
49             if child.nodeType == child.ELEMENT_NODE:
50         
51                 # -----------------------------------------------------------------------
52                 # 'child' is the main class node for a fieldmapper class.
53                 # It has 'fields' and 'links' nodes as children.
54                 # -----------------------------------------------------------------------
55
56                 id = self.__getAttr(child, 'id')
57                 self.IDLObject[id] = {}
58                 obj = self.IDLObject[id]
59                 obj['fields'] = []
60
61                 obj['controller'] = self.__getAttr(child, 'controller')
62                 obj['fieldmapper'] = self.__getAttr(child, 'oils_obj:fieldmapper', OILS_NS_OBJ)
63                 obj['virtual'] = self.__getAttr(child, 'oils_persist:virtual', OILS_NS_PERSIST)
64                 obj['rpt_label'] = self.__getAttr(child, 'reporter:label', OILS_NS_REPORTER)
65                 obj['tablename'] = self.__getAttr(child, 'oils_persist:tablename', OILS_NS_REPORTER)
66
67                 keys = []
68                 for classNode in child.childNodes:
69                     if classNode.nodeType == classNode.ELEMENT_NODE:
70                         if classNode.nodeName == 'fields':
71                             keys = self.parseFields(id, classNode)
72
73                 osrf.net_obj.register_hint(id, keys, 'array')
74
75         doc.unlink()
76
77
78     def parseFields(self, cls, fields):
79         """Takes the fields node and parses the included field elements"""
80
81         keys = []
82         idlobj = self.IDLObject[cls]
83
84         for field in fields.childNodes:
85             if field.nodeType == field.ELEMENT_NODE:
86                 keys.append(None)
87         
88         for field in fields.childNodes:
89             obj = {}
90             if field.nodeType == fields.ELEMENT_NODE:
91                 name            = self.__getAttr(field, 'name')
92                 position        = int(self.__getAttr(field, 'oils_obj:array_position', OILS_NS_OBJ))
93                 obj['name'] = name
94
95                 try:
96                     keys[position] = name
97                 except Exception, e:
98                     osrf.log.log_error("parseFields(): position out of range.  pos=%d : key-size=%d" % (position, len(keys)))
99                     raise e
100
101                 virtual = self.__getAttr(field, 'oils_persist:virtual', OILS_NS_PERSIST)
102                 obj['rpt_label']    = self.__getAttr(field, 'reporter:label', OILS_NS_REPORTER)
103                 obj['rpt_dtype']    = self.__getAttr(field, 'reporter:datatype', OILS_NS_REPORTER)
104                 obj['rpt_select']   = self.__getAttr(field, 'reporter:selector', OILS_NS_REPORTER)
105
106                 if virtual == string.lower('true'):
107                     obj['virtual']  = True
108                 else:
109                     obj['virtual']  = False
110
111                 idlobj['fields'].append(obj)
112
113         return keys
114
115
116
117