Skip to content
Snippets Groups Projects
Commit 0f9e33c2 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Format

parent cf0d0e0f
No related branches found
No related tags found
1 merge request!1First try of type hints
......@@ -18,36 +18,37 @@
# along with xAAL. If not, see <http://www.gnu.org/licenses/>.
#
import time
import logging
from tabulate import tabulate
from typing import Optional, Union, Any
import time
import typing
from typing import Any, Optional, Union
from tabulate import tabulate
from . import config
from . import tools
from . import bindings
from . import bindings, config, tools
from .exceptions import DeviceError
from .types import DeviceT, EngineT, AsyncEngineT
if typing.TYPE_CHECKING:
from .aioengine import AsyncEngine
from .devices import Device
from .engine import Engine
logger = logging.getLogger(__name__)
class Attribute(object):
def __init__(self, name, dev: Optional[DeviceT] = None, default: Any = None):
def __init__(self, name, dev: Optional["Device"] = None, default: Any = None):
self.name = name
self.default = default
self.device: Optional[Device] = dev
self.device: Optional["Device"] = dev
self.__value = default
@property
def value(self):
def value(self) -> Any:
return self.__value
@value.setter
def value(self, value):
def value(self, value: Any):
if value != self.__value and self.device:
eng = self.device.engine
if eng:
......@@ -55,7 +56,7 @@ class Attribute(object):
logger.debug("Attr change %s %s=%s" % (self.device.address, self.name, value))
self.__value = value
def __repr__(self): # pragma: no cover
def __repr__(self) -> str: # pragma: no cover
return f"<{self.__module__}.Attribute {self.name} at 0x{id(self):x}>"
......@@ -66,7 +67,7 @@ class Attributes(list):
if isinstance(value, int):
return list.__getitem__(self, value)
for k in self:
if (value == k.name):
if value == k.name:
return k.value
raise KeyError(value)
......@@ -74,11 +75,12 @@ class Attributes(list):
if isinstance(name, int):
return list.__setitem__(self, name, value)
for k in self:
if (name == k.name):
if name == k.name:
k.value = value
return
raise KeyError(name)
class Device(object):
__slots__ = [
"__dev_type",
......@@ -105,9 +107,8 @@ class Device(object):
self,
dev_type: str,
addr: Optional[bindings.UUID] = None,
engine: Union[EngineT, AsyncEngineT, None] = None,
engine: Union["Engine", "AsyncEngine", None] = None,
):
# xAAL internal attributes for a device
self.dev_type = dev_type # xaal dev_type
self.address = addr # xaal addr
......@@ -129,8 +130,10 @@ class Device(object):
self.next_alive = 0
# Default attributes & methods
self.__attributes = Attributes()
self.methods = {'get_attributes' : self._get_attributes,
'get_description': self._get_description }
self.methods = {
"get_attributes": self._get_attributes,
"get_description": self._get_description,
}
self.engine = engine
@property
......@@ -149,7 +152,7 @@ class Device(object):
@version.setter
def version(self, value: Any):
# version must be a string
# version must be a string
if value:
self.__version = "%s" % value
else:
......@@ -233,8 +236,8 @@ class Device(object):
print("= Device: %s" % self)
# info & description
r = []
r.append(['dev_type',self.dev_type])
r.append(['address',self.address])
r.append(["dev_type", self.dev_type])
r.append(["address", self.address])
for k, v in self._get_description().items():
r.append([k, v])
print(tabulate(r, tablefmt="fancy_grid"))
......@@ -253,7 +256,6 @@ class Device(object):
r.append([k, v.__name__])
print(tabulate(r, tablefmt="fancy_grid"))
def __repr__(self) -> str:
return f"<xaal.Device {id(self):x} {self.address} {self.dev_type}>"
......@@ -263,27 +265,27 @@ class Device(object):
def _get_description(self) -> dict:
result = {}
if self.vendor_id:
result['vendor_id'] = self.vendor_id
result["vendor_id"] = self.vendor_id
if self.product_id:
result['product_id'] = self.product_id
result["product_id"] = self.product_id
if self.version:
result['version'] = self.version
result["version"] = self.version
if self.url:
result['url'] = self.url
result["url"] = self.url
if self.schema:
result['schema'] = self.schema
result["schema"] = self.schema
if self.info:
result['info'] = self.info
result["info"] = self.info
if self.hw_id:
result['hw_id'] = self.hw_id
result["hw_id"] = self.hw_id
if self.group_id:
result['group_id'] = self.group_id
result["group_id"] = self.group_id
if self.unsupported_methods:
result['unsupported_methods'] = self.unsupported_methods
result["unsupported_methods"] = self.unsupported_methods
if self.unsupported_notifications:
result['unsupported_notifications'] = self.unsupported_notifications
result["unsupported_notifications"] = self.unsupported_notifications
if self.unsupported_attributes:
result['unsupported_attributes'] = self.unsupported_attributes
result["unsupported_attributes"] = self.unsupported_attributes
return result
def _get_attributes(self, _attributes=None):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment