summaryrefslogtreecommitdiff
path: root/backend/loader.py
diff options
context:
space:
mode:
Diffstat (limited to 'backend/loader.py')
-rw-r--r--backend/loader.py34
1 files changed, 20 insertions, 14 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)