Commit b559a29c authored by Alejandro Homs Puron's avatar Alejandro Homs Puron
Browse files

[CLIENT] Add Constants support to ParamBase:

* Predefined constants are added to ParamBase-derived class scope
* Specified fields are tested against constants in dumps
parent d0c48713
......@@ -240,18 +240,21 @@ class ParamBase:
item_kws = dict(kws)
item_kws['level'] += 1
def item_str(v):
def item_str(k):
v = self[k]
if any([isinstance(v, b) for b in (EnumParam, ParamArray)]):
return v.dumps(**kws)
elif isinstance(v, ParamBase):
key_suffix = '\n' if indent else ''
return key_suffix + v.dumps(**item_kws)
elif v in self.Constants.get(k, {}).values():
return [n for n, c in self.Constants[k].items() if c == v][0]
return repr(v)
head = '%*s' % (head_len, '')
def item_line(k):
return '%s%-*s%s' % (head, key_len, key_str(k), item_str(self[k]))
return '%s%-*s%s' % (head, key_len, key_str(k), item_str(k))
lines = [item_line(k) for k in keys]
return prefix + sep.join(lines) + suffix
......@@ -366,7 +369,8 @@ class ParamArray(list):
return prefix + sep.join([item_str(x) for x in self]) + suffix
def get_schema_type(schema, name=None, sub_schema=None, name_prefix=''):
def get_schema_type(schema, name=None, sub_schema=None, name_prefix='',
def RefName(x):
return x['$ref'].split('/')[-1]
......@@ -399,7 +403,7 @@ def get_schema_type(schema, name=None, sub_schema=None, name_prefix=''):
name = RefName(p) if '$ref' in p else n
sub_schema = p if 'properties' in p else None
prefix = f'{type_name}.'
return get_schema_type(schema, name, sub_schema, prefix)
return get_schema_type(schema, name, sub_schema, prefix, type_filter)
types = {n: SchemaType(n, p) for n, p in schema_items if HasType(p)}
HasArray = lambda p: 'items' in p
......@@ -421,9 +425,17 @@ def get_schema_type(schema, name=None, sub_schema=None, name_prefix=''):
for n, p in schema_items
if HasArray(p) and ComplexItem(p['items'])}
type_dict = dict(Model=model, Enums=enums, Types=types, Arrays=arrays)
constants = {}
type_dict = dict(Model=model, Enums=enums, Types=types, Arrays=arrays,
for d in enums, types, arrays:
type_dict.update({cpp_2_python_name(n): t for n, t in d.items()})
if type_filter:
type_name, type_dict = type_filter(type_name, type_dict)
for c in type_dict['Constants'].values():
return type(type_name, (ParamBase,), type_dict)
......@@ -435,9 +447,9 @@ def is_special_type_item(item, factory):
return isinstance(item, get_type_from_factory(factory))
def get_type_data(name, schema_json):
def get_type_data(name, schema_json, type_filter=None):
schema = json.loads(schema_json)
klass = get_schema_type(schema)
klass = get_schema_type(schema, type_filter=type_filter)
return TypeData(name, schema, klass)
......@@ -579,7 +591,7 @@ class Detector:
prop = tango_db.get_class_attribute_property(tango_class, n)
return prop[n]['schema'][0]
def GetAttrData(n):
type_data = get_type_data(n, GetSchema(n))
type_data = get_type_data(n, GetSchema(n), self._typeFilter)
json_data = json.loads(getattr(d, n))
value = type_data.klass(json_data)
return dict(type_data=type_data, value=value)
......@@ -614,6 +626,10 @@ class Detector:
desc.addDeviceAttrData(self, attr_data)
setattr(self.__class__, n, desc)
def _typeFilter(klass, type_name, type_dict):
return type_name, type_dict
def ping(self):
for d in self._getDevs():
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment