2022-06-16 14:07:12 +01:00
|
|
|
import itertools
|
2022-09-29 09:44:14 +01:00
|
|
|
import logging
|
2022-06-16 14:07:12 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-06-23 01:19:55 +01:00
|
|
|
from .error import VMError
|
|
|
|
|
2022-09-03 16:28:19 +01:00
|
|
|
try:
|
|
|
|
import tomllib
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
import tomli as tomllib
|
2022-06-16 14:07:12 +01:00
|
|
|
|
|
|
|
from .kinds import request_kind_map as kindmap
|
|
|
|
|
2023-06-23 01:19:55 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-06-16 14:07:12 +01:00
|
|
|
|
|
|
|
class TOMLStrBuilder:
|
|
|
|
"""builds a config profile, as a string, for the toml parser"""
|
|
|
|
|
|
|
|
def __init__(self, kind):
|
|
|
|
self.kind = kind
|
|
|
|
self.higher = itertools.chain(
|
|
|
|
[f"strip-{i}" for i in range(kind.num_strip)],
|
|
|
|
[f"bus-{i}" for i in range(kind.num_bus)],
|
|
|
|
)
|
|
|
|
|
|
|
|
def init_config(self, profile=None):
|
|
|
|
self.virt_strip_params = (
|
|
|
|
[
|
|
|
|
"mute = false",
|
|
|
|
"mono = false",
|
|
|
|
"solo = false",
|
|
|
|
"gain = 0.0",
|
|
|
|
]
|
2022-06-18 11:12:33 +01:00
|
|
|
+ [f"A{i} = false" for i in range(1, self.kind.phys_out + 1)]
|
|
|
|
+ [f"B{i} = false" for i in range(1, self.kind.virt_out + 1)]
|
2022-06-16 14:07:12 +01:00
|
|
|
)
|
|
|
|
self.phys_strip_params = self.virt_strip_params + [
|
2023-06-23 01:19:55 +01:00
|
|
|
"comp.knob = 0.0",
|
|
|
|
"gate.knob = 0.0",
|
|
|
|
"denoiser.knob = 0.0",
|
|
|
|
"eq.on = false",
|
|
|
|
]
|
|
|
|
self.bus_params = [
|
|
|
|
"mono = false",
|
|
|
|
"eq.on = false",
|
|
|
|
"mute = false",
|
|
|
|
"gain = 0.0",
|
2022-06-16 14:07:12 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
if profile == "reset":
|
|
|
|
self.reset_config()
|
|
|
|
|
|
|
|
def reset_config(self):
|
|
|
|
self.phys_strip_params = list(
|
|
|
|
map(lambda x: x.replace("B1 = false", "B1 = true"), self.phys_strip_params)
|
|
|
|
)
|
|
|
|
self.virt_strip_params = list(
|
|
|
|
map(lambda x: x.replace("A1 = false", "A1 = true"), self.virt_strip_params)
|
|
|
|
)
|
|
|
|
|
|
|
|
def build(self, profile="reset"):
|
|
|
|
self.init_config(profile)
|
|
|
|
toml_str = str()
|
|
|
|
for eachclass in self.higher:
|
|
|
|
toml_str += f"[{eachclass}]\n"
|
|
|
|
toml_str = self.join(eachclass, toml_str)
|
|
|
|
return toml_str
|
|
|
|
|
|
|
|
def join(self, eachclass, toml_str):
|
|
|
|
kls, index = eachclass.split("-")
|
|
|
|
match kls:
|
|
|
|
case "strip":
|
|
|
|
toml_str += ("\n").join(
|
|
|
|
self.phys_strip_params
|
2022-06-18 11:12:33 +01:00
|
|
|
if int(index) < self.kind.phys_in
|
2022-06-16 14:07:12 +01:00
|
|
|
else self.virt_strip_params
|
|
|
|
)
|
|
|
|
case "bus":
|
2023-06-23 01:19:55 +01:00
|
|
|
toml_str += ("\n").join(self.bus_params)
|
2022-06-16 14:07:12 +01:00
|
|
|
case _:
|
|
|
|
pass
|
|
|
|
return toml_str + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
class TOMLDataExtractor:
|
|
|
|
def __init__(self, file):
|
|
|
|
with open(file, "rb") as f:
|
|
|
|
self._data = tomllib.load(f)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self):
|
|
|
|
return self._data
|
|
|
|
|
|
|
|
@data.setter
|
|
|
|
def data(self, value):
|
|
|
|
self._data = value
|
|
|
|
|
|
|
|
|
|
|
|
def dataextraction_factory(file):
|
|
|
|
"""
|
|
|
|
factory function for parser
|
|
|
|
|
|
|
|
this opens the possibility for other parsers to be added
|
|
|
|
"""
|
|
|
|
if file.suffix == ".toml":
|
|
|
|
extractor = TOMLDataExtractor
|
|
|
|
else:
|
|
|
|
raise ValueError("Cannot extract data from {}".format(file))
|
|
|
|
return extractor(file)
|
|
|
|
|
|
|
|
|
|
|
|
class SingletonType(type):
|
|
|
|
"""ensure only a single instance of Loader object"""
|
|
|
|
|
|
|
|
_instances = {}
|
|
|
|
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
|
|
if cls not in cls._instances:
|
|
|
|
cls._instances[cls] = super(SingletonType, cls).__call__(*args, **kwargs)
|
|
|
|
return cls._instances[cls]
|
|
|
|
|
|
|
|
|
|
|
|
class Loader(metaclass=SingletonType):
|
|
|
|
"""
|
|
|
|
invokes the parser
|
|
|
|
|
|
|
|
checks if config already in memory
|
|
|
|
|
|
|
|
loads data into memory if not found
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, kind):
|
|
|
|
self._kind = kind
|
2023-06-23 01:19:55 +01:00
|
|
|
self.logger = logger.getChild(self.__class__.__name__)
|
2022-06-16 14:07:12 +01:00
|
|
|
self._configs = dict()
|
|
|
|
self.defaults(kind)
|
|
|
|
self.parser = None
|
|
|
|
|
|
|
|
def defaults(self, kind):
|
|
|
|
self.builder = TOMLStrBuilder(kind)
|
|
|
|
toml_str = self.builder.build()
|
|
|
|
self.register("reset", tomllib.loads(toml_str))
|
|
|
|
|
|
|
|
def parse(self, identifier, data):
|
|
|
|
if identifier in self._configs:
|
2022-09-29 09:44:14 +01:00
|
|
|
self.logger.info(
|
|
|
|
f"config file with name {identifier} already in memory, skipping.."
|
|
|
|
)
|
2023-08-01 18:18:02 +01:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.parser = dataextraction_factory(data)
|
|
|
|
except tomllib.TOMLDecodeError as e:
|
|
|
|
ERR_MSG = (str(e), f"When attempting to load {identifier}.toml")
|
|
|
|
self.logger.error(f"{type(e).__name__}: {' '.join(ERR_MSG)}")
|
|
|
|
return
|
2022-06-16 14:07:12 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def register(self, identifier, data=None):
|
|
|
|
self._configs[identifier] = data if data else self.parser.data
|
2022-09-29 09:44:14 +01:00
|
|
|
self.logger.info(f"config {self.name}/{identifier} loaded into memory")
|
2022-06-16 14:07:12 +01:00
|
|
|
|
|
|
|
def deregister(self):
|
|
|
|
self._configs.clear()
|
|
|
|
self.defaults(self._kind)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def configs(self):
|
|
|
|
return self._configs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._kind.name
|
|
|
|
|
|
|
|
|
|
|
|
def loader(kind):
|
|
|
|
"""
|
|
|
|
traverses defined paths for config files
|
|
|
|
|
|
|
|
directs the loader
|
|
|
|
|
|
|
|
returns configs loaded into memory
|
|
|
|
"""
|
2023-06-23 01:19:55 +01:00
|
|
|
logger_loader = logger.getChild("loader")
|
2022-06-16 14:07:12 +01:00
|
|
|
loader = Loader(kind)
|
|
|
|
|
|
|
|
for path in (
|
|
|
|
Path.cwd() / "configs" / kind.name,
|
2023-06-23 01:19:55 +01:00
|
|
|
Path.home() / ".config" / "voicemeeter" / kind.name,
|
|
|
|
Path.home() / "Documents" / "Voicemeeter" / "configs" / kind.name,
|
2022-06-16 14:07:12 +01:00
|
|
|
):
|
|
|
|
if path.is_dir():
|
2023-06-23 01:19:55 +01:00
|
|
|
logger_loader.info(f"Checking [{path}] for TOML config files:")
|
2022-06-16 14:07:12 +01:00
|
|
|
for file in path.glob("*.toml"):
|
|
|
|
identifier = file.with_suffix("").stem
|
|
|
|
if loader.parse(identifier, file):
|
|
|
|
loader.register(identifier)
|
|
|
|
return loader.configs
|
|
|
|
|
|
|
|
|
|
|
|
def request_config(kind_id: str):
|
|
|
|
"""
|
|
|
|
config entry point.
|
|
|
|
|
|
|
|
Returns all configs loaded into memory for a kind
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
configs = loader(kindmap(kind_id))
|
|
|
|
except KeyError as e:
|
2023-06-23 01:19:55 +01:00
|
|
|
raise VMError(f"Unknown Voicemeeter kind {kind_id}") from e
|
2022-06-16 14:07:12 +01:00
|
|
|
return configs
|