2022-09-28 18:13:07 +01:00
|
|
|
import logging
|
2022-06-16 16:10:06 +01:00
|
|
|
from abc import abstractmethod
|
|
|
|
from enum import IntEnum
|
|
|
|
from functools import cached_property
|
2023-08-02 17:19:08 +01:00
|
|
|
from typing import Iterable
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
from .bus import request_bus_obj as bus
|
|
|
|
from .command import Command
|
|
|
|
from .config import request_config as configs
|
2023-06-25 12:21:02 +01:00
|
|
|
from .error import VBANCMDError
|
2022-06-16 16:10:06 +01:00
|
|
|
from .kinds import KindMapClass
|
|
|
|
from .kinds import request_kind_map as kindmap
|
2023-07-08 07:34:30 +01:00
|
|
|
from .macrobutton import MacroButton
|
2022-06-16 16:10:06 +01:00
|
|
|
from .strip import request_strip_obj as strip
|
2023-07-08 07:34:30 +01:00
|
|
|
from .vban import request_vban_obj as vban
|
2022-09-28 18:13:07 +01:00
|
|
|
from .vbancmd import VbanCmd
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2023-06-25 12:21:02 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
class FactoryBuilder:
|
|
|
|
"""
|
|
|
|
Builder class for factories.
|
|
|
|
|
|
|
|
Separates construction from representation.
|
|
|
|
"""
|
|
|
|
|
2023-07-08 07:34:30 +01:00
|
|
|
BuilderProgress = IntEnum(
|
2025-01-17 02:51:17 +00:00
|
|
|
'BuilderProgress', 'strip bus command macrobutton vban', start=0
|
2023-07-08 07:34:30 +01:00
|
|
|
)
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
def __init__(self, factory, kind: KindMapClass):
|
|
|
|
self._factory = factory
|
|
|
|
self.kind = kind
|
|
|
|
self._info = (
|
2025-01-17 02:51:17 +00:00
|
|
|
f'Finished building strips for {self._factory}',
|
|
|
|
f'Finished building buses for {self._factory}',
|
|
|
|
f'Finished building commands for {self._factory}',
|
|
|
|
f'Finished building macrobuttons for {self._factory}',
|
|
|
|
f'Finished building vban in/out streams for {self._factory}',
|
2022-06-16 16:10:06 +01:00
|
|
|
)
|
2023-06-25 12:21:02 +01:00
|
|
|
self.logger = logger.getChild(self.__class__.__name__)
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2023-08-02 17:19:08 +01:00
|
|
|
def _pinfo(self, name: str) -> None:
|
2022-06-16 16:10:06 +01:00
|
|
|
"""prints progress status for each step"""
|
2025-01-17 02:51:17 +00:00
|
|
|
name = name.split('_')[1]
|
2022-09-28 18:13:07 +01:00
|
|
|
self.logger.info(self._info[int(getattr(self.BuilderProgress, name))])
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2022-09-03 16:47:38 +01:00
|
|
|
def make_strip(self):
|
2022-06-16 16:10:06 +01:00
|
|
|
self._factory.strip = tuple(
|
2022-06-16 17:31:32 +01:00
|
|
|
strip(i < self.kind.phys_in, self._factory, i)
|
2022-06-16 16:10:06 +01:00
|
|
|
for i in range(self.kind.num_strip)
|
|
|
|
)
|
|
|
|
return self
|
|
|
|
|
2022-09-03 16:47:38 +01:00
|
|
|
def make_bus(self):
|
2022-06-16 16:10:06 +01:00
|
|
|
self._factory.bus = tuple(
|
2022-06-16 17:31:32 +01:00
|
|
|
bus(i < self.kind.phys_out, self._factory, i)
|
2022-06-16 16:10:06 +01:00
|
|
|
for i in range(self.kind.num_bus)
|
|
|
|
)
|
|
|
|
return self
|
|
|
|
|
2022-09-03 16:47:38 +01:00
|
|
|
def make_command(self):
|
2022-06-16 16:10:06 +01:00
|
|
|
self._factory.command = Command.make(self._factory)
|
|
|
|
return self
|
|
|
|
|
2023-07-08 07:34:30 +01:00
|
|
|
def make_macrobutton(self):
|
|
|
|
self._factory.button = tuple(MacroButton(self._factory, i) for i in range(80))
|
|
|
|
return self
|
|
|
|
|
|
|
|
def make_vban(self):
|
|
|
|
self._factory.vban = vban(self._factory)
|
|
|
|
return self
|
|
|
|
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
class FactoryBase(VbanCmd):
|
|
|
|
"""Base class for factories, subclasses VbanCmd."""
|
|
|
|
|
|
|
|
def __init__(self, kind_id: str, **kwargs):
|
|
|
|
defaultkwargs = {
|
2025-01-17 02:51:17 +00:00
|
|
|
'ip': None,
|
|
|
|
'port': 6980,
|
|
|
|
'streamname': 'Command1',
|
|
|
|
'bps': 0,
|
|
|
|
'channel': 0,
|
|
|
|
'ratelimit': 0.01,
|
|
|
|
'timeout': 5,
|
|
|
|
'outbound': False,
|
|
|
|
'sync': False,
|
|
|
|
'pdirty': False,
|
|
|
|
'ldirty': False,
|
2022-06-16 16:10:06 +01:00
|
|
|
}
|
2025-01-17 02:51:17 +00:00
|
|
|
if 'subs' in kwargs:
|
|
|
|
defaultkwargs |= kwargs.pop('subs') # for backwards compatibility
|
2022-06-16 16:10:06 +01:00
|
|
|
kwargs = defaultkwargs | kwargs
|
|
|
|
self.kind = kindmap(kind_id)
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.builder = FactoryBuilder(self, self.kind)
|
|
|
|
self._steps = (
|
|
|
|
self.builder.make_strip,
|
|
|
|
self.builder.make_bus,
|
|
|
|
self.builder.make_command,
|
2023-07-08 07:34:30 +01:00
|
|
|
self.builder.make_macrobutton,
|
|
|
|
self.builder.make_vban,
|
2022-06-16 16:10:06 +01:00
|
|
|
)
|
|
|
|
self._configs = None
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2025-01-17 02:51:17 +00:00
|
|
|
return f'Voicemeeter {self.kind}'
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2023-07-08 07:59:51 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return (
|
|
|
|
type(self).__name__
|
|
|
|
+ f"({self.kind}, ip='{self.ip}', port={self.port}, streamname='{self.streamname}')"
|
|
|
|
)
|
|
|
|
|
2022-06-16 16:10:06 +01:00
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def steps(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def configs(self):
|
|
|
|
self._configs = configs(self.kind.name)
|
|
|
|
return self._configs
|
|
|
|
|
|
|
|
|
|
|
|
class BasicFactory(FactoryBase):
|
|
|
|
"""
|
|
|
|
Represents a Basic VbanCmd subclass
|
|
|
|
|
|
|
|
Responsible for directing the builder class
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
if cls is BasicFactory:
|
|
|
|
raise TypeError(f"'{cls.__name__}' does not support direct instantiation")
|
|
|
|
return object.__new__(cls)
|
|
|
|
|
|
|
|
def __init__(self, kind_id, **kwargs):
|
|
|
|
super().__init__(kind_id, **kwargs)
|
|
|
|
[step()._pinfo(step.__name__) for step in self.steps]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def steps(self) -> Iterable:
|
|
|
|
"""steps required to build the interface for a kind"""
|
|
|
|
return self._steps
|
|
|
|
|
|
|
|
|
|
|
|
class BananaFactory(FactoryBase):
|
|
|
|
"""
|
|
|
|
Represents a Banana VbanCmd subclass
|
|
|
|
|
|
|
|
Responsible for directing the builder class
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
if cls is BananaFactory:
|
|
|
|
raise TypeError(f"'{cls.__name__}' does not support direct instantiation")
|
|
|
|
return object.__new__(cls)
|
|
|
|
|
|
|
|
def __init__(self, kind_id, **kwargs):
|
|
|
|
super().__init__(kind_id, **kwargs)
|
|
|
|
[step()._pinfo(step.__name__) for step in self.steps]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def steps(self) -> Iterable:
|
|
|
|
"""steps required to build the interface for a kind"""
|
|
|
|
return self._steps
|
|
|
|
|
|
|
|
|
|
|
|
class PotatoFactory(FactoryBase):
|
|
|
|
"""
|
|
|
|
Represents a Potato VbanCmd subclass
|
|
|
|
|
|
|
|
Responsible for directing the builder class
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
if cls is PotatoFactory:
|
|
|
|
raise TypeError(f"'{cls.__name__}' does not support direct instantiation")
|
|
|
|
return object.__new__(cls)
|
|
|
|
|
|
|
|
def __init__(self, kind_id: str, **kwargs):
|
|
|
|
super().__init__(kind_id, **kwargs)
|
|
|
|
[step()._pinfo(step.__name__) for step in self.steps]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def steps(self) -> Iterable:
|
|
|
|
"""steps required to build the interface for a kind"""
|
|
|
|
return self._steps
|
|
|
|
|
|
|
|
|
|
|
|
def vbancmd_factory(kind_id: str, **kwargs) -> VbanCmd:
|
|
|
|
"""
|
|
|
|
Factory method, invokes a factory creation class of a kind
|
|
|
|
|
|
|
|
Returns a VbanCmd class of a kind
|
|
|
|
"""
|
|
|
|
match kind_id:
|
2025-01-17 02:51:17 +00:00
|
|
|
case 'basic':
|
2022-06-16 16:10:06 +01:00
|
|
|
_factory = BasicFactory
|
2025-01-17 02:51:17 +00:00
|
|
|
case 'banana':
|
2022-06-16 16:10:06 +01:00
|
|
|
_factory = BananaFactory
|
2025-01-17 02:51:17 +00:00
|
|
|
case 'potato':
|
2022-06-16 16:10:06 +01:00
|
|
|
_factory = PotatoFactory
|
|
|
|
case _:
|
|
|
|
raise ValueError(f"Unknown Voicemeeter kind '{kind_id}'")
|
2025-01-17 02:51:17 +00:00
|
|
|
return type(f'VbanCmd{kind_id.capitalize()}', (_factory,), {})(kind_id, **kwargs)
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
def request_vbancmd_obj(kind_id: str, **kwargs) -> VbanCmd:
|
|
|
|
"""
|
|
|
|
Interface entry point. Wraps factory method and handles errors
|
|
|
|
|
|
|
|
Returns a reference to a VbanCmd class of a kind
|
|
|
|
"""
|
2025-01-17 02:51:17 +00:00
|
|
|
logger_entry = logger.getChild('factory.request_vbancmd_obj')
|
2023-06-25 12:21:02 +01:00
|
|
|
|
2022-06-16 16:10:06 +01:00
|
|
|
VBANCMD_obj = None
|
|
|
|
try:
|
|
|
|
VBANCMD_obj = vbancmd_factory(kind_id, **kwargs)
|
|
|
|
except (ValueError, TypeError) as e:
|
2025-01-17 02:51:17 +00:00
|
|
|
logger_entry.exception(f'{type(e).__name__}: {e}')
|
2023-06-25 12:21:02 +01:00
|
|
|
raise VBANCMDError(str(e)) from e
|
2022-06-16 16:10:06 +01:00
|
|
|
return VBANCMD_obj
|