2022-09-29 09:44:14 +01:00
|
|
|
import logging
|
2022-10-06 18:07:34 +01:00
|
|
|
from typing import Iterable, Union
|
2022-09-29 09:44:14 +01:00
|
|
|
|
2023-06-23 03:45:03 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-09-29 09:44:14 +01:00
|
|
|
|
|
|
|
class Event:
|
|
|
|
"""Keeps track of event subscriptions"""
|
|
|
|
|
|
|
|
def __init__(self, subs: dict):
|
|
|
|
self.subs = subs
|
2023-06-23 03:45:03 +01:00
|
|
|
self.logger = logger.getChild(self.__class__.__name__)
|
2022-09-29 09:44:14 +01:00
|
|
|
|
|
|
|
def info(self, msg=None):
|
|
|
|
info = (f"{msg} events",) if msg else ()
|
|
|
|
if self.any():
|
|
|
|
info += (f"now listening for {', '.join(self.get())} events",)
|
|
|
|
else:
|
|
|
|
info += (f"not listening for any events",)
|
|
|
|
self.logger.info(", ".join(info))
|
|
|
|
|
|
|
|
@property
|
2022-10-06 16:45:08 +01:00
|
|
|
def pdirty(self) -> bool:
|
2022-09-29 09:44:14 +01:00
|
|
|
return self.subs["pdirty"]
|
|
|
|
|
2022-10-06 16:45:08 +01:00
|
|
|
@pdirty.setter
|
|
|
|
def pdirty(self, val: bool):
|
|
|
|
self.subs["pdirty"] = val
|
2022-10-06 16:50:00 +01:00
|
|
|
self.info(f"pdirty {'added to' if val else 'removed from'}")
|
2022-10-06 16:45:08 +01:00
|
|
|
|
2022-09-29 09:44:14 +01:00
|
|
|
@property
|
2022-10-06 16:45:08 +01:00
|
|
|
def mdirty(self) -> bool:
|
2022-09-29 09:44:14 +01:00
|
|
|
return self.subs["mdirty"]
|
|
|
|
|
2022-10-06 16:45:08 +01:00
|
|
|
@mdirty.setter
|
|
|
|
def mdirty(self, val: bool):
|
|
|
|
self.subs["mdirty"] = val
|
2022-10-06 16:50:00 +01:00
|
|
|
self.info(f"mdirty {'added to' if val else 'removed from'}")
|
2022-10-06 16:45:08 +01:00
|
|
|
|
2022-09-29 09:44:14 +01:00
|
|
|
@property
|
2022-10-06 16:45:08 +01:00
|
|
|
def midi(self) -> bool:
|
2022-09-29 09:44:14 +01:00
|
|
|
return self.subs["midi"]
|
|
|
|
|
2022-10-06 16:45:08 +01:00
|
|
|
@midi.setter
|
|
|
|
def midi(self, val: bool):
|
|
|
|
self.subs["midi"] = val
|
2022-10-06 16:50:00 +01:00
|
|
|
self.info(f"midi {'added to' if val else 'removed from'}")
|
2022-10-06 16:45:08 +01:00
|
|
|
|
2022-09-29 09:44:14 +01:00
|
|
|
@property
|
2022-10-06 16:45:08 +01:00
|
|
|
def ldirty(self) -> bool:
|
2022-09-29 09:44:14 +01:00
|
|
|
return self.subs["ldirty"]
|
|
|
|
|
2022-10-06 16:45:08 +01:00
|
|
|
@ldirty.setter
|
|
|
|
def ldirty(self, val: bool):
|
|
|
|
self.subs["ldirty"] = val
|
2022-10-06 16:50:00 +01:00
|
|
|
self.info(f"ldirty {'added to' if val else 'removed from'}")
|
2022-10-06 16:45:08 +01:00
|
|
|
|
2022-09-29 09:44:14 +01:00
|
|
|
def get(self) -> list:
|
|
|
|
return [k for k, v in self.subs.items() if v]
|
|
|
|
|
|
|
|
def any(self) -> bool:
|
|
|
|
return any(self.subs.values())
|
|
|
|
|
2022-10-06 18:07:34 +01:00
|
|
|
def add(self, events: Union[str, Iterable[str]]):
|
|
|
|
if isinstance(events, str):
|
|
|
|
events = [events]
|
|
|
|
for event in events:
|
|
|
|
setattr(self, event, True)
|
|
|
|
|
|
|
|
def remove(self, events: Union[str, Iterable[str]]):
|
|
|
|
if isinstance(events, str):
|
|
|
|
events = [events]
|
|
|
|
for event in events:
|
|
|
|
setattr(self, event, False)
|