From b8b446f9bf44cccc541079b546f7f3cdd0172c77 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Mon, 24 Oct 2022 22:42:46 +0100 Subject: [PATCH] use context manager in scene_rotate other refactors --- examples/events/README.md | 2 ++ examples/events/__main__.py | 22 ++++++++++++---------- examples/hotkeys/__main__.py | 17 ++++++++--------- examples/scene_rotate/__main__.py | 15 +++++++-------- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/examples/events/README.md b/examples/events/README.md index ce1b83a..b9f450d 100644 --- a/examples/events/README.md +++ b/examples/events/README.md @@ -14,3 +14,5 @@ host = "localhost" port = 4455 password = "mystrongpass" ``` + +Closing OBS ends the script. diff --git a/examples/events/__main__.py b/examples/events/__main__.py index 6a2ac45..db2bf37 100644 --- a/examples/events/__main__.py +++ b/examples/events/__main__.py @@ -1,10 +1,12 @@ +import time + import obsws_python as obs class Observer: - def __init__(self, cl): - self._cl = cl - self._cl.callback.register( + def __init__(self): + self._client = obs.EventClient() + self._client.callback.register( [ self.on_current_program_scene_changed, self.on_scene_created, @@ -12,7 +14,8 @@ class Observer: self.on_exit_started, ] ) - print(f"Registered events: {self._cl.callback.get()}") + print(f"Registered events: {self._client.callback.get()}") + self.running = True def on_current_program_scene_changed(self, data): """The current program scene has changed.""" @@ -29,13 +32,12 @@ class Observer: def on_exit_started(self, _): """OBS has begun the shutdown process.""" print(f"OBS closing!") - self._cl.unsubscribe() + self._client.unsubscribe() + self.running = False if __name__ == "__main__": - cl = obs.EventClient() - observer = Observer(cl) + observer = Observer() - while cmd := input(" to exit\n"): - if not cmd: - break + while observer.running: + time.sleep(0.1) diff --git a/examples/hotkeys/__main__.py b/examples/hotkeys/__main__.py index 81801a2..e64ca87 100644 --- a/examples/hotkeys/__main__.py +++ b/examples/hotkeys/__main__.py @@ -5,10 +5,10 @@ import obsws_python as obs class Observer: - def __init__(self, cl): - self._cl = cl - self._cl.callback.register(self.on_current_program_scene_changed) - print(f"Registered events: {self._cl.callback.get()}") + def __init__(self): + self._client = obs.EventClient() + self._client.callback.register(self.on_current_program_scene_changed) + print(f"Registered events: {self._client.callback.get()}") @property def event_identifier(self): @@ -20,20 +20,19 @@ class Observer: def version(): - resp = req_cl.get_version() + resp = req_client.get_version() print( f"Running OBS version:{resp.obs_version} with websocket version:{resp.obs_web_socket_version}" ) def set_scene(scene, *args): - req_cl.set_current_program_scene(scene) + req_client.set_current_program_scene(scene) if __name__ == "__main__": - req_cl = obs.ReqClient() - ev_cl = obs.EventClient() - observer = Observer(ev_cl) + req_client = obs.ReqClient() + observer = Observer() keyboard.add_hotkey("0", version) keyboard.add_hotkey("1", set_scene, args=("START",)) diff --git a/examples/scene_rotate/__main__.py b/examples/scene_rotate/__main__.py index 0879e8b..206f150 100644 --- a/examples/scene_rotate/__main__.py +++ b/examples/scene_rotate/__main__.py @@ -4,16 +4,15 @@ import obsws_python as obs def main(): - resp = cl.get_scene_list() - scenes = reversed(tuple(di.get("sceneName") for di in resp.scenes)) + with obs.ReqClient() as client: + resp = client.get_scene_list() + scenes = [di.get("sceneName") for di in reversed(resp.scenes)] - for scene in scenes: - print(f"Switching to scene {scene}") - cl.set_current_program_scene(scene) - time.sleep(0.5) + for scene in scenes: + print(f"Switching to scene {scene}") + client.set_current_program_scene(scene) + time.sleep(0.5) if __name__ == "__main__": - cl = obs.ReqClient() - main()