From 20851c3880642a99a81d229501766df2d48234df Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Wed, 27 Jul 2022 22:44:40 +0100 Subject: [PATCH] request and event data now returned as dataclasses unit tests updated accordingly --- .gitignore | 13 +++++++++++++ examples/events/__main__.py | 6 +++--- examples/hotkeys/__main__.py | 2 +- examples/scene_rotate/__main__.py | 2 +- obsstudio_sdk/baseclient.py | 2 -- obsstudio_sdk/callback.py | 17 +++++------------ obsstudio_sdk/reqs.py | 3 ++- obsstudio_sdk/util.py | 22 ++++++++++++++++++++++ tests/test_request.py | 10 +++++----- 9 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 obsstudio_sdk/util.py diff --git a/.gitignore b/.gitignore index a04522a..7b77f4a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,19 @@ wheels/ *.egg MANIFEST +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + # Environments .env .venv diff --git a/examples/events/__main__.py b/examples/events/__main__.py index 40b297d..214b371 100644 --- a/examples/events/__main__.py +++ b/examples/events/__main__.py @@ -16,15 +16,15 @@ class Observer: def on_current_program_scene_changed(self, data): """The current program scene has changed.""" - print(f"Switched to scene {data['sceneName']}") + print(f"Switched to scene {data.scene_name}") def on_scene_created(self, data): """A new scene has been created.""" - print(f"scene {data['sceneName']} has been created") + print(f"scene {data.scene_name} has been created") def on_input_mute_state_changed(self, data): """An input's mute state has changed.""" - print(f"{data['inputName']} mute toggled") + print(f"{data.input_name} mute toggled") def on_exit_started(self, data): """OBS has begun the shutdown process.""" diff --git a/examples/hotkeys/__main__.py b/examples/hotkeys/__main__.py index 413fe42..5ede72b 100644 --- a/examples/hotkeys/__main__.py +++ b/examples/hotkeys/__main__.py @@ -16,7 +16,7 @@ class Observer: def on_current_program_scene_changed(self, data): """The current program scene has changed.""" - print(f"{self.event_identifier}: {data}") + print(f"{self.event_identifier}: {data.scene_name}") def version(): diff --git a/examples/scene_rotate/__main__.py b/examples/scene_rotate/__main__.py index 9994d05..2454690 100644 --- a/examples/scene_rotate/__main__.py +++ b/examples/scene_rotate/__main__.py @@ -5,7 +5,7 @@ import obsstudio_sdk as obs def main(): resp = cl.get_scene_list() - scenes = reversed(tuple(di["sceneName"] for di in resp["scenes"])) + scenes = reversed(tuple(di.get("sceneName") for di in resp.scenes)) for sc in scenes: print(f"Switching to scene {sc}") diff --git a/obsstudio_sdk/baseclient.py b/obsstudio_sdk/baseclient.py index 5419e1f..50240e8 100644 --- a/obsstudio_sdk/baseclient.py +++ b/obsstudio_sdk/baseclient.py @@ -1,8 +1,6 @@ import base64 import hashlib import json -import time -from enum import IntEnum from pathlib import Path from random import randint diff --git a/obsstudio_sdk/callback.py b/obsstudio_sdk/callback.py index a106c3a..9015997 100644 --- a/obsstudio_sdk/callback.py +++ b/obsstudio_sdk/callback.py @@ -1,6 +1,7 @@ -import re from typing import Callable, Iterable, Union +from .util import as_dataclass, to_camel_case, to_snake_case + class Callback: """Adds support for callbacks""" @@ -10,25 +11,17 @@ class Callback: self._callbacks = list() - def to_camel_case(self, s): - s = "".join(word.title() for word in s.split("_")) - return s[2:] - - def to_snake_case(self, s): - s = re.sub(r"(? list: """returns a list of registered events""" - return [self.to_camel_case(fn.__name__) for fn in self._callbacks] + return [to_camel_case(fn.__name__) for fn in self._callbacks] def trigger(self, event, data): """trigger callback on update""" for fn in self._callbacks: - if fn.__name__ == self.to_snake_case(event): - fn(data.get("eventData")) + if fn.__name__ == f"on_{to_snake_case(event)}": + fn(as_dataclass(event, data.get("eventData"))) def register(self, fns: Union[Iterable, Callable]): """registers callback functions""" diff --git a/obsstudio_sdk/reqs.py b/obsstudio_sdk/reqs.py index c685f11..2bf39f1 100644 --- a/obsstudio_sdk/reqs.py +++ b/obsstudio_sdk/reqs.py @@ -1,5 +1,6 @@ from .baseclient import ObsClient from .error import OBSSDKError +from .util import as_dataclass """ A class to interact with obs-websocket requests @@ -23,7 +24,7 @@ class ReqClient(object): error += (f"With message: {response['requestStatus']['comment']}",) raise OBSSDKError("\n".join(error)) if "responseData" in response: - return response["responseData"] + return as_dataclass(response["requestType"], response["responseData"]) def get_version(self): """ diff --git a/obsstudio_sdk/util.py b/obsstudio_sdk/util.py new file mode 100644 index 0000000..59dbcc3 --- /dev/null +++ b/obsstudio_sdk/util.py @@ -0,0 +1,22 @@ +import re +from dataclasses import dataclass + + +def to_camel_case(s): + s = "".join(word.title() for word in s.split("_")) + return s[2:] + + +def to_snake_case(s): + s = re.sub(r"(?