...
  View open merge request
Commits (1)
import inspect
import re
from Lima import Core
# Convert mixedCase to under_scored
def mc2us(x): # capwords to underscore notation
return re.sub(r'(?<=[a-z])[A-Z]|(?<!^)[A-Z](?=[a-z])', r"_\g<0>", x).lower()
# Define an extended CtControl that merges every subcontrols into one class and
# can be used this way:
# ct.sav_set_saving_parameters({
# 'directory': '/tmp/output',
# 'prefix': 'testeiger',
# 'suffix': '.h5',
# 'fileFormat': Core.CtSaving.HDF5,
# 'savingMode': Core.CtSaving.AutoFrame,
# })
# ct.acq_set_acq_expo_time(0.1)
# ct.acq_set_acq_nb_frames(10)
# ct.img_set_bin(Core.Bin(2, 2))
#
# This is usefull for RPC applications where a single object is served
class CtControlExt(Core.CtControl):
def __init__(self, hw):
super(CtControlExt, self).__init__(hw)
self.bind_subcontrol(self.accumulation(), 'acc_')
self.bind_subcontrol(self.acquisition(), 'acq_')
self.bind_subcontrol(self.buffer(), 'buf_')
self.bind_subcontrol(self.config(), 'cfg_')
self.bind_subcontrol(self.event(), 'evt_')
self.bind_subcontrol(self.image(), 'img_')
self.bind_subcontrol(self.saving(), 'sav_')
self.bind_subcontrol(self.shutter(), 'shu_')
self.bind_subcontrol(self.video(), 'vid_')
def bind_subcontrol(self, subcontrol, prefix):
# Predicate to filter methods from our class
ismethod = lambda obj: inspect.isbuiltin(obj) and not obj.__name__.startswith('_')
# For every method of the subcontrols, add a new method to CtControlExt
for name, method in inspect.getmembers(type(subcontrol), ismethod):
new_name = mc2us(prefix + name)
closure = (lambda method=method: lambda self, *args, **kwargs: method(subcontrol, *args, **kwargs))()
setattr(CtControlExt, new_name, closure)
# Let the user provide a python dict rather than a Core.CtSaving.Parameters object
def sav_set_saving_parameters(self, params):
p = self.saving().getParameters()
for key in params:
setattr(p, key, params[key])
self.saving().setParameters(p)