Update vbancmd.py

add ratelimit kwarg. applies only to multiset operations (apply, profile_apply).
This commit is contained in:
onyx-and-iris 2022-04-26 22:47:48 +01:00
parent 7fb026ca1e
commit c6b9fa6112

View File

@ -29,6 +29,7 @@ class VbanCmd(abc.ABC):
self._channel = kwargs["channel"] self._channel = kwargs["channel"]
self._delay = kwargs["delay"] self._delay = kwargs["delay"]
self._sync = kwargs["sync"] self._sync = kwargs["sync"]
self._ratelimit = kwargs["ratelimit"]
# fmt: off # fmt: off
self._bps_opts = [ self._bps_opts = [
0, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 31250, 0, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 31250,
@ -66,6 +67,7 @@ class VbanCmd(abc.ABC):
self.running = True self.running = True
self._pdirty = False self._pdirty = False
self.cache = {} self.cache = {}
self.in_apply = False
def __enter__(self): def __enter__(self):
self.login() self.login()
@ -203,6 +205,8 @@ class VbanCmd(abc.ABC):
self.cache[f"{id_}.{param}"] = val self.cache[f"{id_}.{param}"] = val
if self._sync: if self._sync:
sleep(self._delay) sleep(self._delay)
if self.in_apply:
sleep(self._ratelimit)
def sendtext(self, cmd): def sendtext(self, cmd):
"""Sends a multiple parameter string over a network.""" """Sends a multiple parameter string over a network."""
@ -237,6 +241,7 @@ class VbanCmd(abc.ABC):
def apply(self, mapping: dict): def apply(self, mapping: dict):
"""Sets all parameters of a di""" """Sets all parameters of a di"""
self.in_apply = True
for key, submapping in mapping.items(): for key, submapping in mapping.items():
obj, index = key.split("-") obj, index = key.split("-")
@ -247,8 +252,8 @@ class VbanCmd(abc.ABC):
else: else:
raise ValueError(obj) raise ValueError(obj)
target.apply(submapping) target.apply(submapping)
if not self._sync: sleep(self._ratelimit)
sleep(self._delay * 30) self.in_apply = False
def apply_profile(self, name: str): def apply_profile(self, name: str):
try: try:
@ -258,7 +263,7 @@ class VbanCmd(abc.ABC):
del profile["extends"] del profile["extends"]
for key in profile.keys(): for key in profile.keys():
if key in base: if key in base:
base[key] |= profile[key] base[key] = base[key] | profile[key]
else: else:
base[key] = profile[key] base[key] = profile[key]
profile = base profile = base
@ -314,6 +319,7 @@ def _make_remote(kind: NamedTuple) -> VbanCmd:
"channel": 0, "channel": 0,
"delay": 0.001, "delay": 0.001,
"sync": False, "sync": False,
"ratelimit": 0.025,
} }
kwargs = defaultkwargs | kwargs kwargs = defaultkwargs | kwargs
VbanCmd.__init__(self, **kwargs) VbanCmd.__init__(self, **kwargs)