xair-api-python/xair_api/bus.py

67 lines
1.7 KiB
Python
Raw Permalink Normal View History

2022-04-05 20:05:55 +01:00
import abc
import logging
2022-11-16 15:51:26 +00:00
from .meta import mute_prop
from .shared import EQ, GEQ, Config, Dyn, Group, Insert, Mix
2022-04-05 20:05:55 +01:00
logger = logging.getLogger(__name__)
2022-04-05 20:05:55 +01:00
class IBus(abc.ABC):
"""Abstract Base Class for buses"""
def __init__(self, remote, index: int):
self._remote = remote
self.index = index + 1
self.logger = logger.getChild(self.__class__.__name__)
2022-04-05 20:05:55 +01:00
def getter(self, param: str):
return self._remote.query(f"{self.address}/{param}")
2022-04-05 20:05:55 +01:00
def setter(self, param: str, val: int):
self._remote.send(f"{self.address}/{param}", val)
@abc.abstractmethod
def address(self):
pass
class Bus(IBus):
"""Concrete class for buses"""
@classmethod
def make(cls, remote, index):
"""
Factory function for buses
Creates a mixin of shared subclasses, sets them as class attributes.
Returns a Bus class of a kind.
"""
BUS_cls = type(
f"Bus{remote.kind}",
2022-04-05 20:05:55 +01:00
(cls,),
{
**{
_cls.__name__.lower(): type(
f"{_cls.__name__}{remote.kind}", (_cls, cls), {}
2022-04-05 20:05:55 +01:00
)(remote, index)
for _cls in (
Config,
Dyn,
Insert,
GEQ.make(),
EQ.make_sixband(cls, remote, index),
Mix,
Group,
)
2022-11-16 15:51:26 +00:00
},
"mute": mute_prop(),
2022-04-05 20:05:55 +01:00
},
)
return BUS_cls(remote, index)
@property
def address(self) -> str:
return f"/bus/{self.index}"