diff --git a/src/config/hotreloader.py b/src/config/hotreloader.py new file mode 100644 index 0000000..3971239 --- /dev/null +++ b/src/config/hotreloader.py @@ -0,0 +1,41 @@ +import logging + +DEPENDENCY_INSTALLED = None + +try: + from watchdog.observers import Observer + from watchdog.events import ( + FileSystemEventHandler, + FileCreatedEvent, + FileModifiedEvent, + ) + + DEPENDENCY_INSTALLED = True + 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.")