用 Python 构建最小的插件架构 [英] Building a minimal plugin architecture in Python

查看:24
本文介绍了用 Python 构建最小的插件架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Python 编写的应用程序,它被相当技术性的受众(科学家)使用.

I have an application, written in Python, which is used by a fairly technical audience (scientists).

我正在寻找一种让用户可以扩展应用程序的好方法,即脚本/插件架构.

I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture.

我正在寻找极轻的东西.大多数脚本或插件不会由第三方开发和分发并安装,而是会由用户在几分钟内创建,以自动执行重复任务,添加对文件格式的支持,等等.所以插件应该有绝对最少的样板代码,除了复制到文件夹之外不需要安装"(所以像 setuptools 入口点,或者 Zope 插件架构似乎太多了.)

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.)

是否已经有这样的系统,或者任何实现类似方案的项目,我应该寻找想法/灵感?

Are there any systems like this already out there, or any projects that implement a similar scheme that I should look at for ideas / inspiration?

推荐答案

我的基本上是一个名为插件"的目录,主应用程序可以轮询然后使用 imp.load_module 来获取文件,寻找可能带有模块级配置参数的知名入口点,然后从那里开始.我使用文件监控的东西来获得一定程度的活力,其中插件处于活动状态,但这是一个很好的选择.

Mine is, basically, a directory called "plugins" which the main app can poll and then use imp.load_module to pick up files, look for a well-known entry point possibly with module-level config params, and go from there. I use file-monitoring stuff for a certain amount of dynamism in which plugins are active, but that's a nice-to-have.

当然,任何伴随着说我不需要[大而复杂的东西] X;我只想要轻量级的东西"的需求都会冒着一次重新实现一个发现的需求的风险.但这并不是说你无论如何都不能玩得开心:)

Of course, any requirement that comes along saying "I don't need [big, complicated thing] X; I just want something lightweight" runs the risk of re-implementing X one discovered requirement at a time. But that's not to say you can't have some fun doing it anyway :)

这篇关于用 Python 构建最小的插件架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆