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:
ttay24
2022-04-06 04:40:11 -05:00
committed by GitHub
parent 8e315fd24d
commit 85e5554c05
+62 -15
View File
@@ -10,33 +10,58 @@ from logging import getLogger
from injector import get_tabs
class FileChangeHandler(FileSystemEventHandler):
def __init__(self, loader) -> None:
def __init__(self, loader, plugin_path) -> None:
super().__init__()
self.logger = getLogger("file-watcher")
self.loader : Loader = loader
self.plugin_path = plugin_path
def on_created(self, event):
src_path = event.src_path
if "__pycache__" in src_path:
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):
src_path = event.src_path
if "__pycache__" in src_path:
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:
def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None:
self.loop = loop
self.logger = getLogger("Loader")
self.plugin_path = plugin_path
self.logger.info(f"plugin_path: {self.plugin_path}")
self.plugins = {}
self.import_plugins()
if live_reload:
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()
server_instance.add_routes([
@@ -48,11 +73,15 @@ class Loader:
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:
spec = spec_from_file_location("_", file)
module = module_from_spec(spec)
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"):
raise KeyError("Plugin {} has not defined a name".format(file))
if module.Plugin.name in self.plugins:
@@ -75,9 +104,12 @@ class Loader:
self.loop.create_task(self.refresh_iframe())
def import_plugins(self):
files = [i for i in listdir(self.plugin_path) if i.endswith(".py")]
for file in files:
self.import_plugin(path.join(self.plugin_path, file))
self.logger.info(f"import plugins from {self.plugin_path}")
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):
self.logger.info("Re-importing plugins.")
@@ -97,15 +129,30 @@ class Loader:
async def load_plugin_main_view(self, request):
plugin = self.plugins[request.match_info["name"]]
ret = """
<script src="/static/library.js"></script>
<script>const plugin_name = '{}' </script>
{}
""".format(plugin.name, plugin.main_view_html)
return web.Response(text=ret, content_type="text/html")
# open up the main template
with open(path.join(self.plugin_path, plugin._plugin_directory, plugin.main_view_html), 'r') as template:
template_data = template.read()
# setup the main script, plugin, and pull in the template
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):
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 = """
<html style="height: fit-content;">
<head>
@@ -119,7 +166,7 @@ class Loader:
{content}
</body>
<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")
@template('plugin_view.html')