Skip to content

Commit

Permalink
Fix hot-reload when there are subdirs (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyCrazy authored May 10, 2022
1 parent cb9b888 commit 414e0da
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions plugin_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from os import path, listdir
from logging import getLogger
from time import time
from genericpath import exists
from pathlib import Path

from injector import get_tabs, get_tab
from plugin import PluginWrapper
Expand All @@ -18,6 +20,12 @@ def __init__(self, queue, plugin_path) -> None:
self.plugin_path = plugin_path
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):
src_path = event.src_path
if "__pycache__" in src_path:
Expand All @@ -27,13 +35,8 @@ def on_created(self, event):
if path.isdir(src_path):
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}")
rel_path = path.relpath(src_path, path.commonprefix([self.plugin_path, 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))
self.maybe_reload(src_path)

def on_modified(self, event):
src_path = event.src_path
Expand All @@ -44,11 +47,8 @@ def on_modified(self, event):
if path.isdir(src_path):
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}")
plugin_dir = path.split(path.relpath(src_path, path.commonprefix([self.plugin_path, src_path])))[0]
self.queue.put_nowait((path.join(self.plugin_path, plugin_dir, "main.py"), plugin_dir, True))
self.maybe_reload(src_path)

class Loader:
def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None:
Expand Down

0 comments on commit 414e0da

Please sign in to comment.