mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-07-11 19:51:58 +00:00
hot reloading, plugin instantiation, plugin main method
- The Loader now watches for file changes in the plugin directory, and will (re)import when a new plugin is created, or an existing one is modified. This is implemented by means of the watchdog library - Plugin classes are now instantiated (and therefore require a self arg in every method). This way they can maintain a state during the runtime of the loader (or until they are reloaded), and share data between methods. - Plugins can now have a __main() method, which can include long-running code. Every plugin's main method is ran in a separate asyncio task. - Plugin methods that start from __ are now uncallable from javascript. This can be helpful when implementing unfinished/development versions of methods.
This commit is contained in:
+61
-16
@@ -1,17 +1,43 @@
|
|||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from aiohttp_jinja2 import template
|
from aiohttp_jinja2 import template
|
||||||
|
from watchdog.observers.polling import PollingObserver as Observer
|
||||||
|
from watchdog.events import FileSystemEventHandler
|
||||||
|
|
||||||
from os import path, listdir
|
from os import path, listdir
|
||||||
from importlib.util import spec_from_file_location, module_from_spec
|
from importlib.util import spec_from_file_location, module_from_spec
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
|
||||||
import injector
|
from injector import get_tabs
|
||||||
|
|
||||||
|
class FileChangeHandler(FileSystemEventHandler):
|
||||||
|
def __init__(self, loader) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.loader : Loader = loader
|
||||||
|
|
||||||
|
def on_created(self, event):
|
||||||
|
src_path = event.src_path
|
||||||
|
if "__pycache__" in src_path:
|
||||||
|
return
|
||||||
|
self.loader.import_plugin(src_path)
|
||||||
|
|
||||||
|
def on_modified(self, event):
|
||||||
|
src_path = event.src_path
|
||||||
|
if "__pycache__" in src_path:
|
||||||
|
return
|
||||||
|
self.loader.import_plugin(src_path)
|
||||||
|
|
||||||
class Loader:
|
class Loader:
|
||||||
def __init__(self, server_instance, plugin_path) -> None:
|
def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None:
|
||||||
|
self.loop = loop
|
||||||
self.logger = getLogger("Loader")
|
self.logger = getLogger("Loader")
|
||||||
self.plugin_path = plugin_path
|
self.plugin_path = plugin_path
|
||||||
self.plugins = self.import_plugins()
|
self.plugins = {}
|
||||||
|
self.import_plugins()
|
||||||
|
|
||||||
|
if live_reload:
|
||||||
|
self.observer = Observer()
|
||||||
|
self.observer.schedule(FileChangeHandler(self), self.plugin_path)
|
||||||
|
self.observer.start()
|
||||||
|
|
||||||
server_instance.add_routes([
|
server_instance.add_routes([
|
||||||
web.get("/plugins/iframe", self.plugin_iframe_route),
|
web.get("/plugins/iframe", self.plugin_iframe_route),
|
||||||
@@ -21,29 +47,48 @@ class Loader:
|
|||||||
web.get("/steam_resource/{path:.+}", self.get_steam_resource)
|
web.get("/steam_resource/{path:.+}", self.get_steam_resource)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
def import_plugin(self, file):
|
||||||
|
try:
|
||||||
|
spec = spec_from_file_location("_", file)
|
||||||
|
module = module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(module)
|
||||||
|
if not hasattr(module.Plugin, "name"):
|
||||||
|
raise KeyError("Plugin {} has not defined a name".format(file))
|
||||||
|
if module.Plugin.name in self.plugins:
|
||||||
|
if hasattr(module.Plugin, "hot_reload") and not module.Plugin.hot_reload:
|
||||||
|
self.logger.info("Plugin {} is already loaded and has requested to not be re-loaded"
|
||||||
|
.format(module.Plugin.name))
|
||||||
|
else:
|
||||||
|
if hasattr(self.plugins[module.Plugin.name], "task"):
|
||||||
|
self.plugins[module.Plugin.name].task.cancel()
|
||||||
|
self.plugins.pop(module.Plugin.name, None)
|
||||||
|
self.plugins[module.Plugin.name] = module.Plugin()
|
||||||
|
if hasattr(module.Plugin, "__main"):
|
||||||
|
setattr(self.plugins[module.Plugin.name], "task",
|
||||||
|
self.loop.create_task(self.plugins[module.Plugin.name].__main()))
|
||||||
|
self.logger.info("Loaded {}".format(module.Plugin.name))
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error("Could not load {}. {}".format(file, e))
|
||||||
|
|
||||||
def import_plugins(self):
|
def import_plugins(self):
|
||||||
files = [i for i in listdir(self.plugin_path) if i.endswith(".py")]
|
files = [i for i in listdir(self.plugin_path) if i.endswith(".py")]
|
||||||
dc = {}
|
|
||||||
for file in files:
|
for file in files:
|
||||||
try:
|
self.import_plugin(path.join(self.plugin_path, file))
|
||||||
spec = spec_from_file_location("_", path.join(self.plugin_path, file))
|
|
||||||
module = module_from_spec(spec)
|
async def watch_for_file_change(self):
|
||||||
spec.loader.exec_module(module)
|
pass
|
||||||
dc[module.Plugin.name] = module.Plugin
|
|
||||||
self.logger.info("Loaded {}".format(module.Plugin.name))
|
|
||||||
except Exception as e:
|
|
||||||
self.logger.error("Could not load {}. {}".format(file, e))
|
|
||||||
return dc
|
|
||||||
|
|
||||||
async def reload_plugins(self, request=None):
|
async def reload_plugins(self, request=None):
|
||||||
self.logger.info("Re-importing all plugins.")
|
self.logger.info("Re-importing plugins.")
|
||||||
self.plugins = self.import_plugins()
|
self.import_plugins()
|
||||||
|
|
||||||
async def handle_plugin_method_call(self, plugin_name, method_name, **kwargs):
|
async def handle_plugin_method_call(self, plugin_name, method_name, **kwargs):
|
||||||
|
if method_name.startswith("__"):
|
||||||
|
raise RuntimeError("Tried to call private method")
|
||||||
return await getattr(self.plugins[plugin_name], method_name)(**kwargs)
|
return await getattr(self.plugins[plugin_name], method_name)(**kwargs)
|
||||||
|
|
||||||
async def get_steam_resource(self, request):
|
async def get_steam_resource(self, request):
|
||||||
tab = (await injector.get_tabs())[0]
|
tab = (await get_tabs())[0]
|
||||||
return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html")
|
return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html")
|
||||||
|
|
||||||
async def load_plugin(self, request):
|
async def load_plugin(self, request):
|
||||||
|
|||||||
@@ -12,14 +12,15 @@ from utilities import util_methods
|
|||||||
CONFIG = {
|
CONFIG = {
|
||||||
"plugin_path": getenv("PLUGIN_PATH", "/home/deck/homebrew/plugins"),
|
"plugin_path": getenv("PLUGIN_PATH", "/home/deck/homebrew/plugins"),
|
||||||
"server_host": getenv("SERVER_HOST", "127.0.0.1"),
|
"server_host": getenv("SERVER_HOST", "127.0.0.1"),
|
||||||
"server_port": int(getenv("SERVER_PORT", "1337"))
|
"server_port": int(getenv("SERVER_PORT", "1337")),
|
||||||
|
"live_reload": getenv("LIVE_RELOAD", "1") == "1"
|
||||||
}
|
}
|
||||||
|
|
||||||
class PluginManager:
|
class PluginManager:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.loop = get_event_loop()
|
self.loop = get_event_loop()
|
||||||
self.web_app = Application()
|
self.web_app = Application()
|
||||||
self.plugin_loader = Loader(self.web_app, CONFIG["plugin_path"])
|
self.plugin_loader = Loader(self.web_app, CONFIG["plugin_path"], self.loop, CONFIG["live_reload"])
|
||||||
|
|
||||||
jinja_setup(self.web_app, loader=FileSystemLoader(path.join(path.dirname(__file__), 'templates')))
|
jinja_setup(self.web_app, loader=FileSystemLoader(path.join(path.dirname(__file__), 'templates')))
|
||||||
self.web_app.on_startup.append(self.inject_javascript)
|
self.web_app.on_startup.append(self.inject_javascript)
|
||||||
|
|||||||
+7
-2
@@ -7,8 +7,13 @@ class Plugin:
|
|||||||
|
|
||||||
tile_view_html = ""
|
tile_view_html = ""
|
||||||
|
|
||||||
async def method_1(**kwargs):
|
hot_reload = False
|
||||||
|
|
||||||
|
async def __main(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def method_2(**kwargs):
|
async def method_1(self, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def method_2(self, **kwargs):
|
||||||
pass
|
pass
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
aiohttp==3.8.1
|
aiohttp==3.8.1
|
||||||
aiohttp-jinja2==1.5.0
|
aiohttp-jinja2==1.5.0
|
||||||
|
watchdog==2.1.7
|
||||||
Reference in New Issue
Block a user