From 2788a3ac917b821d36e92ce26bcd482d1d744493 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Wed, 12 Jul 2023 00:05:27 +0200 Subject: [PATCH 1/3] Reset commands to defaults --- pyhon/commands.py | 4 ++++ pyhon/parameter/base.py | 18 ++++++++++++++---- pyhon/parameter/enum.py | 13 ++++++++++--- pyhon/parameter/fixed.py | 7 ++++++- pyhon/parameter/range.py | 17 +++++++++++++---- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/pyhon/commands.py b/pyhon/commands.py index a00b123..d76c897 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -169,3 +169,7 @@ class HonCommand: else: result[name] = parameter return result + + def reset(self): + for parameter in self._parameters.values(): + parameter.reset() diff --git a/pyhon/parameter/base.py b/pyhon/parameter/base.py index 6bf81ca..397b9f0 100644 --- a/pyhon/parameter/base.py +++ b/pyhon/parameter/base.py @@ -7,14 +7,21 @@ if TYPE_CHECKING: class HonParameter: def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: self._key = key - self._category: str = attributes.get("category", "") - self._typology: str = attributes.get("typology", "") - self._mandatory: int = attributes.get("mandatory", 0) + self._attributes = attributes + self._category: str = "" + self._typology: str = "" + self._mandatory: int = 0 self._value: str | float = "" self._group: str = group self._triggers: Dict[ str, List[Tuple[Callable[["HonRule"], None], "HonRule"]] ] = {} + self._set_attributes() + + def _set_attributes(self): + self._category = self._attributes.get("category", "") + self._typology = self._attributes.get("typology", "") + self._mandatory = self._attributes.get("mandatory", 0) @property def key(self) -> str: @@ -54,7 +61,7 @@ class HonParameter: return self._group def add_trigger( - self, value: str, func: Callable[["HonRule"], None], data: "HonRule" + self, value: str, func: Callable[["HonRule"], None], data: "HonRule" ) -> None: if self._value == value: func(data) @@ -85,3 +92,6 @@ class HonParameter: param[rule.param_key] = rule.param_data.get("defaultValue", "") return result + + def reset(self): + self._set_attributes() diff --git a/pyhon/parameter/enum.py b/pyhon/parameter/enum.py index 9bc4698..45309f0 100644 --- a/pyhon/parameter/enum.py +++ b/pyhon/parameter/enum.py @@ -10,12 +10,19 @@ def clean_value(value: str | float) -> str: class HonParameterEnum(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._default = attributes.get("defaultValue") - self._value = self._default or "0" - self._values: List[str] = attributes.get("enumValues", []) + self._default = "" + self._value = "" + self._values: List[str] = [] + self._set_attributes() if self._default and clean_value(self._default.strip("[]")) not in self.values: self._values.append(self._default) + def _set_attributes(self): + super()._set_attributes() + self._default = self._attributes.get("defaultValue") + self._value = self._default or "0" + self._values: List[str] = self._attributes.get("enumValues", []) + def __repr__(self) -> str: return f"{self.__class__} (<{self.key}> {self.values})" diff --git a/pyhon/parameter/fixed.py b/pyhon/parameter/fixed.py index ceb3046..a17df93 100644 --- a/pyhon/parameter/fixed.py +++ b/pyhon/parameter/fixed.py @@ -6,7 +6,12 @@ from pyhon.parameter.base import HonParameter class HonParameterFixed(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._value = attributes.get("fixedValue", None) + self._value = None + self._set_attributes() + + def _set_attributes(self): + super()._set_attributes() + self._value = self._attributes.get("fixedValue", None) def __repr__(self) -> str: return f"{self.__class__} (<{self.key}> fixed)" diff --git a/pyhon/parameter/range.py b/pyhon/parameter/range.py index e27314e..ef0f58a 100644 --- a/pyhon/parameter/range.py +++ b/pyhon/parameter/range.py @@ -7,10 +7,19 @@ from pyhon.parameter.base import HonParameter class HonParameterRange(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._min: float = str_to_float(attributes["minimumValue"]) - self._max: float = str_to_float(attributes["maximumValue"]) - self._step: float = str_to_float(attributes["incrementValue"]) - self._default: float = str_to_float(attributes.get("defaultValue", self.min)) + self._min: float = 0 + self._max: float = 0 + self._step: float = 0 + self._default: float = 0 + self._value: float = 0 + self._set_attributes() + + def _set_attributes(self): + super()._set_attributes() + self._min: float = str_to_float(self._attributes["minimumValue"]) + self._max: float = str_to_float(self._attributes["maximumValue"]) + self._step: float = str_to_float(self._attributes["incrementValue"]) + self._default: float = str_to_float(self._attributes.get("defaultValue", self.min)) self._value: float = self._default def __repr__(self) -> str: From b0e3b15ff09397888de4dd7fd7270371c3de00f5 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Wed, 12 Jul 2023 19:36:32 +0200 Subject: [PATCH 2/3] Add pylint checks --- .github/workflows/python-check.yml | 6 +++--- .pylintrc | 7 +++++++ pyhon/__main__.py | 6 +++--- pyhon/appliances/ov.py | 1 - pyhon/command_loader.py | 7 ++++--- pyhon/commands.py | 2 +- pyhon/connection/device.py | 4 ++-- pyhon/connection/handler/base.py | 9 +++++---- pyhon/connection/handler/hon.py | 4 ++-- pyhon/parameter/base.py | 6 +++--- pyhon/parameter/enum.py | 10 +++++----- pyhon/parameter/fixed.py | 6 +++--- pyhon/parameter/program.py | 2 +- pyhon/parameter/range.py | 12 ++++++------ pyhon/typedefs.py | 2 +- setup.py | 2 +- 16 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 .pylintrc diff --git a/.github/workflows/python-check.yml b/.github/workflows/python-check.yml index 813ebd7..53a37b0 100644 --- a/.github/workflows/python-check.yml +++ b/.github/workflows/python-check.yml @@ -34,9 +34,9 @@ jobs: - name: Type check with mypy run: | mypy pyhon/ - # - name: Analysing the code with pylint - # run: | - # pylint --max-line-length 88 $(git ls-files '*.py') + - name: Analysing the code with pylint + run: | + pylint $(git ls-files '*.py') - name: Check black style run: | black . --check diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..f350fd7 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,7 @@ +[MESSAGES CONTROL] + +disable=C,R + +[FORMAT] + +max-line-length=88 diff --git a/pyhon/__main__.py b/pyhon/__main__.py index ea529c0..c6460f5 100755 --- a/pyhon/__main__.py +++ b/pyhon/__main__.py @@ -30,13 +30,13 @@ def get_arguments() -> Dict[str, Any]: export.add_argument("--zip", help="create zip archive", action="store_true") export.add_argument("--anonymous", help="anonymize data", action="store_true") export.add_argument("directory", nargs="?", default=Path().cwd()) - translate = subparser.add_parser( + translation = subparser.add_parser( "translate", help="print available translation keys" ) - translate.add_argument( + translation.add_argument( "translate", help="language (de, en, fr...)", metavar="LANGUAGE" ) - translate.add_argument("--json", help="print as json", action="store_true") + translation.add_argument("--json", help="print as json", action="store_true") parser.add_argument( "-i", "--import", help="import pyhon data", nargs="?", default=Path().cwd() ) diff --git a/pyhon/appliances/ov.py b/pyhon/appliances/ov.py index 77478cf..9ebbb87 100644 --- a/pyhon/appliances/ov.py +++ b/pyhon/appliances/ov.py @@ -1,7 +1,6 @@ from typing import Any, Dict from pyhon.appliances.base import ApplianceBase -from pyhon.parameter.program import HonParameterProgram class Appliance(ApplianceBase): diff --git a/pyhon/command_loader.py b/pyhon/command_loader.py index 578fa57..127dd08 100644 --- a/pyhon/command_loader.py +++ b/pyhon/command_loader.py @@ -1,14 +1,15 @@ import asyncio from contextlib import suppress from copy import copy -from typing import Dict, Any, Optional, TYPE_CHECKING, List, Collection +from typing import Dict, Any, Optional, TYPE_CHECKING, List from pyhon.commands import HonCommand +from pyhon.exceptions import NoAuthenticationException from pyhon.parameter.fixed import HonParameterFixed from pyhon.parameter.program import HonParameterProgram if TYPE_CHECKING: - from pyhon import HonAPI, exceptions + from pyhon import HonAPI from pyhon.appliance import HonAppliance @@ -29,7 +30,7 @@ class HonCommandLoader: def api(self) -> "HonAPI": """api connection object""" if self._api is None: - raise exceptions.NoAuthenticationException("Missing hOn login") + raise NoAuthenticationException("Missing hOn login") return self._api @property diff --git a/pyhon/commands.py b/pyhon/commands.py index d76c897..70dc74d 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -170,6 +170,6 @@ class HonCommand: result[name] = parameter return result - def reset(self): + def reset(self) -> None: for parameter in self._parameters.values(): parameter.reset() diff --git a/pyhon/connection/device.py b/pyhon/connection/device.py index 6d680c0..730ef72 100644 --- a/pyhon/connection/device.py +++ b/pyhon/connection/device.py @@ -21,7 +21,7 @@ class HonDevice: return self._os_version @property - def os(self) -> str: + def os_type(self) -> str: return self._os @property @@ -36,7 +36,7 @@ class HonDevice: result: Dict[str, str | int] = { "appVersion": self.app_version, "mobileId": self.mobile_id, - "os": self.os, + "os": self.os_type, "osVersion": self.os_version, "deviceModel": self.device_model, } diff --git a/pyhon/connection/handler/base.py b/pyhon/connection/handler/base.py index efba820..f06053e 100644 --- a/pyhon/connection/handler/base.py +++ b/pyhon/connection/handler/base.py @@ -1,8 +1,8 @@ import logging from collections.abc import AsyncIterator -from contextlib import asynccontextmanager +from contextlib import asynccontextmanager, _AsyncGeneratorContextManager from types import TracebackType -from typing import Optional, Dict, Type, Any, Protocol +from typing import Optional, Dict, Type, Any, Callable, Coroutine, AsyncGenerator import aiohttp from typing_extensions import Self @@ -47,10 +47,11 @@ class ConnectionHandler: return self @asynccontextmanager - def _intercept( + async def _intercept( self, method: Callback, url: str | URL, *args: Any, **kwargs: Dict[str, Any] ) -> AsyncIterator[aiohttp.ClientResponse]: - raise NotImplementedError + async with method(url, *args, **kwargs) as response: + yield response @asynccontextmanager async def get( diff --git a/pyhon/connection/handler/hon.py b/pyhon/connection/handler/hon.py index 5b7692f..8fbeca9 100644 --- a/pyhon/connection/handler/hon.py +++ b/pyhon/connection/handler/hon.py @@ -95,11 +95,11 @@ class HonConnectionHandler(ConnectionHandler): try: await response.json() yield response - except json.JSONDecodeError: + except json.JSONDecodeError as exc: _LOGGER.warning( "%s - JsonDecodeError %s - %s", response.request_info.url, response.status, await response.text(), ) - raise HonAuthenticationError("Decode Error") + raise HonAuthenticationError("Decode Error") from exc diff --git a/pyhon/parameter/base.py b/pyhon/parameter/base.py index 397b9f0..4d70051 100644 --- a/pyhon/parameter/base.py +++ b/pyhon/parameter/base.py @@ -18,7 +18,7 @@ class HonParameter: ] = {} self._set_attributes() - def _set_attributes(self): + def _set_attributes(self) -> None: self._category = self._attributes.get("category", "") self._typology = self._attributes.get("typology", "") self._mandatory = self._attributes.get("mandatory", 0) @@ -61,7 +61,7 @@ class HonParameter: return self._group def add_trigger( - self, value: str, func: Callable[["HonRule"], None], data: "HonRule" + self, value: str, func: Callable[["HonRule"], None], data: "HonRule" ) -> None: if self._value == value: func(data) @@ -93,5 +93,5 @@ class HonParameter: return result - def reset(self): + def reset(self) -> None: self._set_attributes() diff --git a/pyhon/parameter/enum.py b/pyhon/parameter/enum.py index 45309f0..2d49b7c 100644 --- a/pyhon/parameter/enum.py +++ b/pyhon/parameter/enum.py @@ -10,18 +10,18 @@ def clean_value(value: str | float) -> str: class HonParameterEnum(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._default = "" - self._value = "" + self._default: str | float = "" + self._value: str | float = "" self._values: List[str] = [] self._set_attributes() if self._default and clean_value(self._default.strip("[]")) not in self.values: self._values.append(self._default) - def _set_attributes(self): + def _set_attributes(self) -> None: super()._set_attributes() - self._default = self._attributes.get("defaultValue") + self._default = self._attributes.get("defaultValue", "") self._value = self._default or "0" - self._values: List[str] = self._attributes.get("enumValues", []) + self._values = self._attributes.get("enumValues", []) def __repr__(self) -> str: return f"{self.__class__} (<{self.key}> {self.values})" diff --git a/pyhon/parameter/fixed.py b/pyhon/parameter/fixed.py index a17df93..125ecd2 100644 --- a/pyhon/parameter/fixed.py +++ b/pyhon/parameter/fixed.py @@ -6,12 +6,12 @@ from pyhon.parameter.base import HonParameter class HonParameterFixed(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._value = None + self._value: str | float = "" self._set_attributes() - def _set_attributes(self): + def _set_attributes(self) -> None: super()._set_attributes() - self._value = self._attributes.get("fixedValue", None) + self._value = self._attributes.get("fixedValue", "") def __repr__(self) -> str: return f"{self.__class__} (<{self.key}> fixed)" diff --git a/pyhon/parameter/program.py b/pyhon/parameter/program.py index 2925bd2..4ed2004 100644 --- a/pyhon/parameter/program.py +++ b/pyhon/parameter/program.py @@ -37,7 +37,7 @@ class HonParameterProgram(HonParameterEnum): @values.setter def values(self, values: List[str]) -> None: - return + raise ValueError("Cant set values {values}") @property def ids(self) -> Dict[int, str]: diff --git a/pyhon/parameter/range.py b/pyhon/parameter/range.py index ef0f58a..c51642c 100644 --- a/pyhon/parameter/range.py +++ b/pyhon/parameter/range.py @@ -14,13 +14,13 @@ class HonParameterRange(HonParameter): self._value: float = 0 self._set_attributes() - def _set_attributes(self): + def _set_attributes(self) -> None: super()._set_attributes() - self._min: float = str_to_float(self._attributes["minimumValue"]) - self._max: float = str_to_float(self._attributes["maximumValue"]) - self._step: float = str_to_float(self._attributes["incrementValue"]) - self._default: float = str_to_float(self._attributes.get("defaultValue", self.min)) - self._value: float = self._default + self._min = str_to_float(self._attributes["minimumValue"]) + self._max = str_to_float(self._attributes["maximumValue"]) + self._step = str_to_float(self._attributes["incrementValue"]) + self._default = str_to_float(self._attributes.get("defaultValue", self.min)) + self._value = self._default def __repr__(self) -> str: return f"{self.__class__} (<{self.key}> [{self.min} - {self.max}])" diff --git a/pyhon/typedefs.py b/pyhon/typedefs.py index 2a22319..c6594e5 100644 --- a/pyhon/typedefs.py +++ b/pyhon/typedefs.py @@ -1,4 +1,4 @@ -from typing import Union, Any, TYPE_CHECKING, Protocol +from typing import Union, Any, TYPE_CHECKING, Protocol, AsyncIterator, Coroutine import aiohttp from yarl import URL diff --git a/setup.py b/setup.py index 0b5a130..5ee66ab 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages -with open("README.md", "r") as f: +with open("README.md", "r", encoding="utf-8") as f: long_description = f.read() setup( From 442e7a07ddb8aae814af8ee7a08f0545cc92be3f Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Fri, 14 Jul 2023 00:40:48 +0200 Subject: [PATCH 3/3] Fix pylint check --- pyhon/connection/handler/base.py | 4 ++-- pyhon/typedefs.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyhon/connection/handler/base.py b/pyhon/connection/handler/base.py index f06053e..9e5c77b 100644 --- a/pyhon/connection/handler/base.py +++ b/pyhon/connection/handler/base.py @@ -1,8 +1,8 @@ import logging from collections.abc import AsyncIterator -from contextlib import asynccontextmanager, _AsyncGeneratorContextManager +from contextlib import asynccontextmanager from types import TracebackType -from typing import Optional, Dict, Type, Any, Callable, Coroutine, AsyncGenerator +from typing import Optional, Dict, Type, Any import aiohttp from typing_extensions import Self diff --git a/pyhon/typedefs.py b/pyhon/typedefs.py index c6594e5..2a22319 100644 --- a/pyhon/typedefs.py +++ b/pyhon/typedefs.py @@ -1,4 +1,4 @@ -from typing import Union, Any, TYPE_CHECKING, Protocol, AsyncIterator, Coroutine +from typing import Union, Any, TYPE_CHECKING, Protocol import aiohttp from yarl import URL