obsws-python/build/lib/obsstudio_sdk/subject.py
onyx-and-iris b5b69de218 add support for toml config.
subject module added, supports callbacks.

events module added. Provides an event listener and callback trigger.

import isorted, code run through black.

toml section added to readme.

added a couple of examples.
2022-07-25 23:51:30 +01:00

59 lines
1.5 KiB
Python

import re
class Callback:
"""Adds support for callbacks"""
def __init__(self):
"""list of current callbacks"""
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]
def trigger(self, event, data=None):
"""trigger callback on update"""
for fn in self._callbacks:
if fn.__name__ == self.to_snake_case(event):
if "eventData" in data:
fn(data["eventData"])
else:
fn()
def register(self, fns):
"""registers callback functions"""
try:
iter(fns)
for fn in fns:
if fn not in self._callbacks:
self._callbacks.append(fn)
except TypeError as e:
if fns not in self._callbacks:
self._callbacks.append(fns)
def deregister(self, callback):
"""deregisters a callback from _callbacks"""
try:
self._callbacks.remove(callback)
except ValueError:
print(f"Failed to remove: {callback}")
def clear(self):
"""clears the _callbacks list"""
self._callbacks.clear()