From eddccb66c512a7d85539eb398b12e68049a46034 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Thu, 29 Sep 2022 09:44:14 +0100 Subject: [PATCH] event class moved into event.py logger module used to write interface events to console --- voicemeeterlib/config.py | 12 +++++++--- voicemeeterlib/event.py | 48 +++++++++++++++++++++++++++++++++++++++ voicemeeterlib/factory.py | 6 +++-- voicemeeterlib/misc.py | 42 ---------------------------------- 4 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 voicemeeterlib/event.py diff --git a/voicemeeterlib/config.py b/voicemeeterlib/config.py index 21a7d47..1e0cbf9 100644 --- a/voicemeeterlib/config.py +++ b/voicemeeterlib/config.py @@ -1,4 +1,5 @@ import itertools +import logging from pathlib import Path try: @@ -118,6 +119,8 @@ class Loader(metaclass=SingletonType): loads data into memory if not found """ + logger = logging.getLogger("config.Loader") + def __init__(self, kind): self._kind = kind self._configs = dict() @@ -131,14 +134,16 @@ class Loader(metaclass=SingletonType): def parse(self, identifier, data): if identifier in self._configs: - print(f"config file with name {identifier} already in memory, skipping..") + self.logger.info( + f"config file with name {identifier} already in memory, skipping.." + ) return False self.parser = dataextraction_factory(data) return True def register(self, identifier, data=None): self._configs[identifier] = data if data else self.parser.data - print(f"config {self.name}/{identifier} loaded into memory") + self.logger.info(f"config {self.name}/{identifier} loaded into memory") def deregister(self): self._configs.clear() @@ -161,6 +166,7 @@ def loader(kind): returns configs loaded into memory """ + logger = logging.getLogger("config.loader") loader = Loader(kind) for path in ( @@ -169,7 +175,7 @@ def loader(kind): Path.home() / "Documents/Voicemeeter" / "configs" / kind.name, ): if path.is_dir(): - print(f"Checking [{path}] for TOML config files:") + logger.info(f"Checking [{path}] for TOML config files:") for file in path.glob("*.toml"): identifier = file.with_suffix("").stem if loader.parse(identifier, file): diff --git a/voicemeeterlib/event.py b/voicemeeterlib/event.py new file mode 100644 index 0000000..6fb90a4 --- /dev/null +++ b/voicemeeterlib/event.py @@ -0,0 +1,48 @@ +import logging + + +class Event: + """Keeps track of event subscriptions""" + + logger = logging.getLogger("event.event") + + def __init__(self, subs: dict): + self.subs = subs + + def info(self, msg=None): + info = (f"{msg} events",) if msg else () + if self.any(): + info += (f"now listening for {', '.join(self.get())} events",) + else: + info += (f"not listening for any events",) + self.logger.info(", ".join(info)) + + @property + def pdirty(self): + return self.subs["pdirty"] + + @property + def mdirty(self): + return self.subs["mdirty"] + + @property + def midi(self): + return self.subs["midi"] + + @property + def ldirty(self): + return self.subs["ldirty"] + + def get(self) -> list: + return [k for k, v in self.subs.items() if v] + + def any(self) -> bool: + return any(self.subs.values()) + + def add(self, event): + self.subs[event] = True + self.info(f"{event} added to") + + def remove(self, event): + self.subs[event] = False + self.info(f"{event} removed from") diff --git a/voicemeeterlib/factory.py b/voicemeeterlib/factory.py index 895cf5c..b144c33 100644 --- a/voicemeeterlib/factory.py +++ b/voicemeeterlib/factory.py @@ -1,10 +1,10 @@ +import logging from abc import abstractmethod from enum import IntEnum from functools import cached_property from typing import Iterable, NoReturn from . import misc -from .base import Remote from .bus import request_bus_obj as bus from .command import Command from .config import request_config as configs @@ -13,6 +13,7 @@ from .kinds import KindMapClass from .kinds import request_kind_map as kindmap from .macrobutton import MacroButton from .recorder import Recorder +from .remote import Remote from .strip import request_strip_obj as strip from .vban import request_vban_obj as vban @@ -24,6 +25,7 @@ class FactoryBuilder: Separates construction from representation. """ + logger = logging.getLogger("remote.factorybuilder") BuilderProgress = IntEnum( "BuilderProgress", "strip bus command macrobutton vban device option recorder patch fx", @@ -49,7 +51,7 @@ class FactoryBuilder: def _pinfo(self, name: str) -> NoReturn: """prints progress status for each step""" name = name.split("_")[1] - print(self._info[int(getattr(self.BuilderProgress, name))]) + self.logger.info(self._info[int(getattr(self.BuilderProgress, name))]) def make_strip(self): self._factory.strip = tuple( diff --git a/voicemeeterlib/misc.py b/voicemeeterlib/misc.py index 1ae1bcc..a63c0d2 100644 --- a/voicemeeterlib/misc.py +++ b/voicemeeterlib/misc.py @@ -250,45 +250,3 @@ class Midi: def _set(self, key: int, velocity: int): self.cache[key] = velocity - - -class Event: - def __init__(self, subs: dict): - self.subs = subs - - def info(self, msg): - info = ( - f"{msg} events", - f"Now listening for {', '.join(self.get())} events", - ) - print("\n".join(info)) - - @property - def pdirty(self): - return self.subs["pdirty"] - - @property - def mdirty(self): - return self.subs["mdirty"] - - @property - def midi(self): - return self.subs["midi"] - - @property - def ldirty(self): - return self.subs["ldirty"] - - def get(self) -> list: - return [k for k, v in self.subs.items() if v] - - def any(self) -> bool: - return any(self.subs.values()) - - def add(self, event): - self.subs[event] = True - self.info(f"{event} added to") - - def remove(self, event): - self.subs[event] = False - self.info(f"{event} removed from")