2022-08-08 13:43:19 +01:00
|
|
|
from enum import IntEnum
|
2022-07-09 12:26:53 +01:00
|
|
|
from typing import Iterator
|
|
|
|
|
|
|
|
|
2022-04-29 02:57:47 +01:00
|
|
|
def cache_bool(func, param):
|
|
|
|
"""Check cache for a bool prop"""
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
self, *rem = args
|
2023-08-07 17:38:37 +01:00
|
|
|
if self._cmd(param) in self._remote.cache:
|
|
|
|
return self._remote.cache.pop(self._cmd(param)) == 1
|
2022-10-19 14:20:23 +01:00
|
|
|
if self._remote.sync:
|
|
|
|
self._remote.clear_dirty()
|
2022-04-29 02:57:47 +01:00
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def cache_string(func, param):
|
|
|
|
"""Check cache for a string prop"""
|
2022-03-26 23:00:09 +00:00
|
|
|
|
2022-03-20 12:25:50 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
2022-04-29 02:57:47 +01:00
|
|
|
self, *rem = args
|
2023-08-07 17:38:37 +01:00
|
|
|
if self._cmd(param) in self._remote.cache:
|
|
|
|
return self._remote.cache.pop(self._cmd(param))
|
2022-10-19 14:20:23 +01:00
|
|
|
if self._remote.sync:
|
|
|
|
self._remote.clear_dirty()
|
2022-04-29 02:57:47 +01:00
|
|
|
return func(*args, **kwargs)
|
2022-03-26 23:00:09 +00:00
|
|
|
|
|
|
|
return wrapper
|
2022-04-27 14:50:48 +01:00
|
|
|
|
|
|
|
|
2022-04-29 21:53:44 +01:00
|
|
|
def depth(d):
|
|
|
|
if isinstance(d, dict):
|
|
|
|
return 1 + (max(map(depth, d.values())) if d else 0)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2022-04-27 14:50:48 +01:00
|
|
|
def script(func):
|
|
|
|
"""Convert dictionary to script"""
|
|
|
|
|
|
|
|
def wrapper(*args):
|
|
|
|
remote, script = args
|
|
|
|
if isinstance(script, dict):
|
|
|
|
params = ""
|
|
|
|
for key, val in script.items():
|
|
|
|
obj, m2, *rem = key.split("-")
|
|
|
|
index = int(m2) if m2.isnumeric() else int(*rem)
|
|
|
|
params += ";".join(
|
|
|
|
f"{obj}{f'.{m2}stream' if not m2.isnumeric() else ''}[{index}].{k}={int(v) if isinstance(v, bool) else v}"
|
|
|
|
for k, v in val.items()
|
|
|
|
)
|
|
|
|
params += ";"
|
|
|
|
script = params
|
|
|
|
return func(remote, script)
|
|
|
|
|
|
|
|
return wrapper
|
2022-07-09 12:26:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def comp(t0: tuple, t1: tuple) -> Iterator[bool]:
|
|
|
|
"""
|
|
|
|
Generator function, accepts two tuples.
|
|
|
|
|
|
|
|
Evaluates equality of each member in both tuples.
|
|
|
|
"""
|
2022-10-04 15:42:36 +01:00
|
|
|
|
2022-07-09 12:26:53 +01:00
|
|
|
for a, b in zip(t0, t1):
|
2022-10-05 22:54:26 +01:00
|
|
|
if ((1 << 16) - 1) - b <= 7200:
|
2022-07-09 12:26:53 +01:00
|
|
|
yield a == b
|
2022-10-04 15:42:36 +01:00
|
|
|
else:
|
|
|
|
yield True
|
2022-08-08 13:43:19 +01:00
|
|
|
|
|
|
|
|
2023-07-12 04:52:50 +01:00
|
|
|
def deep_merge(dict1, dict2):
|
|
|
|
"""Generator function for deep merging two dicts"""
|
|
|
|
for k in set(dict1) | set(dict2):
|
|
|
|
if k in dict1 and k in dict2:
|
|
|
|
if isinstance(dict1[k], dict) and isinstance(dict2[k], dict):
|
|
|
|
yield k, dict(deep_merge(dict1[k], dict2[k]))
|
|
|
|
else:
|
|
|
|
yield k, dict2[k]
|
|
|
|
elif k in dict1:
|
|
|
|
yield k, dict1[k]
|
|
|
|
else:
|
|
|
|
yield k, dict2[k]
|
|
|
|
|
|
|
|
|
2022-08-08 13:43:19 +01:00
|
|
|
Socket = IntEnum("Socket", "register request response", start=0)
|