Merge pull request 'v0.0.1' (#5) from v0.0.1 into main
Reviewed-on: https://gitea.com/Lerking/XtendR/pulls/5
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
# xtendr
|
||||
# XtendR
|
||||
|
||||
A python 3 extension system to ease the use of plugins.
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
from xtendr.xtendr import XtendR
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""Example usage of the PluginSystem.
|
||||
|
||||
Example:
|
||||
>>> system = XtendR()
|
||||
>>> system.attach("example_plugin")
|
||||
Attached plugin 'example_plugin'.
|
||||
>>> system.run("example_plugin")
|
||||
ExamplePlugin is running!
|
||||
>>> system.stop("example_plugin")
|
||||
ExamplePlugin has stopped!
|
||||
>>> system.detach("example_plugin")
|
||||
Detached plugin 'example_plugin'.
|
||||
"""
|
||||
system = XtendR()
|
||||
system.attach("example_plugin") # Assuming 'example_plugin/plugin_info.json' exists
|
||||
system.run("example_plugin")
|
||||
system.stop("example_plugin")
|
||||
system.detach("example_plugin")
|
||||
@@ -0,0 +1,19 @@
|
||||
if __name__ == "__main__":
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name="XtendR",
|
||||
version="0.0.1",
|
||||
packages=find_packages(),
|
||||
install_requires=[],
|
||||
author="Jan Lerking",
|
||||
author_email="",
|
||||
description="A modular plugin system for Python.",
|
||||
url="",
|
||||
classifiers=[
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
],
|
||||
python_requires='>=3.12',
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "Example Plugin",
|
||||
"version": "1.0",
|
||||
"module": "example_plugin",
|
||||
"description": "An example plugin that prints a message."
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
from xtendr import XtendRBase
|
||||
|
||||
class ExamplePlugin(XtendRBase):
|
||||
"""Example plugin implementation.
|
||||
|
||||
Example:
|
||||
>>> plugin = ExamplePlugin()
|
||||
>>> plugin.run()
|
||||
ExamplePlugin is running!
|
||||
>>> plugin.stop()
|
||||
ExamplePlugin has stopped!
|
||||
"""
|
||||
def run(self):
|
||||
print("ExamplePlugin is running!")
|
||||
|
||||
def stop(self):
|
||||
print("ExamplePlugin has stopped!")
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<rect width="64" height="64" fill="none"/>
|
||||
<path d="M16 32H48M32 16V48" stroke="currentColor" stroke-width="6" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<circle cx="16" cy="32" r="4" fill="currentColor"/>
|
||||
<circle cx="48" cy="32" r="4" fill="currentColor"/>
|
||||
<circle cx="32" cy="16" r="4" fill="currentColor"/>
|
||||
<circle cx="32" cy="48" r="4" fill="currentColor"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 534 B |
@@ -0,0 +1 @@
|
||||
import xtendr
|
||||
@@ -0,0 +1,109 @@
|
||||
import importlib
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class XtendRBase(ABC):
|
||||
"""Abstract base class for all plugins.
|
||||
|
||||
Example:
|
||||
>>> class TestPlugin(XtendRBase):
|
||||
... def run(self):
|
||||
... print("Running TestPlugin")
|
||||
... def stop(self):
|
||||
... print("Stopping TestPlugin")
|
||||
|
||||
>>> plugin = TestPlugin()
|
||||
>>> plugin.run()
|
||||
Running TestPlugin
|
||||
>>> plugin.stop()
|
||||
Stopping TestPlugin
|
||||
"""
|
||||
@abstractmethod
|
||||
def run(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def stop(self):
|
||||
pass
|
||||
|
||||
class XtendR:
|
||||
"""Plugin system to manage plugins.
|
||||
|
||||
Example:
|
||||
>>> system = XtendRSystem()
|
||||
>>> system.attach("example_plugin") # Assuming 'example_plugin/plugin_info.json' exists
|
||||
>>> system.run("example_plugin")
|
||||
ExamplePlugin is running!
|
||||
>>> system.stop("example_plugin")
|
||||
ExamplePlugin has stopped!
|
||||
>>> system.detach("example_plugin")
|
||||
Detached plugin 'example_plugin'.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.plugins = {}
|
||||
|
||||
def attach(self, name: str) -> None:
|
||||
"""Dynamically load a plugin from its folder."""
|
||||
if name in self.plugins:
|
||||
print(f"Plugin '{name}' is already attached.")
|
||||
return
|
||||
|
||||
plugin_path = os.path.join(os.getcwd(), name)
|
||||
info_path = os.path.join(plugin_path, "plugin_info.json")
|
||||
|
||||
if not os.path.isdir(plugin_path) or not os.path.isfile(info_path):
|
||||
print(f"Failed to attach plugin '{name}', folder or info file not found.")
|
||||
return
|
||||
|
||||
try:
|
||||
with open(info_path, "r", encoding="utf-8") as f:
|
||||
plugin_info = json.load(f)
|
||||
module_name = plugin_info.get("module")
|
||||
class_name = plugin_info.get("class")
|
||||
if not module_name or not class_name:
|
||||
print(f"Plugin '{name}' info file is missing 'module' or 'class' key.")
|
||||
return
|
||||
|
||||
sys.path.insert(0, plugin_path)
|
||||
module = importlib.import_module(module_name)
|
||||
plugin_class = getattr(module, class_name)
|
||||
instance = plugin_class()
|
||||
|
||||
if not isinstance(instance, XtendRBase):
|
||||
print(f"Plugin '{name}' does not inherit from PluginBase.")
|
||||
return
|
||||
|
||||
self.plugins[name] = {
|
||||
'instance': instance,
|
||||
'running': False,
|
||||
'info': plugin_info
|
||||
}
|
||||
print(f"Attached plugin '{name}'.")
|
||||
except (ModuleNotFoundError, json.JSONDecodeError, AttributeError) as e:
|
||||
print(f"Failed to attach plugin '{name}': {e}")
|
||||
|
||||
def run(self, name: str, *args, **kwargs):
|
||||
"""Run the plugin's 'run' method if available."""
|
||||
if name in self.plugins:
|
||||
self.plugins[name]['running'] = True
|
||||
return self.plugins[name]['instance'].run(*args, **kwargs)
|
||||
print(f"Plugin '{name}' not found or has no 'run' method.")
|
||||
|
||||
def stop(self, name: str) -> None:
|
||||
"""Stop the plugin if it's running."""
|
||||
if name in self.plugins and self.plugins[name]['running']:
|
||||
self.plugins[name]['running'] = False
|
||||
self.plugins[name]['instance'].stop()
|
||||
else:
|
||||
print(f"Plugin '{name}' is not running.")
|
||||
|
||||
def detach(self, name: str) -> None:
|
||||
"""Unload a plugin."""
|
||||
if name in self.plugins:
|
||||
del self.plugins[name]
|
||||
sys.modules.pop(name, None)
|
||||
print(f"Detached plugin '{name}'.")
|
||||
else:
|
||||
print(f"Plugin '{name}' is not attached.")
|
||||
Reference in New Issue
Block a user