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,32 +8,27 @@ try:
FileModifiedEvent, 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.") logging.debug("Watchdog imported successfully. Hot reloading available.")
except ImportError: except ImportError:
DEPENDENCY_INSTALLED = False
logging.info("Watchdog is not installed. Hot reloading unavailable.") 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.")