diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/loader.py | 34 | ||||
| -rw-r--r-- | backend/plugin.py | 12 |
2 files changed, 25 insertions, 21 deletions
diff --git a/backend/loader.py b/backend/loader.py index 60b1a901..493e7f10 100644 --- a/backend/loader.py +++ b/backend/loader.py @@ -1,4 +1,5 @@ from asyncio import Queue +from json.decoder import JSONDecodeError from logging import getLogger from os import listdir, path from pathlib import Path @@ -6,24 +7,22 @@ from traceback import print_exc from aiohttp import web from genericpath import exists -from json.decoder import JSONDecodeError -from watchdog.events import FileSystemEventHandler -from watchdog.observers.polling import PollingObserver as Observer +from watchdog.events import RegexMatchingEventHandler +from watchdog.observers.inotify import InotifyObserver as Observer -from injector import inject_to_tab, get_tab +from injector import get_tab, inject_to_tab from plugin import PluginWrapper -class FileChangeHandler(FileSystemEventHandler): +class FileChangeHandler(RegexMatchingEventHandler): def __init__(self, queue, plugin_path) -> None: - super().__init__() + super().__init__(regexes=[r'^.*?dist\/index\.js$', r'^.*?main\.py$']) self.logger = getLogger("file-watcher") 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)) @@ -74,7 +73,8 @@ class Loader: web.get("/plugins", self.get_plugins), web.get("/plugins/{plugin_name}/frontend_bundle", self.handle_frontend_bundle), web.post("/plugins/{plugin_name}/methods/{method_name}", self.handle_plugin_method_call), - + web.get("/plugins/{plugin_name}/assets/{path:.*}", self.handle_frontend_assets), + # The following is legacy plugin code. web.get("/plugins/load_main/{name}", self.load_plugin_main_view), web.get("/plugins/plugin_resource/{name}/{path:.+}", self.handle_sub_route), @@ -85,10 +85,16 @@ class Loader: plugins = list(self.plugins.values()) return web.json_response([str(i) if not i.legacy else "$LEGACY_"+str(i) for i in plugins]) - async def handle_frontend_bundle(self, request): + def handle_frontend_assets(self, request): plugin = self.plugins[request.match_info["plugin_name"]] + file = path.join(self.plugin_path, plugin.plugin_directory, "dist/assets", request.match_info["path"]) + + return web.FileResponse(file) - with open(path.join(self.plugin_path, plugin.plugin_directory, plugin.frontend_bundle), 'r') as bundle: + def handle_frontend_bundle(self, request): + plugin = self.plugins[request.match_info["plugin_name"]] + + with open(path.join(self.plugin_path, plugin.plugin_directory, "dist/index.js"), 'r') as bundle: return web.Response(text=bundle.read(), content_type="application/javascript") def import_plugin(self, file, plugin_directory, refresh=False): @@ -145,12 +151,12 @@ class Loader: res["success"] = False return web.json_response(res) - """ + """ The following methods are used to load legacy plugins, which are considered deprecated. I made the choice to re-add them so that the first iteration/version of the react loader - can work as a drop-in replacement for the stable branch of the PluginLoader, so that we + can work as a drop-in replacement for the stable branch of the PluginLoader, so that we can introduce it more smoothly and give people the chance to sample the new features even - without plugin support. They will be removed once legacy plugins are no longer relevant. + without plugin support. They will be removed once legacy plugins are no longer relevant. """ async def load_plugin_main_view(self, request): plugin = self.plugins[request.match_info["name"]] @@ -180,4 +186,4 @@ class Loader: try: return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html") except Exception as e: - return web.Response(text=str(e), status=400)
\ No newline at end of file + return web.Response(text=str(e), status=400) diff --git a/backend/plugin.py b/backend/plugin.py index 502b35bf..e6cceffd 100644 --- a/backend/plugin.py +++ b/backend/plugin.py @@ -10,6 +10,7 @@ from signal import SIGINT, signal from sys import exit from time import time + class PluginWrapper: def __init__(self, file, plugin_directory, plugin_path) -> None: self.file = file @@ -21,13 +22,10 @@ class PluginWrapper: json = load(open(path.join(plugin_path, plugin_directory, "plugin.json"), "r")) - if "frontend_bundle" in json: - self.frontend_bundle = json["frontend_bundle"] - self.legacy = False - else: - self.main_view_html = json["main_view_html"] - self.tile_view_html = json["tile_view_html"] if "tile_view_html" in json else "" - self.legacy = True + self.legacy = False + self.main_view_html = json["main_view_html"] if "main_view_html" in json else "" + self.tile_view_html = json["tile_view_html"] if "tile_view_html" in json else "" + self.legacy = self.main_view_html or self.tile_view_html self.name = json["name"] self.author = json["author"] |
