From 7b725a51e3cf4ad63069025df55d3859c89de496 Mon Sep 17 00:00:00 2001 From: Onyx and Iris Date: Wed, 15 Jan 2025 12:34:31 +0000 Subject: [PATCH] update examples --- examples/dsl/__main__.py | 3 +- examples/events/__main__.py | 2 +- examples/gui/__main__.py | 4 +- examples/midi/__main__.py | 2 +- examples/obs/__main__.py | 82 ++++++++++++++++++----------------- examples/observer/__main__.py | 2 +- scripts.py | 6 +-- 7 files changed, 52 insertions(+), 49 deletions(-) diff --git a/examples/dsl/__main__.py b/examples/dsl/__main__.py index 76d49f9..12f03f1 100644 --- a/examples/dsl/__main__.py +++ b/examples/dsl/__main__.py @@ -131,7 +131,8 @@ class Parser: case [ [kls, index], [secondary, param], - [val] | val, + [val] + | val, ]: primary = getattr(self.vm, kls)[int(index)] target = getattr(primary, secondary) diff --git a/examples/events/__main__.py b/examples/events/__main__.py index 2d13ba9..5485e7b 100644 --- a/examples/events/__main__.py +++ b/examples/events/__main__.py @@ -41,7 +41,7 @@ def main(): KIND_ID = "banana" with voicemeeterlib.api(KIND_ID) as vm: - with App(vm) as app: + with App(vm): for i in range(5, 0, -1): print(f"events start in {i} seconds") time.sleep(1) diff --git a/examples/gui/__main__.py b/examples/gui/__main__.py index c7b0f53..b9d487e 100644 --- a/examples/gui/__main__.py +++ b/examples/gui/__main__.py @@ -1,10 +1,10 @@ import logging +import tkinter as tk +from tkinter import ttk import voicemeeterlib logging.basicConfig(level=logging.DEBUG) -import tkinter as tk -from tkinter import ttk class App(tk.Tk): diff --git a/examples/midi/__main__.py b/examples/midi/__main__.py index 83a67e9..b529118 100644 --- a/examples/midi/__main__.py +++ b/examples/midi/__main__.py @@ -43,7 +43,7 @@ def main(): with voicemeeterlib.api(KIND_ID, midi=True) as vm: App(vm) - while cmd := input("Press to exit\n"): + while _ := input("Press to exit\n"): pass diff --git a/examples/obs/__main__.py b/examples/obs/__main__.py index 20c1fd0..c4a84f6 100644 --- a/examples/obs/__main__.py +++ b/examples/obs/__main__.py @@ -1,4 +1,4 @@ -import time +import threading from logging import config import obsws_python as obsws @@ -7,37 +7,43 @@ import voicemeeterlib config.dictConfig( { - "version": 1, - "formatters": { - "standard": { - "format": "%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s" + 'version': 1, + 'formatters': { + 'standard': { + 'format': '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s' } }, - "handlers": { - "stream": { - "level": "DEBUG", - "class": "logging.StreamHandler", - "formatter": "standard", + 'handlers': { + 'stream': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'standard', } }, - "loggers": { - "voicemeeterlib.iremote": {"handlers": ["stream"], "level": "DEBUG"} + 'loggers': { + 'voicemeeterlib.iremote': {'handlers': ['stream'], 'level': 'DEBUG'} }, } ) class MyClient: - def __init__(self, vm): + def __init__(self, vm, stop_event): self.vm = vm - self.client = obsws.EventClient() - self.client.callback.register( + self._stop_event = stop_event + self._client = obsws.EventClient() + self._client.callback.register( ( self.on_current_program_scene_changed, self.on_exit_started, ) ) - self.is_running = True + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + self._client.disconnect() def on_start(self): self.vm.strip[0].mute = True @@ -51,10 +57,10 @@ class MyClient: def on_end(self): self.vm.apply( { - "strip-0": {"mute": True, "comp": {"ratio": 4.3}}, - "strip-1": {"mute": True, "B1": False, "gate": {"attack": 2.3}}, - "strip-2": {"mute": True, "B1": False}, - "vban-in-0": {"on": False}, + 'strip-0': {'mute': True, 'comp': {'ratio': 4.3}}, + 'strip-1': {'mute': True, 'B1': False, 'gate': {'attack': 2.3}}, + 'strip-2': {'mute': True, 'B1': False}, + 'vban-in-0': {'on': False}, } ) @@ -65,33 +71,31 @@ class MyClient: self.vm.vban.instream[0].on = True def on_current_program_scene_changed(self, data): - def fget(scene): - run = { - "START": self.on_start, - "BRB": self.on_brb, - "END": self.on_end, - "LIVE": self.on_live, - } - return run.get(scene) - scene = data.scene_name - print(f"Switched to scene {scene}") - if fn := fget(scene): - fn() + print(f'Switched to scene {scene}') + match scene: + case 'START': + self.on_start() + case 'BRB': + self.on_brb() + case 'END': + self.on_end() + case 'LIVE': + self.on_live() def on_exit_started(self, _): - self.client.unsubscribe() - self.is_running = False + self._stop_event.set() def main(): - KIND_ID = "potato" + KIND_ID = 'potato' with voicemeeterlib.api(KIND_ID) as vm: - client = MyClient(vm) - while client.is_running: - time.sleep(0.1) + stop_event = threading.Event() + + with MyClient(vm, stop_event): + stop_event.wait() -if __name__ == "__main__": +if __name__ == '__main__': main() diff --git a/examples/observer/__main__.py b/examples/observer/__main__.py index 99b24d1..f8e8584 100644 --- a/examples/observer/__main__.py +++ b/examples/observer/__main__.py @@ -37,7 +37,7 @@ def main(): ) as vm: App(vm) - while cmd := input("Press to exit\n"): + while _ := input("Press to exit\n"): pass diff --git a/scripts.py b/scripts.py index 4577897..66ca60f 100644 --- a/scripts.py +++ b/scripts.py @@ -5,8 +5,7 @@ from pathlib import Path def ex_dsl(): - scriptpath = Path.cwd() / "examples" / "dsl" / "." - subprocess.run([sys.executable, str(scriptpath)]) + subprocess.run(["tox", "r", "-e", "dsl"]) def ex_events(): @@ -30,8 +29,7 @@ def ex_midi(): def ex_obs(): - scriptpath = Path.cwd() / "examples" / "obs" / "." - subprocess.run([sys.executable, str(scriptpath)]) + subprocess.run(["tox", "r", "-e", "obs"]) def ex_observer():