我有一个用Python编写的应用程序,它是由相当专业的观众(科学家)使用的。
我正在寻找一个好方法,使应用程序可扩展的用户,即脚本/插件架构。
I am looking for something extremely lightweight. Most scripts, or plugins, are not going to be developed and distributed by a third-party and installed, but are going to be something whipped up by a user in a few minutes to automate a repeating task, add support for a file format, etc. So plugins should have the absolute minimum boilerplate code, and require no 'installation' other than copying to a folder (so something like setuptools entry points, or the Zope plugin architecture seems like too much.)
是否已经有类似的系统存在,或者是否有项目实现了类似的方案,我应该看看想法/灵感?
你也可以看看“基础工作”。
其思想是围绕可重用组件(称为模式和插件)构建应用程序。插件是派生自GwBasePattern的类。
这里有一个基本的例子:
from groundwork import App
from groundwork.patterns import GwBasePattern
class MyPlugin(GwBasePattern):
def __init__(self, app, **kwargs):
self.name = "My Plugin"
super().__init__(app, **kwargs)
def activate(self):
pass
def deactivate(self):
pass
my_app = App(plugins=[MyPlugin]) # register plugin
my_app.plugins.activate(["My Plugin"]) # activate it
还有更高级的模式来处理命令行接口、信号或共享对象。
foundation可以通过编程方式将插件绑定到应用程序中,如上面所示,也可以通过setuptools自动查找插件。包含插件的Python包必须使用一个特殊的入口点groundwork.plugin声明这些插件。
这是文件。
免责声明:我是《基础》的作者之一。
在我们当前的医疗保健产品中,我们有一个用接口类实现的插件体系结构。我们的技术栈是Django在Python之上的API, Nuxtjs在nodejs之上的前端。
我们为我们的产品编写了一个插件管理器应用程序,它基本上是pip和npm包,遵循Django和Nuxtjs。
对于新插件开发(pip和npm),我们将插件管理器作为依赖项。
在Pip软件包中:
在setup.py的帮助下,你可以添加插件的入口点,用插件管理器做一些事情(注册表,初始化,等等)。
https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation
在npm包中:
与pip类似,npm脚本中有钩子来处理安装。
https://docs.npmjs.com/misc/scripts
我们的usecase:
现在插件开发团队与核心开发团队是分开的。插件开发的范围是与第三方应用程序集成,这些应用程序定义在产品的任何类别中。插件接口分类如下:-传真,电话,电子邮件…等插件管理器可以增强到新的类别。
在你的情况下:也许你可以写一个插件,然后重复使用它来做一些事情。
如果插件开发人员需要使用重用核心对象,可以通过在插件管理器中进行抽象级别来使用该对象,以便任何插件都可以继承这些方法。
只是分享一下我们是如何在我们的产品中实现的,希望能给大家一点启发。
module_example.py:
def plugin_main(*args, **kwargs):
print args, kwargs
loader.py:
def load_plugin(name):
mod = __import__("module_%s" % name)
return mod
def call_plugin(name, *args, **kwargs):
plugin = load_plugin(name)
plugin.plugin_main(*args, **kwargs)
call_plugin("example", 1234)
它当然是“最小的”,它绝对没有错误检查,可能有无数的安全问题,它不是很灵活-但它应该告诉你一个Python插件系统可以多么简单。
你可能也想看看imp模块,尽管你可以用__import__, os。Listdir和一些字符串操作。