voicemeeter-api-python/voicemeeterlib/iremote.py

60 lines
1.7 KiB
Python
Raw Normal View History

import logging
2022-06-16 14:07:12 +01:00
import time
from abc import ABCMeta, abstractmethod
logger = logging.getLogger(__name__)
2022-06-16 14:07:12 +01:00
class IRemote(metaclass=ABCMeta):
"""
Common interface between base class and extended (higher) classes
Provides some default implementation
"""
def __init__(self, remote, index=None):
self._remote = remote
self.index = index
self.logger = logger.getChild(self.__class__.__name__)
2022-06-16 14:07:12 +01:00
def getter(self, param, **kwargs):
"""Gets a parameter value"""
self.logger.debug(f"getter: {self._cmd(param)}")
return self._remote.get(self._cmd(param), **kwargs)
2022-06-16 14:07:12 +01:00
def setter(self, param, val):
"""Sets a parameter value"""
self.logger.debug(f"setter: {self._cmd(param)}={val}")
self._remote.set(self._cmd(param), val)
def _cmd(self, param):
cmd = (self.identifier,)
if param:
cmd += (f".{param}",)
return "".join(cmd)
2022-06-16 14:07:12 +01:00
@abstractmethod
def identifier(self):
pass
def apply(self, data: dict):
def fget(attr, val):
if attr == "mode":
return (getattr(self, attr), val, 1)
return (self, attr, val)
2022-06-16 14:07:12 +01:00
for attr, val in data.items():
if not isinstance(val, dict):
2023-06-24 19:06:23 +01:00
if attr in dir(self): # avoid calling getattr (with hasattr)
target, attr, val = fget(attr, val)
setattr(target, attr, val)
2023-06-24 19:06:23 +01:00
else:
self.logger.error(f"invalid attribute {attr} for {self}")
else:
target = getattr(self, attr)
target.apply(val)
2022-06-16 14:07:12 +01:00
return self
def then_wait(self):
time.sleep(self._remote.DELAY)