From ce9bc7e8d65429b86d26cc353c2b9673662c91cc Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Thu, 28 Jul 2022 11:55:05 +0100 Subject: [PATCH] add attrs to dataclasses --- obsstudio_sdk/callback.py | 2 +- obsstudio_sdk/events.py | 5 ++++- obsstudio_sdk/util.py | 8 +++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/obsstudio_sdk/callback.py b/obsstudio_sdk/callback.py index 8aa9a0c..1924848 100644 --- a/obsstudio_sdk/callback.py +++ b/obsstudio_sdk/callback.py @@ -21,7 +21,7 @@ class Callback: for fn in self._callbacks: if fn.__name__ == f"on_{to_snake_case(event)}": - fn(as_dataclass(event, data.get("eventData"))) + fn(as_dataclass(event, data)) def register(self, fns: Union[Iterable, Callable]): """registers callback functions""" diff --git a/obsstudio_sdk/events.py b/obsstudio_sdk/events.py index fa34a13..6abc89d 100644 --- a/obsstudio_sdk/events.py +++ b/obsstudio_sdk/events.py @@ -57,7 +57,10 @@ class EventClient(object): self.running = True while self.running: self.data = json.loads(self.base_client.ws.recv()) - event, data = (self.data["d"].get("eventType"), self.data["d"]) + event, data = ( + self.data["d"].get("eventType"), + self.data["d"].get("eventData"), + ) self.callback.trigger(event, data) time.sleep(self.DELAY) diff --git a/obsstudio_sdk/util.py b/obsstudio_sdk/util.py index 51fe39a..7f1ec48 100644 --- a/obsstudio_sdk/util.py +++ b/obsstudio_sdk/util.py @@ -11,10 +11,16 @@ def to_snake_case(s): def as_dataclass(identifier, data): + def attrs(): + return list(data.keys()) + return dataclass( type( f"{identifier}Dataclass", (), - {**{to_snake_case(k): v for k, v in data.items()}}, + { + "attrs": attrs, + **{to_snake_case(k): v for k, v in data.items()}, + }, ) )