mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-07-11 11:31:58 +00:00
Feature/20 plugins as folders (#24)
* updated loader to watch directories correctly and pull in main.py; also made sure we pull in main.py; WIP on pulling in the template correctly * Making other changes to support pulling in templates from other pages
This commit is contained in:
+62
-15
@@ -10,33 +10,58 @@ from logging import getLogger
|
|||||||
from injector import get_tabs
|
from injector import get_tabs
|
||||||
|
|
||||||
class FileChangeHandler(FileSystemEventHandler):
|
class FileChangeHandler(FileSystemEventHandler):
|
||||||
def __init__(self, loader) -> None:
|
def __init__(self, loader, plugin_path) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.logger = getLogger("file-watcher")
|
||||||
self.loader : Loader = loader
|
self.loader : Loader = loader
|
||||||
|
self.plugin_path = plugin_path
|
||||||
|
|
||||||
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:
|
||||||
return
|
return
|
||||||
self.loader.import_plugin(src_path, refresh=True)
|
|
||||||
|
# check to make sure this isn't a directory
|
||||||
|
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")
|
||||||
|
if not path.isfile(main_file_path):
|
||||||
|
return
|
||||||
|
self.loader.import_plugin(main_file_path, plugin_dir, refresh=True)
|
||||||
|
|
||||||
def on_modified(self, event):
|
def on_modified(self, event):
|
||||||
src_path = event.src_path
|
src_path = event.src_path
|
||||||
if "__pycache__" in src_path:
|
if "__pycache__" in src_path:
|
||||||
return
|
return
|
||||||
self.loader.import_plugin(src_path, refresh=True)
|
|
||||||
|
# check to make sure this isn't a directory
|
||||||
|
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.loader.import_plugin(path.join(self.plugin_path, plugin_dir, "main.py"), plugin_dir, refresh=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:
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
self.logger = getLogger("Loader")
|
self.logger = getLogger("Loader")
|
||||||
self.plugin_path = plugin_path
|
self.plugin_path = plugin_path
|
||||||
|
self.logger.info(f"plugin_path: {self.plugin_path}")
|
||||||
self.plugins = {}
|
self.plugins = {}
|
||||||
self.import_plugins()
|
self.import_plugins()
|
||||||
|
|
||||||
if live_reload:
|
if live_reload:
|
||||||
self.observer = Observer()
|
self.observer = Observer()
|
||||||
self.observer.schedule(FileChangeHandler(self), self.plugin_path)
|
self.observer.schedule(FileChangeHandler(self, plugin_path), self.plugin_path, recursive=True)
|
||||||
self.observer.start()
|
self.observer.start()
|
||||||
|
|
||||||
server_instance.add_routes([
|
server_instance.add_routes([
|
||||||
@@ -48,11 +73,15 @@ 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, refresh=False):
|
def import_plugin(self, file, plugin_directory, refresh=False):
|
||||||
try:
|
try:
|
||||||
spec = spec_from_file_location("_", file)
|
spec = spec_from_file_location("_", file)
|
||||||
module = module_from_spec(spec)
|
module = module_from_spec(spec)
|
||||||
spec.loader.exec_module(module)
|
spec.loader.exec_module(module)
|
||||||
|
|
||||||
|
# add member for what directory the given plugin lives under
|
||||||
|
module.Plugin._plugin_directory = plugin_directory
|
||||||
|
|
||||||
if not hasattr(module.Plugin, "name"):
|
if not hasattr(module.Plugin, "name"):
|
||||||
raise KeyError("Plugin {} has not defined a name".format(file))
|
raise KeyError("Plugin {} has not defined a name".format(file))
|
||||||
if module.Plugin.name in self.plugins:
|
if module.Plugin.name in self.plugins:
|
||||||
@@ -75,9 +104,12 @@ class Loader:
|
|||||||
self.loop.create_task(self.refresh_iframe())
|
self.loop.create_task(self.refresh_iframe())
|
||||||
|
|
||||||
def import_plugins(self):
|
def import_plugins(self):
|
||||||
files = [i for i in listdir(self.plugin_path) if i.endswith(".py")]
|
self.logger.info(f"import plugins from {self.plugin_path}")
|
||||||
for file in files:
|
|
||||||
self.import_plugin(path.join(self.plugin_path, file))
|
directories = [i for i in listdir(self.plugin_path) if path.isdir(path.join(self.plugin_path, i)) and path.isfile(path.join(self.plugin_path, i, "main.py"))]
|
||||||
|
for directory in directories:
|
||||||
|
self.logger.info(f"found plugin: {directory}")
|
||||||
|
self.import_plugin(path.join(self.plugin_path, directory, "main.py"), directory)
|
||||||
|
|
||||||
async def reload_plugins(self, request=None):
|
async def reload_plugins(self, request=None):
|
||||||
self.logger.info("Re-importing plugins.")
|
self.logger.info("Re-importing plugins.")
|
||||||
@@ -97,15 +129,30 @@ class Loader:
|
|||||||
|
|
||||||
async def load_plugin_main_view(self, request):
|
async def load_plugin_main_view(self, request):
|
||||||
plugin = self.plugins[request.match_info["name"]]
|
plugin = self.plugins[request.match_info["name"]]
|
||||||
ret = """
|
|
||||||
<script src="/static/library.js"></script>
|
# open up the main template
|
||||||
<script>const plugin_name = '{}' </script>
|
with open(path.join(self.plugin_path, plugin._plugin_directory, plugin.main_view_html), 'r') as template:
|
||||||
{}
|
template_data = template.read()
|
||||||
""".format(plugin.name, plugin.main_view_html)
|
# setup the main script, plugin, and pull in the template
|
||||||
return web.Response(text=ret, content_type="text/html")
|
ret = """
|
||||||
|
<script src="/static/library.js"></script>
|
||||||
|
<script>const plugin_name = '{}' </script>
|
||||||
|
{}
|
||||||
|
""".format(plugin.name, template_data)
|
||||||
|
return web.Response(text=ret, content_type="text/html")
|
||||||
|
|
||||||
async def load_plugin_tile_view(self, request):
|
async def load_plugin_tile_view(self, request):
|
||||||
plugin = self.plugins[request.match_info["name"]]
|
plugin = self.plugins[request.match_info["name"]]
|
||||||
|
|
||||||
|
inner_content = ""
|
||||||
|
|
||||||
|
# open up the tile template (if we have one defined)
|
||||||
|
if len(plugin.tile_view_html) > 0:
|
||||||
|
with open(path.join(self.plugin_path, plugin._plugin_directory, plugin.tile_view_html), 'r') as template:
|
||||||
|
template_data = template.read()
|
||||||
|
inner_content = template_data
|
||||||
|
|
||||||
|
# setup the default template
|
||||||
ret = """
|
ret = """
|
||||||
<html style="height: fit-content;">
|
<html style="height: fit-content;">
|
||||||
<head>
|
<head>
|
||||||
@@ -119,7 +166,7 @@ class Loader:
|
|||||||
{content}
|
{content}
|
||||||
</body>
|
</body>
|
||||||
<html>
|
<html>
|
||||||
""".format(name=plugin.name, content=plugin.tile_view_html)
|
""".format(name=plugin.name, content=inner_content)
|
||||||
return web.Response(text=ret, content_type="text/html")
|
return web.Response(text=ret, content_type="text/html")
|
||||||
|
|
||||||
@template('plugin_view.html')
|
@template('plugin_view.html')
|
||||||
|
|||||||
Reference in New Issue
Block a user