34 lines
1 KiB
Python
34 lines
1 KiB
Python
import logging
|
|
|
|
try:
|
|
from watchdog.observers import Observer
|
|
from watchdog.events import (
|
|
FileSystemEventHandler,
|
|
FileCreatedEvent,
|
|
FileModifiedEvent,
|
|
)
|
|
|
|
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:
|
|
logging.info("Watchdog is not installed. Hot reloading unavailable.")
|