forked from mxcube/mxcubecore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardwareObjectFileParser.py
372 lines (285 loc) · 12.1 KB
/
HardwareObjectFileParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import imp
import logging
import xml.sax
from xml.sax.handler import ContentHandler
import BaseHardwareObjects
currentXML = None
try:
newObjectsClasses = { 'equipment': BaseHardwareObjects.Equipment,
'device': BaseHardwareObjects.Device,
'procedure': BaseHardwareObjects.Procedure }
except AttributeError:
pass
def parse(filename, name):
curHandler = HardwareObjectHandler(name)
global currentXML
try:
f = open(filename)
currentXML = f.read()
except:
currentXML = None
xml.sax.parse(filename, curHandler)
return curHandler.getHardwareObject()
def parseString(XMLHardwareObject, name):
global currentXML
currentXML = XMLHardwareObject
curHandler = HardwareObjectHandler(name)
# LNLS
#python2.7
#xml.sax.parseString(XMLHardwareObject, curHandler)
# python3.4
xml.sax.parseString(str.encode(XMLHardwareObject), curHandler)
return curHandler.getHardwareObject()
def loadModule(hardwareObjectName):
return __import__(hardwareObjectName, globals(), locals(), [""])
def instanciateClass(moduleName, className, objectName):
module = loadModule(moduleName)
if module is None:
return
else:
try:
classObj = getattr(module, className)
except AttributeError:
logging.getLogger("HWR").error('No class %s in module %s', className, moduleName)
else:
# check the XML
if module.__doc__ is not None and currentXML is not None:
i = module.__doc__.find('template:')
if i >= 0:
XMLTemplate = module.__doc__[i+10:]
xmlStructureRetriever = XMLStructureRetriever()
xml.sax.parseString(currentXML, xmlStructureRetriever)
currentStructure = xmlStructureRetriever.getStructure()
xmlStructureRetriever = XMLStructureRetriever()
xml.sax.parseString(XMLTemplate, xmlStructureRetriever)
templateStructure = xmlStructureRetriever.getStructure()
if not templateStructure == currentStructure:
logging.getLogger("HWR").error('%s: XML file does not match the %s class template' % (objectName, className))
return
try:
newInstance = classObj(objectName)
except:
logging.getLogger("HWR").exception('Cannot instanciate class %s', className)
else:
return newInstance
class HardwareObjectHandler(ContentHandler):
def __init__(self, name):
ContentHandler.__init__(self)
self.name = name
self.classError = False
self.objects = []
self.reference = ''
self.property = ''
self.elementIsAReference = False
self.elementRole = None
self.buffer = ''
self.path = ''
self.previousPath = ''
self.hwr_import_reference = None
def getHardwareObject(self):
if self.hwr_import_reference is not None:
return self.hwr_import_reference
elif len(self.objects) == 1:
return self.objects[0]
def startElement(self, name, attrs):
if self.classError:
return
self.buffer = ''
if len(self.objects) == 0:
objectName = self.name
else:
objectName = name
assert not self.elementIsAReference
self.elementRole = None
self.property = ''
self.command = {}
self.channel = {}
#
# determine path to the new object
#
self.path += '/' + str(name) + '[%d]'
i = self.previousPath.rfind('[')
if i >= 0 and self.path[:-4] == self.previousPath[:i]:
objectIndex = int(self.previousPath[i+1:-1]) + 1
else:
objectIndex = 1 #XPath indexes begin at 1
self.path %= objectIndex
_attrs = attrs
attrs = {}
for k in list(_attrs.keys()):
v = str(_attrs[k])
if v=="None":
attrs[str(k)]=None
else:
try:
attrs[str(k)]=int(v)
except:
try:
attrs[str(k)]=float(v)
except:
if v=="False":
attrs[str(k)]=False
elif v=="True":
attrs[str(k)]=True
else:
attrs[str(k)]=v
if name == "hwr_import":
self.hwr_import_reference = attrs["href"]
if 'role' in attrs:
self.elementRole = attrs['role']
if name == 'device':
# maybe we have to add the DeviceContainer mix-in class to each node of the Hardware Object hierarchy
i = len(self.objects) - 1
while i >= 0 and not isinstance(self.objects[i], BaseHardwareObjects.DeviceContainer):
#newClass = new.classobj("toto", (self.objects[i].__class__,) + self.objects[i].__class__.__bases__ + (BaseHardwareObjects.DeviceContainer, ), {})
self.objects[i].__class__ = BaseHardwareObjects.DeviceContainerNode
i -= 1
#
# is element a reference to another hardware object ?
#
ref = 'hwrid' in attrs and attrs['hwrid'] or 'href' in attrs and attrs['href']
if ref:
self.elementIsAReference = True
self.reference = str(ref)
if self.reference.startswith('../'):
self.reference = '/'.join(self.name.split('/')[:-1] + [self.reference[3:]])
elif self.reference.startswith('./'):
self.reference = '/'.join(self.name.split('/')[:-1] + [self.reference[2:]])
return
if name in newObjectsClasses:
if 'class' in attrs:
moduleName = str(attrs['class'])
className = moduleName.split('.')[-1]
newObject = instanciateClass(moduleName, className, objectName)
if newObject is None:
self.classError = True
return
else:
newObject.setPath(self.path)
self.objects.append(newObject)
else:
newObjectClass = newObjectsClasses[name]
newObject = newObjectClass(objectName)
newObject.setPath(self.path)
self.objects.append(newObject)
elif name == 'command':
if 'name' in attrs and 'type' in attrs:
#short command notation
self.command.update(attrs)
else:
#long command notation (allow arguments)
self.objects.append(BaseHardwareObjects.HardwareObjectNode(objectName))
elif name == 'channel':
if 'name' in attrs and 'type' in attrs:
self.channel.update(attrs)
else:
if len(self.objects) == 0:
if 'class' in attrs:
moduleName = str(attrs['class'])
className = moduleName.split('.')[-1]
newObject = instanciateClass(moduleName, className, objectName)
if newObject is None:
self.classError = True
return
else:
newObject = BaseHardwareObjects.HardwareObject(objectName)
newObject.setPath(self.path)
self.objects.append(newObject)
"""
# maybe we can create a HardwareObject ? be strict for the moment...
logging.getLogger("HWR").error("%s: unknown Hardware Object type (should be one of %s)", objectName, str(newObjectsClasses.keys()))
self.classError = True
return
"""
else:
newObject = BaseHardwareObjects.HardwareObjectNode(objectName)
newObject.setPath(self.path)
self.objects.append(newObject)
self.property = name #element is supposed to be a Property
def characters(self, content):
if self.classError:
return
self.buffer += str(content)
def endElement(self, name):
if self.classError:
return
name = str(name)
if self.elementIsAReference:
if len(self.objects) > 0:
self.objects[-1].addReference(name, self.reference, role=self.elementRole)
else:
try:
if name == 'command':
if len(self.command) > 0:
if len(self.objects) > 0:
self.objects[-1].addCommand(self.command, self.buffer, addNow=False)
else:
if len(self.objects) > 1:
self.objects[-2].addCommand(self.objects.pop(), addNow=False)
elif name == 'channel':
if len(self.channel) > 0:
if len(self.objects) > 0:
self.objects[-1].addChannel(self.channel, self.buffer, addNow=False)
elif name == self.property:
del self.objects[-1] #remove empty object
self.objects[-1].setProperty(name, self.buffer)
else:
if len(self.objects) == 1:
return
if len(self.objects) > 1:
self.objects[-2].addObject(name, self.objects[-1], role = self.elementRole)
if len(self.objects) > 0:
del self.objects[-1]
except:
logging.getLogger("HWR").exception("%s: error while creating Hardware Object from XML file", self.name)
self.elementIsAReference = False
self.elementRole = None
self.buffer = ''
self.previousPath = self.path
self.path = self.path[:self.path.rfind('/')] #remove last added name and suffix
class XMLStructure:
def __init__(self):
self.xmlpaths = set()
self.attributes = {}
def add(self, xmlpath, attributesSet):
self.xmlpaths.add(xmlpath)
if len(attributesSet) > 0:
self.attributes[xmlpath] = attributesSet
def __eq__(self, s):
if self.xmlpaths.issubset(s.xmlpaths):
for xmlpath, attributeSet in self.attributes.items():
try:
attributeSet2 = s.attributes[xmlpath]
except KeyError:
return False
else:
if not attributeSet.issubset(attributeSet2):
return False
return True
else:
return False
class XMLStructureRetriever(ContentHandler):
def __init__(self):
ContentHandler.__init__(self)
self.path = ''
self.previousPath = ''
self.currentAttributes = set()
self.structure = XMLStructure()
def getStructure(self):
return self.structure
def startElement(self, name, attrs):
self.path += '/' + str(name) + '[%d]'
i = self.previousPath.rfind('[')
if i >= 0 and self.path[:-4] == self.previousPath[:i]:
index = int(self.previousPath[i+1:-1]) + 1
else:
index = 1 #XPath indexes begin at 1
self.path %= index
for attr, value in list(attrs.items()):
if str(attr) == 'hwrid':
attr = 'href'
self.currentAttributes.add("%s=%s" % (str(attr), str(value)))
def endElement(self, name):
self.structure.add(self.path, self.currentAttributes)
self.previousPath = self.path
self.path = self.path[:self.path.rfind('/')]