upd examples, they now use context managers

This commit is contained in:
onyx-and-iris 2024-01-07 11:21:01 +00:00
parent cac236c004
commit 2cebd5eedb
3 changed files with 33 additions and 22 deletions

View File

@ -17,6 +17,12 @@ class Observer:
print(f"Registered events: {self._client.callback.get()}") print(f"Registered events: {self._client.callback.get()}")
self.running = True 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): def on_current_program_scene_changed(self, data):
"""The current program scene has changed.""" """The current program scene has changed."""
print(f"Switched to scene {data.scene_name}") print(f"Switched to scene {data.scene_name}")
@ -31,13 +37,11 @@ class Observer:
def on_exit_started(self, _): def on_exit_started(self, _):
"""OBS has begun the shutdown process.""" """OBS has begun the shutdown process."""
print(f"OBS closing!") print("OBS closing!")
self._client.unsubscribe()
self.running = False self.running = False
if __name__ == "__main__": if __name__ == "__main__":
observer = Observer() with Observer() as observer:
while observer.running: while observer.running:
time.sleep(0.1) time.sleep(0.1)

View File

@ -1,6 +1,7 @@
import inspect import inspect
import keyboard import keyboard
import obsws_python as obs import obsws_python as obs
@ -10,6 +11,12 @@ class Observer:
self._client.callback.register(self.on_current_program_scene_changed) self._client.callback.register(self.on_current_program_scene_changed)
print(f"Registered events: {self._client.callback.get()}") 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 @property
def event_identifier(self): def event_identifier(self):
return inspect.stack()[1].function return inspect.stack()[1].function
@ -31,9 +38,8 @@ def set_scene(scene, *args):
if __name__ == "__main__": if __name__ == "__main__":
req_client = obs.ReqClient() with obs.ReqClient() as req_client:
observer = Observer() with Observer() as observer:
keyboard.add_hotkey("0", version) keyboard.add_hotkey("0", version)
keyboard.add_hotkey("1", set_scene, args=("START",)) keyboard.add_hotkey("1", set_scene, args=("START",))
keyboard.add_hotkey("2", set_scene, args=("BRB",)) keyboard.add_hotkey("2", set_scene, args=("BRB",))

View File

@ -9,6 +9,8 @@ LEVELTYPE = IntEnum(
start=0, start=0,
) )
DEVICE = "Desktop Audio"
def on_input_mute_state_changed(data): def on_input_mute_state_changed(data):
"""An input's mute state has changed.""" """An input's mute state has changed."""
@ -32,15 +34,14 @@ def on_input_volume_meters(data):
def main(): def main():
client = obs.EventClient(subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS)) 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]) client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
while cmd := input("<Enter> to exit>\n"): while _ := input("<Enter> to exit>\n"):
if not cmd: pass
break
if __name__ == "__main__": if __name__ == "__main__":
DEVICE = "Desktop Audio"
main() main()