1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
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.
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 # -----------------------------------------------------------------------
16 from osrf.json import *
17 from osrf.log import *
18 from osrf.set import osrfSettingsValue
20 import sys, libxml2, osrf.conf, string
21 from oils.const import OILS_NS_OBJ, OILS_NS_PERSIST, OILS_NS_REPORTER
23 __global_parser = None
26 global __global_parser
27 idlParser = oilsIDLParser();
28 idlParser.setIDL(osrfSettingsValue('IDL'))
30 __global_parser = idlParser
32 def oilsGetIDLParser():
33 global __global_parser
34 return __global_parser
36 class oilsIDLParser(object):
41 def setIDL(self, file):
42 osrfLogInfo("setting IDL file to " + file)
46 """Parses the IDL file and builds class objects"""
48 doc = libxml2.parseFile(self.idlFile)
54 if child.type == 'element':
56 # -----------------------------------------------------------------------
57 # 'child' is the main class node for a fieldmapper class.
58 # It has 'fields' and 'links' nodes as children.
59 # -----------------------------------------------------------------------
62 self.IDLObject[id] = {}
63 obj = self.IDLObject[id]
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)
71 class_node = child.children
72 #osrfLogInternal("parseIDL(): parsing class %s" % id)
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
82 osrfNetworkRegisterHint(id, keys, 'array' )
89 def parseFields(self, cls, fields):
90 """Takes the fields node and parses the included field elements"""
92 field = fields.children
94 idlobj = self.IDLObject[cls]
97 if field.type == 'element':
101 field = fields.children
104 if field.type == 'element':
105 name = field.prop('name')
106 position = int(field.nsProp('array_position', OILS_NS_OBJ))
110 keys[position] = name
112 osrfLogErr("parseFields(): position out of range. pos=%d : key-size=%d" % (position, len(keys)))
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)
120 if virtual == string.lower('true'):
121 obj['virtual'] = True
123 obj['virtual'] = False
125 idlobj['fields'].append(obj)