我是靠谱客的博主 笑点低茉莉,最近开发中收集的这篇文章主要介绍python做软件插件_在Python中实现插件系统,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1586010002-jmsa.png

I am writing an application (a server wrapper for Minecraft) that I wish to make extensible with plugins. I have a system in place that works, however I think that in the long run it could use improvement.

The way that I have it now, the application calls a "get_plugins" method in the server class that, first imports a file named pluginutils.py (pluginutils.py defines a BasePlugin class which all plugins subclass), and then loops through every .py file in the cmds directory, importing it and checking to see if it is a subclass of BasePlugin. If it is, it stores an instance of it in a dictionary, for which the key is a cmd class variable defined in the plugin. Whenever the application receives a command from the server it checks if it is a key in the dictionary and if so runs the start method of the instance stored in the dictionary, to which it passes the necessary arguments taken from the command.

While this works, I feel like this is a sloppy way to do it. Are there any better techniques for implementing a system similar to this? I want to write this myself (I don't want to use anything like zope.interface) as this is a learning experience. Thanks.

解决方案

Having written quite a few different plugin architectures in different platforms and languages, I will say that you are pretty much on track with how most plugins systems are written.

Basically what it boils down to, is your host and your plugin need to have some kind of common contract to work with; in other words, your host needs to know enough about your plugin that it can pass along or share whatever common resources the plugin will need, and the plugin needs to know enough about the host to interact with those resources.

Implementing this using a base class and a derived class as you have done is a very common methodology.

最后

以上就是笑点低茉莉为你收集整理的python做软件插件_在Python中实现插件系统的全部内容,希望文章能够帮你解决python做软件插件_在Python中实现插件系统所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部