Files
controller-wisetopdataserver/python/biopro/file/user/setup.py
T
2021-12-20 14:52:55 +08:00

113 lines
2.8 KiB
Python

from typing import Dict, List
from biopro.util.json import JsonSerialize, JSON
from biopro.util.list_like import ListLike, MutableSequence
class UserSetup(JsonSerialize):
"""user experimental setup section view.
**format**
::
[setup name]
label = label
0 = conf_name[0]
1 = conf_name[1]
**json format**
::
user_setup = {
"name": str
"label": str
"configuration": [str]
}
"""
FIELD_LABEL = 'label'
__slots__ = '__name', '__section', '__configuration'
def __init__(self, name: str, section: Dict[str, str]):
self.__name = name
self.__section = section
self.__configuration: List[str] = []
if len(section) == 0:
section[self.FIELD_LABEL] = ''
else:
try:
i = 0
while True:
self.__configuration.append(section[str(i)])
i += 1
except KeyError:
pass
@property
def name(self) -> str:
return self.__name
@property
def label(self) -> str:
return self.__section.get(self.FIELD_LABEL, '')
@label.setter
def label(self, value: str):
self.__section[self.FIELD_LABEL] = value
@label.deleter
def label(self):
try:
del self.__section[self.FIELD_LABEL]
except KeyError:
pass
@property
def configuration(self) -> MutableSequence[str]:
return ListLike(self._configuration_len,
self._configuration_get,
self._configuration_add,
self._configuration_set,
self._configuration_del)
def _configuration_len(self):
return len(self.__configuration)
def _configuration_get(self, item: int):
return self.__configuration[item]
def _configuration_add(self, item: int, value: str):
self.__configuration.insert(item, value)
# overwrite section
for i, c in enumerate(self.__configuration):
self.__section[str(i)] = c
def _configuration_set(self, item: int, value: str):
self.__configuration[item] = value
self.__section[str(item)] = value
def _configuration_del(self, item: int):
t = len(self.__configuration)
del self.__configuration[item]
# overwrite section
for i, c in enumerate(self.__configuration):
self.__section[str(i)] = c
try:
del self.__section[str(t)]
except KeyError:
pass
def as_json(self) -> JSON:
# noinspection PyTypeChecker
return {
'name': self.__name,
'label': self.label,
'configuration': self.__configuration
}