]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/python/oils/utils/idl.py
fixed some attr fetching bugs. also loading tablename (fwiw). fixed some logging
[working/Evergreen.git] / Open-ILS / src / python / oils / utils / idl.py
1 from osrf.net_obj import osrfNetworkRegisterHint
2 from osrf.log import *
3 from osrf.set import osrfSettingsValue
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     idlParser = oilsIDLParser();
13     idlParser.setIDL(osrfSettingsValue('IDL'))
14     idlParser.parseIDL()
15     __global_parser = idlParser
16
17 def oilsGetIDLParser():
18     global __global_parser
19     return __global_parser
20
21 class oilsIDLParser(object):
22
23     def __init__(self):
24         self.IDLObject = {}
25
26     def setIDL(self, file):
27         osrfLogInfo("setting IDL file to " + str(file))
28         self.idlFile = file
29
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.
34             """
35         for (k, v) in node.attributes.items():
36             if k == name:
37                 return v
38         return None
39
40     def parseIDL(self):
41         """Parses the IDL file and builds class objects"""
42
43         doc = xml.dom.minidom.parse(self.idlFile)
44         root = doc.childNodes[0]
45
46         for child in root.childNodes:
47         
48             if child.nodeType == child.ELEMENT_NODE:
49         
50                 # -----------------------------------------------------------------------
51                 # 'child' is the main class node for a fieldmapper class.
52                 # It has 'fields' and 'links' nodes as children.
53                 # -----------------------------------------------------------------------
54
55                 id = self.__getAttr(child, 'id')
56                 self.IDLObject[id] = {}
57                 obj = self.IDLObject[id]
58                 obj['fields'] = []
59
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)
65
66                 keys = []
67                 for classNode in child.childNodes:
68                     if classNode.nodeType == classNode.ELEMENT_NODE:
69                         if classNode.nodeName == 'fields':
70                             keys = self.parseFields(id, classNode)
71
72                 osrfNetworkRegisterHint(id, keys, 'array')
73
74         doc.unlink()
75
76
77     def parseFields(self, cls, fields):
78         """Takes the fields node and parses the included field elements"""
79
80         keys = []
81         idlobj = self.IDLObject[cls]
82
83         for field in fields.childNodes:
84             if field.nodeType == field.ELEMENT_NODE:
85                 keys.append(None)
86         
87         for field in fields.childNodes:
88             obj = {}
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))
92                 obj['name'] = name
93
94                 try:
95                     keys[position] = name
96                 except Exception, e:
97                     osrfLogErr("parseFields(): position out of range.  pos=%d : key-size=%d" % (position, len(keys)))
98                     raise e
99
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)
104
105                 if virtual == string.lower('true'):
106                     obj['virtual']  = True
107                 else:
108                     obj['virtual']  = False
109
110                 idlobj['fields'].append(obj)
111
112         return keys
113
114
115
116