Fix hot-reload when there are subdirs (#56)

This commit is contained in:
Jonas Dellinger
2022-05-11 01:11:14 +02:00
committed by GitHub
parent cb9b888dc6
commit 414e0da2f3
+10 -10
View File
@@ -6,6 +6,8 @@ from asyncio import Queue
from os import path, listdir from os import path, listdir
from logging import getLogger from logging import getLogger
from time import time from time import time
from genericpath import exists
from pathlib import Path
from injector import get_tabs, get_tab from injector import get_tabs, get_tab
from plugin import PluginWrapper from plugin import PluginWrapper
@@ -18,6 +20,12 @@ class FileChangeHandler(FileSystemEventHandler):
self.plugin_path = plugin_path self.plugin_path = plugin_path
self.queue = queue self.queue = queue
def maybe_reload(self, src_path):
plugin_dir = Path(path.relpath(src_path, self.plugin_path)).parts[0]
self.logger.info(path.join(self.plugin_path, plugin_dir, "plugin.json"))
if exists(path.join(self.plugin_path, plugin_dir, "plugin.json")):
self.queue.put_nowait((path.join(self.plugin_path, plugin_dir, "main.py"), plugin_dir, True))
def on_created(self, event): def on_created(self, event):
src_path = event.src_path src_path = event.src_path
if "__pycache__" in src_path: if "__pycache__" in src_path:
@@ -27,13 +35,8 @@ class FileChangeHandler(FileSystemEventHandler):
if path.isdir(src_path): if path.isdir(src_path):
return return
# get the directory name of the plugin so that we can find its "main.py" and reload it; the
# file that changed is not necessarily the one that needs to be reloaded
self.logger.debug(f"file created: {src_path}") self.logger.debug(f"file created: {src_path}")
rel_path = path.relpath(src_path, path.commonprefix([self.plugin_path, src_path])) self.maybe_reload(src_path)
plugin_dir = path.split(rel_path)[0]
main_file_path = path.join(self.plugin_path, plugin_dir, "main.py")
self.queue.put_nowait((main_file_path, plugin_dir, True))
def on_modified(self, event): def on_modified(self, event):
src_path = event.src_path src_path = event.src_path
@@ -44,11 +47,8 @@ class FileChangeHandler(FileSystemEventHandler):
if path.isdir(src_path): if path.isdir(src_path):
return return
# get the directory name of the plugin so that we can find its "main.py" and reload it; the
# file that changed is not necessarily the one that needs to be reloaded
self.logger.debug(f"file modified: {src_path}") self.logger.debug(f"file modified: {src_path}")
plugin_dir = path.split(path.relpath(src_path, path.commonprefix([self.plugin_path, src_path])))[0] self.maybe_reload(src_path)
self.queue.put_nowait((path.join(self.plugin_path, plugin_dir, "main.py"), plugin_dir, True))
class Loader: class Loader:
def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None: def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None: