From 86612a65cb16ad88f262c0a37d0d7071326d8fc4 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Thu, 6 Oct 2022 16:45:08 +0100 Subject: [PATCH] add property setters in event class use event property setters in examples update README patch bump --- README.md | 20 +++++++++++++----- examples/dsl/__main__.py | 2 +- examples/observer/__main__.py | 4 ++-- pyproject.toml | 2 +- voicemeeterlib/event.py | 38 ++++++++++++++++++++++++++--------- 5 files changed, 47 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index babf779..b2e7439 100644 --- a/README.md +++ b/README.md @@ -605,20 +605,30 @@ The following methods are available: example: ```python -# register the app self as an event observer -self.vm.subject.add(self) +# register an app to receive updates +class App(): + def __init__(self, vm): + vm.subject.add(self) + ... ``` #### `vm.event` -You may also add/remove event subscriptions as necessary with the Event class. +Use the event class to toggle updates as necessary. + +The following properties are available: + +- `pdirty`: boolean +- `mdirty`: boolean +- `midi`: boolean +- `ldirty`: boolean example: ```python -vm.event.add("ldirty") +vm.event.ldirty = True -vm.event.remove("pdirty") +vm.event.pdirty = False # get a list of currently subscribed print(vm.event.get()) diff --git a/examples/dsl/__main__.py b/examples/dsl/__main__.py index ab077f4..c9757a8 100644 --- a/examples/dsl/__main__.py +++ b/examples/dsl/__main__.py @@ -56,7 +56,7 @@ class Parser: def main(cmds=None): kind_id = "banana" - subs = {ev: False for ev in ["pdirty", "mdirty", "ldirty", "midi"]} + subs = {ev: False for ev in ["pdirty", "mdirty", "midi"]} with voicemeeterlib.api(kind_id, subs=subs) as vm: parser = Parser(vm) diff --git a/examples/observer/__main__.py b/examples/observer/__main__.py index cf9caf5..0051e58 100644 --- a/examples/observer/__main__.py +++ b/examples/observer/__main__.py @@ -8,8 +8,8 @@ class Observer: self.vm = vm # register your app as event observer self.vm.subject.add(self) - # add level updates, since they are disabled by default. - self.vm.event.add("ldirty") + # enable level updates, since they are disabled by default. + self.vm.event.ldirty = True # define an 'on_update' callback function to receive event updates def on_update(self, subject): diff --git a/pyproject.toml b/pyproject.toml index 219b8db..47edf17 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "voicemeeter-api" -version = "0.8.2" +version = "0.8.3" description = "A Python wrapper for the Voiceemeter API" authors = ["onyx-and-iris "] license = "MIT" diff --git a/voicemeeterlib/event.py b/voicemeeterlib/event.py index 6fb90a4..6e9da2d 100644 --- a/voicemeeterlib/event.py +++ b/voicemeeterlib/event.py @@ -18,21 +18,41 @@ class Event: self.logger.info(", ".join(info)) @property - def pdirty(self): + def pdirty(self) -> bool: return self.subs["pdirty"] + @pdirty.setter + def pdirty(self, val: bool): + self.subs["pdirty"] = val + self.info(f"pdirty {'added to' if val else {'removed from'}}") + @property - def mdirty(self): + def mdirty(self) -> bool: return self.subs["mdirty"] - @property - def midi(self): - return self.subs["midi"] + @mdirty.setter + def mdirty(self, val: bool): + self.subs["mdirty"] = val + self.info(f"mdirty {'added to' if val else {'removed from'}}") @property - def ldirty(self): + def midi(self) -> bool: + return self.subs["midi"] + + @midi.setter + def midi(self, val: bool): + self.subs["midi"] = val + self.info(f"midi {'added to' if val else {'removed from'}}") + + @property + def ldirty(self) -> bool: return self.subs["ldirty"] + @ldirty.setter + def ldirty(self, val: bool): + self.subs["ldirty"] = val + self.info(f"ldirty {'added to' if val else {'removed from'}}") + def get(self) -> list: return [k for k, v in self.subs.items() if v] @@ -40,9 +60,7 @@ class Event: return any(self.subs.values()) def add(self, event): - self.subs[event] = True - self.info(f"{event} added to") + setattr(self, event, True) def remove(self, event): - self.subs[event] = False - self.info(f"{event} removed from") + setattr(self, event, False)