Commit 589fa12e authored by Alejandro Homs Puron's avatar Alejandro Homs Puron
Browse files

[CLIENT] Add dumps method to EnumParam, ParamBase & ParamArray:

* Provide pretty-print functionality
parent d3c48638
......@@ -49,6 +49,22 @@ def get_gpu_devs():
return glob.glob('/dev/nvidia[0-9]')
class EnumParam(Enum):
def dumps(self, **kws):
indent = kws.get('indent', None)
typename = kws.get('typename', True)
klass = self.__class__.__name__
klass_prefix = f'{klass}: ' if typename else ''
value = str(self.value)
if indent is None:
prefix, suffix = f'<{klass_prefix}', '>'
else:
prefix, suffix = klass_prefix, ''
value = value.upper()
return prefix + value + suffix
TypeData = namedtuple('Detector_TypeData', ['name', 'schema', 'klass'])
class ParamBase:
......@@ -182,14 +198,57 @@ class ParamBase:
return repr(self)
def __repr__(self):
klass = self.__class__.__name__
return f'<{klass}: {repr(dict(self.items()))}>'
return self.dumps()
def __dir__(self):
this_dir = super().__dir__()
item_dirs = [d.keys() for n, d in self._getItemDataDicts()]
return itertools.chain(this_dir, *item_dirs)
def dumps(self, **kws):
indent = kws.get('indent', None)
level = kws.setdefault('level', 0)
typename = kws.get('typename', True)
sort_keys = kws.get('sort_keys', False)
capitalize = kws.get('capitalize', False)
klass = self.__class__.__name__
klass_prefix = f'{klass}: ' if typename else ''
keys = sorted(self.keys()) if sort_keys else self.keys()
if indent is None:
prefix, suffix = f'<{klass_prefix}', '>'
sep = ', '
head_len = 0
key_len = 0
else:
prefix, suffix = klass_prefix, ''
sep = '\n'
head_len = indent * level
key_len = max([len(k) for k in keys]) + 2
def key_str(k):
if capitalize:
k = ' '.join([x.capitalize() for x in k.split('_')])
return k + ': '
item_kws = dict(kws)
item_kws['level'] += 1
def item_str(v):
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)
else:
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]))
lines = [item_line(k) for k in keys]
return prefix + sep.join(lines) + suffix
def getModelDict(self):
f = self._getToModelFunc()
return {p: f(p, v) for p, v in self.items()}
......@@ -273,6 +332,31 @@ class ParamArray(list):
klass = self.__class__
return klass(self.getModelList(), data=self.__data__)
def dumps(self, **kws):
if not len(self):
return '[]'
indent = kws.get('indent', None)
level = kws.get('level', 0)
klass = self.__data__['klass']
is_special = any([issubclass(klass, b)
for b in (EnumParam, ParamBase, ParamArray)])
if indent is None or not is_special or issubclass(klass, EnumParam):
prefix, suffix = '[', ']'
sep = ', '
else:
close_indent = level * indent
prefix, suffix = '[\n', '\n' + ' ' * close_indent + ']'
sep = ',\n'
item_kws = dict(kws)
item_kws['level'] += 1
def item_str(v):
if is_special:
return v.dumps(**item_kws)
else:
return repr(v)
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 RefName(x):
......@@ -299,7 +383,7 @@ def get_schema_type(schema, name=None, sub_schema=None, name_prefix=''):
HasEnum = lambda p: 'enum' in p
def SchemaEnum(n, items):
enum_name = f'{type_name}.{cpp_2_python_name(n)}'
return Enum(enum_name, {e.upper(): e for e in items})
return EnumParam(enum_name, {e.upper(): e for e in items})
enums = {n: SchemaEnum(n, p['enum'])
for n, p in schema_items if HasEnum(p)}
......
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