From f83c6d0570936b328c596ee8fea4d6ea955624ac Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 4 Nov 2022 09:19:18 +0100 Subject: [PATCH] Fixes unbound import of dependency --- src/config/hotreloader.py | 49 +++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/config/hotreloader.py b/src/config/hotreloader.py index a6377c4..0ca4010 100644 --- a/src/config/hotreloader.py +++ b/src/config/hotreloader.py @@ -1,7 +1,5 @@ import logging -DEPENDENCY_INSTALLED = None - try: from watchdog.observers import Observer from watchdog.events import ( @@ -10,32 +8,27 @@ try: FileModifiedEvent, ) - DEPENDENCY_INSTALLED = True + class HotReloader(FileSystemEventHandler): + """Might be unbound, if watchdog is not installed. Check if equal to None before use.""" + + def __init__(self, path): + self.path = path + self.observer = Observer() + self.observer.schedule(self, path, recursive=True) + self.observer.start() + + def __del__(self): + self.observer.stop() + self.observer.join() + + def on_modified(self, event: FileModifiedEvent): + logging.info("Config file modified. Triggering hot reload.") + + def on_created(self, event: FileCreatedEvent): + logging.info("New config file created. Triggering hot reload.") + logging.debug("Watchdog imported successfully. Hot reloading available.") + + except ImportError: - DEPENDENCY_INSTALLED = False logging.info("Watchdog is not installed. Hot reloading unavailable.") - - -class HotReloader(FileSystemEventHandler): - def __init__(self, path): - if not DEPENDENCY_INSTALLED: - return - - self.path = path - self.observer = Observer() - self.observer.schedule(self, path, recursive=True) - self.observer.start() - - def __del__(self): - if not DEPENDENCY_INSTALLED: - return - - self.observer.stop() - self.observer.join() - - def on_modified(self, event: FileModifiedEvent): - logging.info("Config file modified. Triggering hot reload.") - - def on_created(self, event: FileCreatedEvent): - logging.info("New config file created. Triggering hot reload.")