started working on getting plugins working
This commit is contained in:
3
main.py
3
main.py
@@ -8,6 +8,8 @@ from assets.theme_mappings import theme_mappings
|
|||||||
|
|
||||||
from textual_fspicker import FileOpen, FileSave
|
from textual_fspicker import FileOpen, FileSave
|
||||||
|
|
||||||
|
from plugin_loader import PluginLoader
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -54,6 +56,7 @@ class Berry(App):
|
|||||||
# yield Terminal(command="bash", id="terminal")
|
# yield Terminal(command="bash", id="terminal")
|
||||||
|
|
||||||
yield Footer()
|
yield Footer()
|
||||||
|
yield PluginLoader()
|
||||||
|
|
||||||
def on_input_submitted(self, event: Input.Submitted):
|
def on_input_submitted(self, event: Input.Submitted):
|
||||||
if event.input.id != "console-input":
|
if event.input.id != "console-input":
|
||||||
|
|||||||
74
plugin_loader.py
Normal file
74
plugin_loader.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
from textual_window import Window
|
||||||
|
from textual.widgets import RichLog
|
||||||
|
|
||||||
|
import os, json
|
||||||
|
|
||||||
|
|
||||||
|
class PluginLoader(Window):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
|
id="Plugin Loader",
|
||||||
|
mode="permanent",
|
||||||
|
icon="⚙️",
|
||||||
|
starting_horizontal="centerright",
|
||||||
|
starting_vertical="lowermiddle",
|
||||||
|
start_open=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def on_mount(self):
|
||||||
|
log = self.query_one(RichLog)
|
||||||
|
log.write("[b]Finding plugins...[/]")
|
||||||
|
|
||||||
|
# Find all plugins (they're just in the plugins folder for now)
|
||||||
|
folders = [
|
||||||
|
os.path.join(os.path.dirname(__file__), "plugins")
|
||||||
|
]
|
||||||
|
# path to the folder of all correctly formatted plugins
|
||||||
|
plugin_paths = []
|
||||||
|
|
||||||
|
for folder in folders:
|
||||||
|
log.write(f"Searching {folder}...")
|
||||||
|
plugin_folders = os.listdir(folder)
|
||||||
|
|
||||||
|
for plugin_folder in plugin_folders:
|
||||||
|
plugin_folder = os.path.join(folder, plugin_folder)
|
||||||
|
|
||||||
|
if not os.path.isdir(plugin_folder):
|
||||||
|
log.write(f"[d]Ignoring {plugin_folder} because it is not a folder.[/]")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not os.path.isdir(os.path.join(plugin_folder, "lua")):
|
||||||
|
log.write(f"[d]Ignoring {plugin_folder} because it has no \"lua\" folder.[/]")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not os.path.isfile(os.path.join(plugin_folder, "plugin.json")):
|
||||||
|
log.write(f"[d]Ignoring {plugin_folder} because it has no plugin.json file.[/]")
|
||||||
|
continue
|
||||||
|
|
||||||
|
with open(os.path.join(plugin_folder, "plugin.json"), "r") as f:
|
||||||
|
try:
|
||||||
|
plugin_json = json.loads(f.read())
|
||||||
|
plugin_json["name"]
|
||||||
|
plugin_json["version"]
|
||||||
|
plugin_json["author"]
|
||||||
|
plugin_json["dependencies"]
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is unreadable.[/]")
|
||||||
|
continue
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is malformed.[/]")
|
||||||
|
continue
|
||||||
|
except KeyError as e:
|
||||||
|
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is missing the field {e}.")
|
||||||
|
continue
|
||||||
|
except Exception as e:
|
||||||
|
log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]")
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
log.write(f"[b green]FOUND[/] {plugin_json['name']} ({plugin_json['version']})")
|
||||||
|
plugin_paths.append(plugin_folder)
|
||||||
|
|
||||||
|
def compose(self):
|
||||||
|
yield RichLog(markup=True, id="plugins-log")
|
||||||
6
plugins/test-plugin/plugin.json
Normal file
6
plugins/test-plugin/plugin.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"version": "0.0.1",
|
||||||
|
"name": "Test Plugin",
|
||||||
|
"author": "SpookyDervish",
|
||||||
|
"dependencies": []
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user