From 2cebd5eedbb5a883527582c3f437dce063eb4228 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 7 Jan 2024 11:21:01 +0000 Subject: [PATCH] upd examples, they now use context managers --- examples/events/__main__.py | 16 ++++++++++------ examples/hotkeys/__main__.py | 24 +++++++++++++++--------- examples/levels/__main__.py | 15 ++++++++------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/examples/events/__main__.py b/examples/events/__main__.py index db2bf37..e5e14f7 100644 --- a/examples/events/__main__.py +++ b/examples/events/__main__.py @@ -17,6 +17,12 @@ class Observer: print(f"Registered events: {self._client.callback.get()}") self.running = True + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + self._client.disconnect() + def on_current_program_scene_changed(self, data): """The current program scene has changed.""" print(f"Switched to scene {data.scene_name}") @@ -31,13 +37,11 @@ class Observer: def on_exit_started(self, _): """OBS has begun the shutdown process.""" - print(f"OBS closing!") - self._client.unsubscribe() + print("OBS closing!") self.running = False if __name__ == "__main__": - observer = Observer() - - while observer.running: - time.sleep(0.1) + with Observer() as observer: + while observer.running: + time.sleep(0.1) diff --git a/examples/hotkeys/__main__.py b/examples/hotkeys/__main__.py index e64ca87..efc5803 100644 --- a/examples/hotkeys/__main__.py +++ b/examples/hotkeys/__main__.py @@ -1,6 +1,7 @@ import inspect import keyboard + import obsws_python as obs @@ -10,6 +11,12 @@ class Observer: self._client.callback.register(self.on_current_program_scene_changed) print(f"Registered events: {self._client.callback.get()}") + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + self._client.disconnect() + @property def event_identifier(self): return inspect.stack()[1].function @@ -31,13 +38,12 @@ def set_scene(scene, *args): if __name__ == "__main__": - req_client = obs.ReqClient() - observer = Observer() + with obs.ReqClient() as req_client: + with Observer() as observer: + keyboard.add_hotkey("0", version) + keyboard.add_hotkey("1", set_scene, args=("START",)) + keyboard.add_hotkey("2", set_scene, args=("BRB",)) + keyboard.add_hotkey("3", set_scene, args=("END",)) - keyboard.add_hotkey("0", version) - keyboard.add_hotkey("1", set_scene, args=("START",)) - keyboard.add_hotkey("2", set_scene, args=("BRB",)) - keyboard.add_hotkey("3", set_scene, args=("END",)) - - print("press ctrl+enter to quit") - keyboard.wait("ctrl+enter") + print("press ctrl+enter to quit") + keyboard.wait("ctrl+enter") diff --git a/examples/levels/__main__.py b/examples/levels/__main__.py index 62e03a8..a1e8503 100644 --- a/examples/levels/__main__.py +++ b/examples/levels/__main__.py @@ -9,6 +9,8 @@ LEVELTYPE = IntEnum( start=0, ) +DEVICE = "Desktop Audio" + def on_input_mute_state_changed(data): """An input's mute state has changed.""" @@ -32,15 +34,14 @@ def on_input_volume_meters(data): def main(): - client = obs.EventClient(subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS)) - client.callback.register([on_input_volume_meters, on_input_mute_state_changed]) + with obs.EventClient( + subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS) + ) as client: + client.callback.register([on_input_volume_meters, on_input_mute_state_changed]) - while cmd := input(" to exit>\n"): - if not cmd: - break + while _ := input(" to exit>\n"): + pass if __name__ == "__main__": - DEVICE = "Desktop Audio" - main()