Fixes unbound import of dependency

This commit is contained in:
Maximilian Giller 2022-11-04 09:19:18 +01:00
parent 9483b98e34
commit f83c6d0570

View file

@ -1,7 +1,5 @@
import logging import logging
DEPENDENCY_INSTALLED = None
try: try:
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import ( from watchdog.events import (
@ -10,27 +8,16 @@ try:
FileModifiedEvent, FileModifiedEvent,
) )
DEPENDENCY_INSTALLED = True class HotReloader(FileSystemEventHandler):
logging.debug("Watchdog imported successfully. Hot reloading available.") """Might be unbound, if watchdog is not installed. Check if equal to None before use."""
except ImportError:
DEPENDENCY_INSTALLED = False
logging.info("Watchdog is not installed. Hot reloading unavailable.")
class HotReloader(FileSystemEventHandler):
def __init__(self, path): def __init__(self, path):
if not DEPENDENCY_INSTALLED:
return
self.path = path self.path = path
self.observer = Observer() self.observer = Observer()
self.observer.schedule(self, path, recursive=True) self.observer.schedule(self, path, recursive=True)
self.observer.start() self.observer.start()
def __del__(self): def __del__(self):
if not DEPENDENCY_INSTALLED:
return
self.observer.stop() self.observer.stop()
self.observer.join() self.observer.join()
@ -39,3 +26,9 @@ class HotReloader(FileSystemEventHandler):
def on_created(self, event: FileCreatedEvent): def on_created(self, event: FileCreatedEvent):
logging.info("New config file created. Triggering hot reload.") logging.info("New config file created. Triggering hot reload.")
logging.debug("Watchdog imported successfully. Hot reloading available.")
except ImportError:
logging.info("Watchdog is not installed. Hot reloading unavailable.")