]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/python/oils/utils/idl.py
adding oils libs
[Evergreen.git] / Open-ILS / src / python / oils / utils / idl.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16 from osrf.json import *
17 from osrf.log import *
18 from osrf.set import osrfSettingsValue
19
20 import sys, libxml2, osrf.conf, string
21 from oils.const import OILS_NS_OBJ, OILS_NS_PERSIST, OILS_NS_REPORTER
22
23 __global_parser = None
24
25 def oilsParseIDL():
26         global __global_parser
27         idlParser = oilsIDLParser();
28         idlParser.setIDL(osrfSettingsValue('IDL'))
29         idlParser.parseIDL()
30         __global_parser = idlParser
31
32 def oilsGetIDLParser():
33         global __global_parser
34         return __global_parser
35
36 class oilsIDLParser(object):
37
38         def __init__(self):
39                 self.IDLObject = {}
40
41         def setIDL(self, file):
42                 osrfLogInfo("setting IDL file to " + file)
43                 self.idlFile = file
44
45         def parseIDL(self):
46                 """Parses the IDL file and builds class objects"""
47
48                 doc     = libxml2.parseFile(self.idlFile)
49                 root    = doc.children
50                 child = root.children
51
52                 while child:
53                 
54                         if child.type == 'element':
55                 
56                                 # -----------------------------------------------------------------------
57                                 # 'child' is the main class node for a fieldmapper class.
58                                 # It has 'fields' and 'links' nodes as children.
59                                 # -----------------------------------------------------------------------
60
61                                 id = child.prop('id')
62                                 self.IDLObject[id] = {}
63                                 obj = self.IDLObject[id]
64                                 obj['fields'] = []
65
66                                 obj['controller'] = child.prop('controller')
67                                 obj['fieldmapper'] = child.nsProp('fieldmapper', OILS_NS_OBJ)
68                                 obj['virtual'] = child.nsProp('virtual', OILS_NS_PERSIST)
69                                 obj['rpt_label'] = child.nsProp('label', OILS_NS_REPORTER)
70
71                                 class_node = child.children
72                                 #osrfLogInternal("parseIDL(): parsing class %s" % id)
73                 
74                                 keys = []
75                                 while class_node:
76                                         if class_node.type == 'element':
77                                                 if class_node.name == 'fields':
78                                                         keys = self.parseFields(id, class_node)
79                                         class_node = class_node.next
80
81                                 #obj['fields'] = keys
82                                 osrfNetworkRegisterHint(id, keys, 'array' )
83
84                         child = child.next
85
86                 doc.freeDoc()
87
88
89         def parseFields(self, cls, fields):
90                 """Takes the fields node and parses the included field elements"""
91
92                 field = fields.children
93                 keys = []
94                 idlobj = self.IDLObject[cls]
95
96                 while field:
97                         if field.type == 'element':
98                                 keys.append(None)
99                         field = field.next
100                 
101                 field = fields.children
102                 while field:
103                         obj = {}
104                         if field.type == 'element':
105                                 name                    = field.prop('name')
106                                 position                = int(field.nsProp('array_position', OILS_NS_OBJ))
107                                 obj['name'] = name
108
109                                 try:
110                                         keys[position] = name
111                                 except Exception, e:
112                                         osrfLogErr("parseFields(): position out of range.  pos=%d : key-size=%d" % (position, len(keys)))
113                                         raise e
114
115                                 virtual = field.nsProp('virtual', OILS_NS_PERSIST)
116                                 obj['rpt_label']        = field.nsProp('label', OILS_NS_REPORTER)
117                                 obj['rpt_dtype']        = field.nsProp('datatype', OILS_NS_REPORTER)
118                                 obj['rpt_select']       = field.nsProp('selector', OILS_NS_REPORTER)
119
120                                 if virtual == string.lower('true'):
121                                         obj['virtual']  = True
122                                 else:
123                                         obj['virtual']  = False
124
125                                 idlobj['fields'].append(obj)
126
127                         field = field.next
128
129                 return keys
130
131
132
133