notifier is an addon and is taken from installed addons

This commit is contained in:
əlemi 2022-04-19 01:56:20 +02:00
parent 7b5958dde3
commit 7b7c46e184
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
2 changed files with 16 additions and 7 deletions

View file

@ -1,12 +1,14 @@
from typing import Callable, List
class Notifier: # TODO this should be an Addon too!
from treepuncher import Addon
class Notifier(Addon): # TODO this should be an Addon too!
_report_functions : List[Callable]
def __init__(self):
self._report_functions = []
def register(self, fn:Callable):
def add_reporter(self, fn:Callable):
self._report_functions.append(fn)
return fn

View file

@ -102,7 +102,7 @@ class Treepuncher(
config: ConfigParser
storage: Storage
notifier: Notifier
notifier: Optional[Notifier]
scheduler: AsyncIOScheduler
modules: List[Addon]
ctx: Dict[Any, Any]
@ -115,7 +115,6 @@ class Treepuncher(
config_file: str = None,
online_mode: bool = True,
legacy: bool = False,
notifier: Notifier = None,
**kwargs
):
self.ctx = dict()
@ -153,8 +152,8 @@ class Treepuncher(
self.storage = Storage(self.name)
self.modules = []
self.notifier = None
self.notifier = notifier or Notifier()
# tz = datetime.datetime.now(datetime.timezone.utc).astimezone().tzname() # This doesn't work anymore
self.scheduler = AsyncIOScheduler() # TODO APScheduler warns about timezone ugghh
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING) # So it's way less spammy
@ -187,6 +186,8 @@ class Treepuncher(
# if self.started: # TODO readd check
# return
await super().start()
if not self.notifier:
self.notifier = Notifier()
await self.notifier.initialize()
for m in self.modules:
await m.initialize()
@ -205,12 +206,18 @@ class Treepuncher(
await self.join_callbacks()
for m in self.modules:
await m.cleanup()
await self.notifier.cleanup()
if self.notifier:
await self.notifier.cleanup()
await super().stop()
self.logger.info("Treepuncher stopped")
def install(self, module: Type[Addon]) -> Type[Addon]:
self.modules.append(module(self))
m = module(self)
self.modules.append(m)
if isinstance(m, Notifier):
if self.notifier:
self.logger.warning("Replacing previously loaded notifier %s", str(self.notifier))
self.notifier = m
return module
async def _work(self):