forked from mxcube/mxcubecore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HardwareRepositoryBrowser.py
587 lines (445 loc) · 21.1 KB
/
HardwareRepositoryBrowser.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
"""Gives access to the Hardware Objects contained in the Hardware Repository database
The Hardware Repository database is a set of XML files describing devices, equipments
and procedures on a beamline. Each XML file represent a Hardware Object.
The Hardware Repository module provides access to these Hardware Objects, and manages
connections to the Control Software (Spec or Taco Device Servers).
"""
__author__ = 'Matias Guijarro'
__version__ = 1.3
import logging
import gevent
import weakref
import types
import sys
import os
import stat
import time
try:
from SpecClient_gevent import SpecEventsDispatcher
from SpecClient_gevent import SpecConnectionsManager
from SpecClient_gevent import SpecWaitObject
from SpecClient_gevent import SpecClientError
except ImportError:
pass
import HardwareObjectFileParser
import BaseHardwareObjects
from dispatcher import *
_instance = None
_hwrserver = None
def addHardwareObjectsDirs(hoDirs):
if type(hoDirs) == list:
newHoDirs = list(filter(os.path.isdir, list(map(os.path.abspath, hoDirs))))
for newHoDir in newHoDirs:
if not newHoDir in sys.path:
#print 'inserted in sys.path = %s' % newHoDir
sys.path.insert(0, newHoDir)
default_local_ho_dir = os.environ.get('CUSTOM_HARDWARE_OBJECTS_PATH', '').split(os.path.pathsep)
addHardwareObjectsDirs(default_local_ho_dir)
def setHardwareRepositoryServer(hwrserver):
global _hwrserver
xml_dirs_list = list(filter(os.path.exists, hwrserver.split(os.path.pathsep)))
if xml_dirs_list:
_hwrserver = xml_dirs_list
else:
_hwrserver = hwrserver
def HardwareRepository(hwrserver = None):
"""Return the Singleton instance of the Hardware Repository."""
global _instance
if _instance is None:
if _hwrserver is None:
setHardwareRepositoryServer(hwrserver)
_instance = __HardwareRepositoryClient(_hwrserver)
return _instance
class __HardwareRepositoryClient:
"""Hardware Repository class
Warning -- should not be instanciated directly ; call the module's level HardwareRepository() function instead
"""
def __init__(self, serverAddress):
"""Constructor
serverAddress needs to be the HWR server address (host:port) or
a list of paths where to find XML files locally (when server is not in use)
"""
self.serverAddress = serverAddress
self.requiredHardwareObjects = {}
self.xml_source={}
def connect(self):
self.invalidHardwareObjects = set()
self.hardwareObjects = weakref.WeakValueDictionary()
if type(self.serverAddress)==bytes:
mngr = SpecConnectionsManager.SpecConnectionsManager()
self.server = mngr.getConnection(self.serverAddress)
with gevent.Timeout(3):
while not self.server.isSpecConnected():
time.sleep(0.5)
#SpecWaitObject.waitConnection(self.server, timeout = 3)
# in case of update of a Hardware Object, we discard it => bricks will receive a signal and can reload it
self.server.registerChannel("update", self.discardHardwareObject, dispatchMode=SpecEventsDispatcher.FIREEVENT)
else:
self.server = None
def require(self, mnemonicsList):
"""Download a list of Hardware Objects in one go"""
self.requiredHardwareObjects = {}
if not self.server:
return
try:
t0=time.time()
mnemonics = ",".join([repr(mne) for mne in mnemonicsList])
if len(mnemonics) > 0:
self.requiredHardwareObjects = SpecWaitObject.waitReply(self.server, 'send_msg_cmd_with_return' , ('xml_getall(%s)' % mnemonics, ), timeout = 3)
logging.getLogger("HWR").debug("Getting all the hardware objects took %s ms." % ((time.time()-t0)*1000))
except SpecClientError.SpecClientTimeoutError:
logging.getLogger('HWR').error("Timeout loading Hardware Objects")
except:
logging.getLogger('HWR').exception("Could not execute 'require' on Hardware Repository server")
def loadHardwareObject(self, hoName):
"""Load a Hardware Object
Parameters :
hoName -- string name of the Hardware Object to load, for example '/motors/m0'
Return :
the loaded Hardware Object, or None if it fails
"""
if self.server:
if self.server.isSpecConnected():
try:
#t0=time.time()
if hoName in self.requiredHardwareObjects:
replyDict = self.requiredHardwareObjects[hoName]
#del self.requiredHardwareObjects[hoName]
else:
replyDict = SpecWaitObject.waitReply(self.server, 'send_msg_chan_read', ('xml_get("%s")' % hoName, ), timeout = 3)
except:
logging.getLogger('HWR').exception('Could not load Hardware Object "%s"', hoName)
else:
#print 'loading %s took %s ms' % (hoName, 1000*(time.time()-t0))
try:
xmldata = replyDict['xmldata']
mtime = int(replyDict['mtime'])
except KeyError:
logging.getLogger("HWR").error("Cannot load Hardware Object %s: file does not exist.", hoName)
return
else:
logging.getLogger('HWR').error('Cannot load Hardware Object "%s" : not connected to server.', hoName)
else:
xmldata = ""
for xml_files_path in self.serverAddress:
file_name = hoName[1:] if hoName.startswith(os.path.sep) else hoName
file_path = os.path.join(xml_files_path, file_name)+os.path.extsep+"xml"
if os.path.exists(file_path):
try:
xmldata = open(file_path, "r").read()
except:
pass
break
if True:
#print xmldata
if len(xmldata) > 0:
try:
#t0 = time.time()
ho = self.parseXML(xmldata, hoName)
#print 'parsing %s took %s ms' % (hoName, (time.time()-t0)*1000)
except:
logging.getLogger("HWR").exception("Cannot parse XML file for Hardware Object %s", hoName)
else:
if ho is not None:
self.xml_source[hoName]=xmldata
dispatcher.send('hardwareObjectLoaded', hoName, self)
def hardwareObjectDeleted(name=ho.name()):
logging.getLogger("HWR").debug("%s Hardware Object has been deleted from Hardware Repository", name)
del self.hardwareObjects[name]
ho.resolveReferences()
try:
def addChannelsAndCommands(node):
#import pdb; pdb.set_trace()
if isinstance(node, BaseHardwareObjects.CommandContainer):
node._addChannelsAndCommands()
for child_node in node:
addChannelsAndCommands(child_node)
addChannelsAndCommands(ho)
except:
logging.getLogger('HWR').exception("Error while adding commands and/or channels to Hardware Object %s", hoName)
try:
ho._init()
ho.init()
except:
logging.getLogger('HWR').exception('Cannot initialize Hardware Object "%s"', hoName)
self.invalidHardwareObjects.add(ho.name())
return None
else:
if ho.name() in self.invalidHardwareObjects:
self.invalidHardwareObjects.remove(ho.name())
self.hardwareObjects[ho.name()] = ho
return ho
else:
logging.getLogger("HWR").error("Failed to load Hardware Object %s", hoName)
else:
logging.getLogger('HWR').error('Cannot load Hardware Object "%s" : file not found.', hoName)
def discardHardwareObject(self, hoName):
"""Remove a Hardware Object from the Hardware Repository
Parameters :
hoName -- the name of the Hardware Object to remove
Emitted signals :
hardwareObjectDiscarded (<object name>) -- emitted when the object has been removed
"""
try:
del self.hardwareObjects[hoName]
except KeyError:
pass
try:
self.invalidHardwareObjects.remove(hoName)
except:
pass
try:
del self.requiredHardwareObjects[hoName]
except KeyError:
pass
dispatcher.send('hardwareObjectDiscarded', hoName, self)
def parseXML(self, XMLString, hoName):
"""Load a Hardware Object from its XML string representation
Parameters :
XMLString -- the XML string
hoName -- the name of the Hardware Object to load (i.e. '/motors/m0')
Return :
the Hardware Object, or None if it fails
"""
try:
ho = HardwareObjectFileParser.parseString(XMLString, hoName)
except:
logging.getLogger('HWR').exception('Cannot parse Hardware Repository file %s', hoName)
else:
return ho
def update(self, name, updatesList):
#TODO: update without HWR server
if self.server is not None and self.server.isSpecConnected():
self.server.send_msg_cmd_with_return('xml_multiwrite("%s", "%s")' % (name, str(updatesList)))
else:
logging.getLogger('HWR').error('Cannot update Hardware Object %s : not connected to server', name)
def rewrite_xml(self, name, xml):
#TODO: rewrite without HWR server
if self.server is not None and self.server.isSpecConnected():
self.server.send_msg_cmd_with_return('xml_writefile("%s", %s)' % (name, repr(xml)))
self.xml_source[name]=xml
else:
logging.getLogger('HWR').error('Cannot update Hardware Object %s : not connected to server', name)
def __getitem__(self, item):
if item == 'equipments':
return self.getEquipments()
elif item == 'procedures':
return self.getProcedures()
elif item == 'devices':
return self.getDevices()
else:
return self.getHardwareObject(item)
raise KeyError
def getHardwareRepositoryPath(self):
if self.server:
return ""
else:
path = self.serverAddress[0]
return os.path.abspath(path)
def getHardwareRepositoryFiles(self, startdir = '/'):
#TODO: when server is not used
if not self.server:
return
try:
completeFilesList = SpecWaitObject.waitReply(self.server, 'send_msg_chan_read', ('readDirectory()', ), timeout = 3)
except:
logging.getLogger('HWR').error('Cannot retrieve Hardware Repository files list')
else:
if '__error__' in completeFilesList:
logging.getLogger('HWR').error('Error while doing Hardware Repository files list')
return
else:
for name, filename in completeFilesList.items():
if name.startswith(startdir):
yield (name, filename)
def getEquipments(self):
"""Return the list of the currently loaded Equipments Hardware Objects"""
list = []
for hoName in self.hardwareObjects:
if self.isEquipment(hoName):
list.append(self.hardwareObjects[hoName])
return list
def getProcedures(self):
"""Return the list of the currently loaded Procedures Hardware Objects"""
list = []
for hoName in self.hardwareObjects:
if self.isProcedure(hoName):
list.append(self.hardwareObjects[hoName])
return list
def getDevices(self):
"""Return the list of the currently loaded Devices Hardware Objects"""
list = []
for hoName in self.hardwareObjects:
if self.isDevice(hoName):
list.append(self.hardwareObjects[hoName])
return list
def getHardwareObject(self, objectName):
"""Return a Hardware Object given its name
If the object is not in the Hardware Repository, try to load it.
Parameters :
objectName -- the name of the Hardware Object
Return :
the required Hardware Object
"""
if not objectName.startswith("/"):
objectName="/"+objectName
try:
if objectName:
if objectName in self.invalidHardwareObjects:
return None
try:
ho = self.hardwareObjects[objectName]
except KeyError:
ho = self.loadHardwareObject(objectName)
return ho
except TypeError as err:
logging.getLogger("HWR").exception("could not get Hardware Object %s", objectName)
def getEquipment(self, equipmentName):
"""Return an Equipment given its name (see getHardwareObject())"""
return self.getHardwareObject(equipmentName)
def getDevice(self, deviceName):
"""Return a Device given its name (see getHardwareObject())"""
return self.getHardwareObject(deviceName)
def getProcedure(self, procedureName):
"""Return a Procedure given its name (see getHardwareObject())"""
return self.getHardwareObject(procedureName)
def getConnection(self, connectionName):
"""Return the Connection object for a Spec connection, given its name
Parameters :
connectionName -- a Spec version name ('host:port' string)
Return :
the corresponding SpecConnection object
"""
connectionsManager = SpecConnectionsManager.SpecConnectionsManager()
return connectionsManager.getConnection(connectionName)
def isDevice(self, name):
"""Check if a Hardware Object is a Device
Parameters :
name -- name of the Hardware Object to test
Return :
True if the Hardware Object is a Device, False otherwise
"""
try:
return isinstance(self.hardwareObjects[name], BaseHardwareObjects.Device)
except:
return False
def isProcedure(self, name):
"""Check if a Hardware Object is a Procedure
Parameters :
name -- name of the Hardware Object to test
Return :
True if the Hardware Object is a Procedure, False otherwise
"""
try:
return isinstance(self.hardwareObjects[name], BaseHardwareObjects.Procedure)
except:
return False
def isEquipment(self, name):
"""Check if a Hardware Object is an Equipment
Parameters :
name -- name of the Hardware Object to test
Return :
True if the Hardware Object is an Equipment, False otherwise
"""
try:
return isinstance(self.hardwareObjects[name], BaseHardwareObjects.Equipment)
except:
return False
def hasHardwareObject(self, name):
"""Check if the Hardware Repository contains an object
Parameters :
name -- name of the Hardware Object
Return :
True if the Hardware Object is loaded in the Hardware Repository, False otherwise
"""
return name in self.hardwareObjects
def getInfo(self, name):
"""Return a dictionary with information about the specified Hardware Object
Parameters :
name -- name of the Hardware Object
Return :
a dictionary containing information about the Hardware Object
"""
try:
ho = self.hardwareObjects[name]
except KeyError:
return {}
else:
ho_class = ho.__class__.__name__
d = { "class": ho_class,
"python module": sys.modules[ho.__module__].__file__ }
if hasattr(ho, "isReady"):
d["is ready ?"] = str(ho.isReady())
if hasattr(ho, "getCommands"):
# hardware object is a command container
d["commands"] = {}
for cmd in ho.getCommands():
if cmd.__class__.__name__ == "SpecCommand":
d["commands"][cmd.userName()] = { "type": "spec",
"version": "%s:%s" % (cmd.connection.host, cmd.connection.port or cmd.connection.scanname),
"connected ?": cmd.isSpecConnected() and "yes" or "no",
"macro or function": str(cmd.command) }
elif cmd.__class__.__name__ == "TacoCommand":
dd = { "type": "taco",
"device": cmd.deviceName }
try:
dd["imported ?"] = cmd.device.imported and "yes" or "no"
except:
dd["imported ?"] = "no, invalid Taco device"
dd["device method"] = str(cmd.command)
d["commands"][cmd.userName()] = dd
elif cmd.__class__.__name__ == "TangoCommand":
d["commands"][cmd.userName()] = { "type": "tango",
"device": cmd.deviceName,
"imported ?": cmd.device is not None and "yes" or "no, invalid Tango device",
"device method": str(cmd.command) }
d["channels"] = {}
for chan in ho.getChannels():
if chan.__class__.__name__ == "SpecChannel":
d["channels"][chan.userName()] = { "type": "spec",
"version": "%s:%s" % (chan.connection.host, chan.connection.port or chan.connection.scanname),
"connected ?": chan.isSpecConnected() and "yes" or "no",
"variable": str(chan.varName) }
elif chan.__class__.__name__ == "TangoChannel":
d["channels"][chan.userName()] = { "type": "tango",
"device": chan.deviceName,
"imported ?": chan.device is not None and "yes" or "no, invalid Tango device or attribute name",
"attribute": str(chan.attributeName) }
if "SpecMotorA" in [klass.__name__ for klass in ho.__class__.__bases__]:
d["spec version"] = ho.specVersion
d["motor mnemonic"] = ho.specName
try:
d["connected ?"] = ho.connection.isSpecConnected() and "yes" or "no"
except:
d["connected ?"] = "no"
if isinstance(ho, BaseHardwareObjects.DeviceContainer):
d["children"] = {}
for ho in ho.getDevices():
d["children"][ho.name()] = self.getInfo(ho.name())
return d
def endPolling(self):
"""Stop all pollers
Warning : should not be used directly (finalization purposes only)
"""
return
def close(self):
"""'close' the Hardware Repository
Discards all Hardware Objects
"""
self.endPolling()
self.hardwareObjects = weakref.WeakValueDictionary()
def timerEvent(self, t_ev):
try:
global _timers
func_ref = _timers[t_ev.timerId()]
func = func_ref()
if func is None:
self.killTimer(t_ev.timerId())
del _timers[t_ev.timerId()]
else:
try:
func()
except:
logging.getLogger("HWR").exception("an error occured while calling timer function")
except:
logging.getLogger("HWR").exception("an error occured inside the timerEvent")