From 462a3afaea0aae1bec06928209a49143ce6389c2 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sat, 4 May 2024 11:43:26 -0500 Subject: [PATCH] added callbacks --- directory_monitor.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/directory_monitor.py b/directory_monitor.py index 2cf10ef..38e2486 100644 --- a/directory_monitor.py +++ b/directory_monitor.py @@ -3,7 +3,7 @@ import logging from dataclasses import dataclass, field from datetime import datetime from pathlib import Path -from typing import AsyncIterable, Dict, Iterable, List +from typing import AsyncIterable, Awaitable, Callable, Dict, Iterable, List import anyio @@ -15,10 +15,10 @@ class DirectoryMonitor: dir: Path glob: str = '*' times: Dict[Path | anyio.Path, float] = field(default_factory=dict, repr=False) - changed: List[Path | anyio.Path] = field(init=False, repr=False) - new: List[Path | anyio.Path] = field(init=False, repr=False) - deleted: List[Path | anyio.Path] = field(init=False, repr=False) last_checked: datetime = field(default_factory=datetime.now) + new: List[Path | anyio.Path] = field(init=False, repr=False) + changed: List[Path | anyio.Path] = field(init=False, repr=False) + deleted: List[Path | anyio.Path] = field(init=False, repr=False) def _reset_counts(self): self.last_checked = datetime.now() @@ -68,7 +68,7 @@ class DirectoryMonitor: self._delete_file(file) async def update_async(self): - logger.debug('Async update') + # logger.debug('Async update') self._reset_counts() async for file in self.async_rglob(): @@ -79,20 +79,6 @@ class DirectoryMonitor: if not (await file.exists()): self._delete_file(file) - async def monitor(self): - while True: - try: - await self.update_async() - except KeyboardInterrupt: - # if a KeyboardInterrupt happens during the update, pass it through - raise - except Exception as e: - # if anything unexpected happens, log it and break the loop cleanly - logger.exception(e) - break - else: - await asyncio.sleep(1.0) - @dataclass class AppConfigMonitor: @@ -107,7 +93,11 @@ class AppConfigMonitor: self.config_monitor = DirectoryMonitor(self.dir, '*.yaml') self.module_monitor = DirectoryMonitor(self.dir, '*.py') - async def monitor(self): + async def monitor( + self, + config_update_callback: Callable | Awaitable = None, + module_update_callback: Callable | Awaitable = None, + ): while True: try: await self.config_monitor.update_async() @@ -120,6 +110,16 @@ class AppConfigMonitor: logger.exception(e) break else: + if isinstance(config_update_callback, Callable): + config_update_callback(self.config_monitor) + elif isinstance(config_update_callback, Awaitable): + await config_update_callback(self.config_monitor) + + if isinstance(module_update_callback, Callable): + module_update_callback(self.module_monitor) + elif isinstance(module_update_callback, Awaitable): + await module_update_callback(self.module_monitor) + await asyncio.sleep(self.poll_sleep_time)