Commit 69ab9c0a authored by Sylvain Guieu's avatar Sylvain Guieu

cosmetic and renew field after selections

parent 5c43161c
......@@ -114,6 +114,7 @@ sw_control_master:
xls_col_name: SW control master
sw_device:
xls_col_name: SW device on instrument SW (WP31)
ftype: 'str_choice'
sw_supervision:
xls_col_name: Supervision SW
......
from ..db import filters
from .io import get_ftype
from . import keys as K
FTYPE= 'ftype'
def filter_device(devices, field, filter):
if hasattr(filter, "check"):
checker = filter.check
......@@ -42,3 +45,32 @@ def _filter_fields(devices, fields, checker):
def clone_fields(fields):
""" return raw fields according to fileds dictionay input
The new field can be used for new data
"""
new_fields = {}
for field_name, field in fields.items():
ftype = field.get(FTYPE, 'str')
field[FTYPE] = ftype
ftype_cls = get_ftype(ftype)
ftype_obj = ftype_cls()
new_field = ftype_obj.init_field(field_name, dict(field))
new_fields[field_name] = new_field
return new_fields
def renew_fields(devices, fields):
for device_name, device in devices.items():
for field_name, field in fields.items():
try:
property = device[field_name]
except KeyError:
raise KeyError(field_name)
property = device[field_name]
parser= field[K.PARSER]
parser(field, property[K.VALUE])
\ No newline at end of file
......@@ -6,6 +6,26 @@ from . import api
from . import ioxls
from . import keys as K
# if true the fields property are renew from new data when filter is used
AUTO_RENEW = True
def _get_field(fields, name):
""" general function to get field
maybe we may want to create an field on the fly if not exists
"""
return fields[name]
if AUTO_RENEW:
def _renewed_fields(data, fields):
newfields = api.clone_fields(fields)
api.renew_fields(data, newfields)
return newfields
else:
def _renewed_fields(data, fields):
return fields
class _FieldsHandler:
def fields(self):
""" iter on fields
......@@ -82,15 +102,16 @@ class _DevicesHandler:
data = {}
for field, filter in zip(args[::2], args[1::2]):
data.update( api.filter_device(self._data, field, filter) )
return Devices(data, self._fields)
return Devices(data, _renewed_fields(data, self._fields))
def filter_device_and(self, *args):
if len(args)%2:
raise ValueError('expecting a even number of argument got %d'%len(args))
data = self._data
for field, filter in zip(args[::2], args[1::2]):
data = api.filter_device(data, field, filter)
return Devices(data, self._fields)
data = api.filter_device(data, field, filter)
return Devices(data, _renewed_fields(data, self._fields))
......@@ -216,7 +237,7 @@ class Device(_FieldsHandler):
each iteration return a DeviceProperty object
"""
for field_name in self.field_keys():
yield DeviceProperty(self._data[field_name])
yield DeviceProperty(self._data[field_name], _get_field(self._fields,field_name))
def property_keys(self):
""" iter on value property keys
......@@ -232,7 +253,7 @@ class Device(_FieldsHandler):
each iteration return a str key / DeviceProperty object pair
"""
for field_name in self.field_keys():
yield field_name, DeviceProperty(self._data[field_name])
yield field_name, DeviceProperty(self._data[field_name], _get_field(self._fields,field_name))
def get_property(self, field_name):
try:
......@@ -240,7 +261,7 @@ class Device(_FieldsHandler):
except KeyError:
raise ValueError('%r is not a property of device')
return DeviceProperty(property_dict)
return DeviceProperty(property_dict, _get_field(self._fields,field_name))
def __getattr__(self, attr):
try:
......@@ -271,13 +292,14 @@ class Device(_FieldsHandler):
return self._fields.keys()
class DeviceProperty:
def __init__(self, property_dict):
def __init__(self, property_dict, field_dict):
self._data = property_dict
self._field = field_dict
@property
def field(self):
return Field(self._data[K.FIELD])
return Field(self._field)
@property
def value(self):
return self._data[K.VALUE]
......
......@@ -137,7 +137,7 @@ def read_device_list(sheet, header_row=1, key_col='A', columns=None, rows=None,
for col, field in header.items():
parser = field[K.PARSER]
cell = '%s%d'%(col, row)
line[field['name']] = { K.VALUE: parser(field, sheet[cell].value), K.FIELD:field, K.CELL:cell }
line[field['name']] = { K.VALUE: parser(field, sheet[cell].value), K.FIELD:col, K.CELL:cell }
#line['xls_row'] = row
return data, fields
......@@ -7,7 +7,7 @@ import sys
from setuptools import setup
name = "hrmtools"
version='0.1.dev4' # https://www.python.org/dev/peps/pep-0440/
version='0.1.dev5' # https://www.python.org/dev/peps/pep-0440/
author='Sylvain Guieu'
author_email='sylvain.guieu@univ-grenoble-alpes.fr'
install_requires = ['PyYAML', 'openpyxl>=3.0']
......
Markdown is supported
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