Add more type hints
parent
ad0d065b03
commit
9eb99f283b
@ -0,0 +1,4 @@
|
|||||||
|
[mypy]
|
||||||
|
check_untyped_defs = True
|
||||||
|
disallow_any_generics = True
|
||||||
|
disallow_untyped_defs = True
|
@ -1,15 +1,25 @@
|
|||||||
|
from typing import Dict, Any, TYPE_CHECKING
|
||||||
|
|
||||||
|
from pyhon.parameter.program import HonParameterProgram
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pyhon.appliance import HonAppliance
|
||||||
|
|
||||||
|
|
||||||
class ApplianceBase:
|
class ApplianceBase:
|
||||||
def __init__(self, appliance):
|
def __init__(self, appliance: "HonAppliance"):
|
||||||
self.parent = appliance
|
self.parent = appliance
|
||||||
|
|
||||||
def attributes(self, data):
|
def attributes(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
program_name = "No Program"
|
program_name = "No Program"
|
||||||
if program := int(str(data.get("parameters", {}).get("prCode", "0"))):
|
if program := int(str(data.get("parameters", {}).get("prCode", "0"))):
|
||||||
if start_cmd := self.parent.settings.get("startProgram.program"):
|
if start_cmd := self.parent.settings.get("startProgram.program"):
|
||||||
if ids := start_cmd.ids:
|
if isinstance(start_cmd, HonParameterProgram) and (
|
||||||
|
ids := start_cmd.ids
|
||||||
|
):
|
||||||
program_name = ids.get(program, program_name)
|
program_name = ids.get(program, program_name)
|
||||||
data["programName"] = program_name
|
data["programName"] = program_name
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def settings(self, settings):
|
def settings(self, settings: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
return settings
|
return settings
|
||||||
|
@ -0,0 +1,94 @@
|
|||||||
|
from typing import Dict, Any, TYPE_CHECKING, List
|
||||||
|
|
||||||
|
from pyhon.parameter.enum import HonParameterEnum
|
||||||
|
from pyhon.parameter.range import HonParameterRange
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pyhon.commands import HonCommand
|
||||||
|
|
||||||
|
|
||||||
|
def key_print(data: Any, key: str = "", start: bool = True) -> str:
|
||||||
|
result = ""
|
||||||
|
if isinstance(data, list):
|
||||||
|
for i, value in enumerate(data):
|
||||||
|
result += key_print(value, key=f"{key}.{i}", start=False)
|
||||||
|
elif isinstance(data, dict):
|
||||||
|
for k, value in sorted(data.items()):
|
||||||
|
result += key_print(value, key=k if start else f"{key}.{k}", start=False)
|
||||||
|
else:
|
||||||
|
result += f"{key}: {data}\n"
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# yaml.dump() would be done the same, but needs an additional dependency...
|
||||||
|
def pretty_print(
|
||||||
|
data: Any,
|
||||||
|
key: str = "",
|
||||||
|
intend: int = 0,
|
||||||
|
is_list: bool = False,
|
||||||
|
whitespace: str = " ",
|
||||||
|
) -> str:
|
||||||
|
result = ""
|
||||||
|
if isinstance(data, list):
|
||||||
|
if key:
|
||||||
|
result += f"{whitespace * intend}{'- ' if is_list else ''}{key}:\n"
|
||||||
|
intend += 1
|
||||||
|
for i, value in enumerate(data):
|
||||||
|
result += pretty_print(
|
||||||
|
value, intend=intend, is_list=True, whitespace=whitespace
|
||||||
|
)
|
||||||
|
elif isinstance(data, dict):
|
||||||
|
if key:
|
||||||
|
result += f"{whitespace * intend}{'- ' if is_list else ''}{key}:\n"
|
||||||
|
intend += 1
|
||||||
|
for i, (key, value) in enumerate(sorted(data.items())):
|
||||||
|
if is_list and not i:
|
||||||
|
result += pretty_print(
|
||||||
|
value, key=key, intend=intend, is_list=True, whitespace=whitespace
|
||||||
|
)
|
||||||
|
elif is_list:
|
||||||
|
result += pretty_print(
|
||||||
|
value, key=key, intend=intend + 1, whitespace=whitespace
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
result += pretty_print(
|
||||||
|
value, key=key, intend=intend, whitespace=whitespace
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
result += f"{whitespace * intend}{'- ' if is_list else ''}{key}{': ' if key else ''}{data}\n"
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def create_command(
|
||||||
|
commands: Dict[str, "HonCommand"], concat: bool = False
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
result: Dict[str, Any] = {}
|
||||||
|
for name, command in commands.items():
|
||||||
|
for parameter, data in command.available_settings.items():
|
||||||
|
if isinstance(data, HonParameterEnum):
|
||||||
|
value: List[str] | Dict[str, str | float] = data.values
|
||||||
|
elif isinstance(data, HonParameterRange):
|
||||||
|
value = {"min": data.min, "max": data.max, "step": data.step}
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
if not concat:
|
||||||
|
result.setdefault(name, {})[parameter] = value
|
||||||
|
else:
|
||||||
|
result[f"{name}.{parameter}"] = value
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def create_rules(
|
||||||
|
commands: Dict[str, "HonCommand"], concat: bool = False
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
result: Dict[str, Any] = {}
|
||||||
|
for name, command in commands.items():
|
||||||
|
for parameter, data in command.available_settings.items():
|
||||||
|
value = data.triggers
|
||||||
|
if not value:
|
||||||
|
continue
|
||||||
|
if not concat:
|
||||||
|
result.setdefault(name, {})[parameter] = value
|
||||||
|
else:
|
||||||
|
result[f"{name}.{parameter}"] = value
|
||||||
|
return result
|
@ -0,0 +1,27 @@
|
|||||||
|
from typing import Union, Any, TYPE_CHECKING, Protocol
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
from yarl import URL
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pyhon.parameter.base import HonParameter
|
||||||
|
from pyhon.parameter.enum import HonParameterEnum
|
||||||
|
from pyhon.parameter.fixed import HonParameterFixed
|
||||||
|
from pyhon.parameter.program import HonParameterProgram
|
||||||
|
from pyhon.parameter.range import HonParameterRange
|
||||||
|
|
||||||
|
|
||||||
|
class Callback(Protocol):
|
||||||
|
def __call__(
|
||||||
|
self, url: str | URL, *args: Any, **kwargs: Any
|
||||||
|
) -> aiohttp.client._RequestContextManager:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
Parameter = Union[
|
||||||
|
"HonParameter",
|
||||||
|
"HonParameterRange",
|
||||||
|
"HonParameterEnum",
|
||||||
|
"HonParameterFixed",
|
||||||
|
"HonParameterProgram",
|
||||||
|
]
|
Loading…
Reference in New Issue