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 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] _report_functions : List[Callable]
def __init__(self): def __init__(self):
self._report_functions = [] self._report_functions = []
def register(self, fn:Callable): def add_reporter(self, fn:Callable):
self._report_functions.append(fn) self._report_functions.append(fn)
return fn return fn

View file

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