defaultkwarg subs added.

misc module added with Event class.

base class observable thread now checks for currently registered events.
This commit is contained in:
onyx-and-iris 2022-08-02 09:28:32 +01:00
parent 2ba42279cf
commit 6971feb398
3 changed files with 50 additions and 4 deletions

View File

@ -1,4 +1,3 @@
import dataclasses
import socket import socket
import time import time
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
@ -6,6 +5,7 @@ from enum import IntEnum
from threading import Thread from threading import Thread
from typing import Iterable, NoReturn, Optional, Union from typing import Iterable, NoReturn, Optional, Union
from .misc import Event
from .packet import ( from .packet import (
HEADER_SIZE, HEADER_SIZE,
RegisterRTHeader, RegisterRTHeader,
@ -48,7 +48,8 @@ class VbanCmd(metaclass=ABCMeta):
) )
self.running = True self.running = True
self.subject = Subject() self.subject = Subject()
self.cache = {} self.cache = dict()
self.event = Event(self.subs)
@abstractmethod @abstractmethod
def __str__(self): def __str__(self):
@ -187,6 +188,7 @@ class VbanCmd(metaclass=ABCMeta):
pass pass
def _updates(self) -> NoReturn: def _updates(self) -> NoReturn:
print(f"Listening for {', '.join(self.event.get())} events")
self.cache["strip_level"], self.cache["bus_level"] = self._get_levels( self.cache["strip_level"], self.cache["bus_level"] = self._get_levels(
self.public_packet self.public_packet
) )
@ -197,13 +199,13 @@ class VbanCmd(metaclass=ABCMeta):
self._strip_buf, self._bus_buf = self._get_levels(self._pp) self._strip_buf, self._bus_buf = self._get_levels(self._pp)
self._pdirty = self._pp.pdirty(self.public_packet) self._pdirty = self._pp.pdirty(self.public_packet)
if self.ldirty: if self.event.ldirty and self.ldirty:
self.cache["strip_level"] = self._strip_buf self.cache["strip_level"] = self._strip_buf
self.cache["bus_level"] = self._bus_buf self.cache["bus_level"] = self._bus_buf
self.subject.notify("ldirty") self.subject.notify("ldirty")
if self.public_packet != self._pp: if self.public_packet != self._pp:
self._public_packet = self._pp self._public_packet = self._pp
if self.pdirty: if self.event.pdirty and self.pdirty:
self.subject.notify("pdirty") self.subject.notify("pdirty")
elapsed = time.time() - start elapsed = time.time() - start
if self.ratelimit - elapsed > 0: if self.ratelimit - elapsed > 0:

View File

@ -58,6 +58,9 @@ class FactoryBase(VbanCmd):
"""Base class for factories, subclasses VbanCmd.""" """Base class for factories, subclasses VbanCmd."""
def __init__(self, kind_id: str, **kwargs): def __init__(self, kind_id: str, **kwargs):
defaultsubs = {"pdirty": True, "ldirty": False}
if "subs" in kwargs:
defaultsubs = defaultsubs | kwargs.pop("subs")
defaultkwargs = { defaultkwargs = {
"ip": None, "ip": None,
"port": 6980, "port": 6980,
@ -66,6 +69,7 @@ class FactoryBase(VbanCmd):
"channel": 0, "channel": 0,
"ratelimit": 0.01, "ratelimit": 0.01,
"sync": False, "sync": False,
"subs": defaultsubs,
} }
kwargs = defaultkwargs | kwargs kwargs = defaultkwargs | kwargs
self.kind = kindmap(kind_id) self.kind = kindmap(kind_id)

40
vban_cmd/misc.py Normal file
View File

@ -0,0 +1,40 @@
class Event:
def __init__(self, subs: dict):
self.subs = subs
def info(self, msg):
info = (
f"{msg} events",
f"Now listening for {', '.join(self.get())} events",
)
print("\n".join(info))
@property
def pdirty(self):
return self.subs["pdirty"]
@property
def mdirty(self):
return self.subs["mdirty"]
@property
def midi(self):
return self.subs["midi"]
@property
def ldirty(self):
return self.subs["ldirty"]
def get(self) -> list:
return [k for k, v in self.subs.items() if v]
def any(self) -> bool:
return any(self.subs.values())
def add(self, event):
self.subs[event] = True
self.info(f"{event} added to")
def remove(self, event):
self.subs[event] = False
self.info(f"{event} removed from")