106 lines
2.5 KiB
Python
106 lines
2.5 KiB
Python
from typing import Optional, Dict
|
|
|
|
from biopro.util.address import ADDRESS, address_str
|
|
from biopro.util.json import JsonSerialize, JSON
|
|
|
|
|
|
class UserDeviceAlias(JsonSerialize):
|
|
"""user device alias section view
|
|
|
|
**format**
|
|
|
|
::
|
|
|
|
[alias device_address]
|
|
name = device_name
|
|
alias = device alias
|
|
label = label
|
|
|
|
**json format**
|
|
|
|
::
|
|
|
|
user_alias = {
|
|
"alias": str
|
|
"label": str
|
|
"device_name": str
|
|
"device_address": mac_address
|
|
}
|
|
|
|
"""
|
|
|
|
FIELD_LABEL = 'label'
|
|
FIELD_ALIAS = 'alias'
|
|
FIELD_NAME = 'name'
|
|
|
|
__slots__ = '__address', '__section'
|
|
|
|
def __init__(self, address: ADDRESS, section: Dict[str, str]):
|
|
self.__address = address
|
|
self.__section = section
|
|
|
|
if len(section) == 0:
|
|
section[self.FIELD_LABEL] = ''
|
|
section[self.FIELD_NAME] = ''
|
|
section[self.FIELD_ALIAS] = ''
|
|
|
|
@property
|
|
def device_address(self) -> Optional[ADDRESS]:
|
|
return self.__address
|
|
|
|
@property
|
|
def device_name(self) -> str:
|
|
return self.__section.get(self.FIELD_NAME, '')
|
|
|
|
@device_name.setter
|
|
def device_name(self, value: str):
|
|
self.__section[self.FIELD_NAME] = value
|
|
|
|
@device_name.deleter
|
|
def device_name(self):
|
|
self.__section[self.FIELD_NAME] = ''
|
|
|
|
@property
|
|
def device_alias(self) -> str:
|
|
return self.__section.get(self.FIELD_ALIAS, '')
|
|
|
|
@device_alias.setter
|
|
def device_alias(self, value: str):
|
|
self.__section[self.FIELD_ALIAS] = value
|
|
|
|
@device_alias.deleter
|
|
def device_alias(self):
|
|
self.__section[self.FIELD_ALIAS] = ''
|
|
|
|
@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):
|
|
self.__section[self.FIELD_LABEL] = ''
|
|
|
|
def as_json(self) -> JSON:
|
|
# noinspection PyTypeChecker
|
|
return {
|
|
'device_address': self.device_address,
|
|
'device_name': self.device_name,
|
|
'alias': self.device_alias,
|
|
'label': self.label,
|
|
}
|
|
|
|
def __str__(self):
|
|
a = self.device_alias
|
|
|
|
if ' ' in a:
|
|
a = repr(a)
|
|
|
|
return '%s=%s[%s]' % (a, self.device_name, address_str(self.device_address))
|
|
|
|
def __repr__(self):
|
|
return str(self)
|