mirror of
https://github.com/onyx-and-iris/obsws-python.git
synced 2026-04-18 14:03:32 +00:00
request and event data now returned as dataclasses
unit tests updated accordingly
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
from enum import IntEnum
|
||||
from pathlib import Path
|
||||
from random import randint
|
||||
|
||||
|
||||
@@ -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"(?<!^)(?=[A-Z])", "_", s).lower()
|
||||
return f"on_{s}"
|
||||
|
||||
def get(self) -> 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"""
|
||||
|
||||
@@ -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):
|
||||
"""
|
||||
|
||||
22
obsstudio_sdk/util.py
Normal file
22
obsstudio_sdk/util.py
Normal file
@@ -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"(?<!^)(?=[A-Z])", "_", s).lower()
|
||||
return f"{s}"
|
||||
|
||||
|
||||
def as_dataclass(identifier, data):
|
||||
return dataclass(
|
||||
type(
|
||||
f"{identifier}Dataclass",
|
||||
(),
|
||||
{**{to_snake_case(k): v for k, v in data.items()}},
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user