Repository navigation
Description
This was something I was thinking of during the hiatus
Store plugins in a folder, maybe ~/AppData/Roaming/rovr/plugins; likewise, versions for other platforms
Then, in this folder, you can install a plugin, so like
~/AppData/Roaming/rovr/plugins
|-> everything/
| |-> .git/
| |-> README.md
| |-> main.py # contains main widget
| \-> other stuff/ # yes
|
|-> plocate/
| |-> .git/
| |-> README.md
| |-> main.py # contains main widget
main.py contains source code; the main widget must be a button that can be placed in the menu area, along with copy, cut, paste, rename, delete, etc
An example file would be
from textual.widgets import Button
class EverythingSearchButton(Button):
def on_mount(self) -> None:
self.app.action_open_everything = self.action_open_everything
self.display = False
async def action_open_everything(self) -> None:
...
__widget__ = EverythingSearchButtonThen rovr will check each folder for __widget__, check mro for Button and if so, mounts it to #menu, boom plugin system
This also means people can monkeypatch, but just mark that as a feature, I guess, pros and cons of using Python
Then we could add onto rovr, like I guess rovr --plug clone <link to git repo> and rovr will clone with shallow, check contents, ast.parse check for __widget__, if not list and is class, import it, check issubclass, if it is, then allow plugin to exist
Also consider a --ignore-plugins to run without plugins in the case that anything fails.
For enabling and disabling, we could just extend the [plugins] table, kind of like
[plugins.bat]
...
[plugins.everything]
enabled = true
executable = "es.exe"If the plugin's enabled is not there, assume it is disabled
Loading a plugin into a namespace is also the best idea to support child imports, so
import importlib.util, sys, types
from pathlib import Path
def load_plugin(folder: Path) -> types.ModuleType:
# Assume folder.name is a validated, unique Python identifier.
name = f"_rovr_plugin_{folder.name}"
spec = importlib.util.spec_from_file_location(
name,
folder / "main.py",
submodule_search_locations=[str(folder)],
)
if spec is None or spec.loader is None:
raise ImportError(f"Cannot load plugin: {folder.name}")
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
try:
spec.loader.exec_module(module)
except BaseException:
for key in tuple(sys.modules):
if key == name or key.startswith(name + "."):
del sys.modules[key]
raise
return moduleso that such loaded modules are now under
_rovr_plugin_everything
_rovr_plugin_everything.search_screen
and importing becomes
from .search_screen import EverythingSearchScreenPreferably don't add hot reloading; it becomes an absolute mess.
Also, since rovr is importing the plugin into its own venv (or is using the compiled executable), importing external modules is probably not possible unless the modules do some fancy importing with importlib (actually, I think that seems quite reasonable; I could see people doing that tbh).
However, need to consider certain widgets not being compiled because Nuitka is actually considering the lazy importing of textual.widgets, which means certain widgets like DataTable, MarkdownViewer, Tree may not be available (didn't check though; should check)
Activity