我是靠谱客的博主 清新奇异果,最近开发中收集的这篇文章主要介绍python插件化设计_Python-插件编程,python,化,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

引用

2018-11-06 python插件式框架开发

https://www.jianshu.com/p/a472f44c7161

python 优雅地实现插件架构

https://www.cnblogs.com/hhh5460/p/6681363.html

一. @classmethod + @function装饰器

1.1 项目结构

├─ project

├─ run.py

├─ app

├─ __init__.py

├─ main.py

├─ platform.py

├─ plugins

├─ __init__.py

├─ plugin1.py

├─ plugin2.py

1.2 项目代码

project/run.py

# -*- coding: UTF-8 -*-

from app.main import test

test()

project/app/__init__.py

# -*- coding: UTF-8 -*-

__all__ = ['plugin1', 'plugin2']

project/app/main.py

# -*- coding: UTF-8 -*-

from .platform import TextProcessor

def test():

processor = TextProcessor()

print(processor.PLUGINS)

processed = processor.process(text="**foo bar**", plugins=('plugin1', ))

processed = processor.process(text="--foo bar--")

project/app/platform.py

# -*- coding: UTF-8 -*-

class TextProcessor(object):

PLUGINS = {}

def process(self, text, plugins=()):

if plugins == ():

print("------Run all registered plugins:")

for plugin_name in self.PLUGINS.keys():

print("--Excute the {} plugin--".format(plugin_name))

res= self.PLUGINS[plugin_name]().process(text)

else:

print("------Run all specified plugins:")

for plugin_name in plugins:

print("--Excute the {} plugin--".format(plugin_name))

res= self.PLUGINS[plugin_name]().process(text)

return res

@classmethod

def plugin_register(cls, plugin_name):

def wrapper(plugin):

print(plugin)

cls.PLUGINS.update({plugin_name:plugin})

return plugin

return wrapper

project/app/plugins/__init__.py

# -*- coding: UTF-8 -*-

from .plugins import *

project/app/plugins/plugin1.py

# -*- coding: UTF-8 -*-

from ..platform import TextProcessor

@TextProcessor.plugin_register('plugin1')

class CleanMarkdownBolds(object):

def process(self, text):

print("I am plugin1")

return 0

project/app/plugins/plugin2.py

# -*- coding: UTF-8 -*-

from ..platform import TextProcessor

@TextProcessor.plugin_register('plugin2')

class CleanMarkdownItalic(object):

def process(self, text):

print("I am plugin2")

return 0

1.3 Readme

在 app 目录下,除了 app/__init__.py,不需要在别的任何地方显式地导入插件:from .plugins import * 或 from .plugins import plugin1

若想添加插件 plugin3.py,可将其复制到 plugins 目录下,然后修改 app/plugins/__init__.py 文件为 __all__ = ['plugin1', 'plugin2', 'plugin3']

二. __import__()

import module相当于__import__("module")

from module import func相当于__import__("module", fromlist=["func"])

import package.module相当于__import__("package.module", fromlist=["module"])

2.1 项目结构

|---main.py

---plugins

plugin1.py

plugin2.py

__init__.py

2.2 项目代码

main.py

# -*- coding: UTF-8 -*-

import os

class Platform:

def __init__(self):

self.plugins = []

self.loadPlugins()

def loadPlugins(self):

for filename in os.listdir("plugins"):

if not filename.endswith(".py") or filename.startswith("_"):

continue

self.runPlugin(filename)

def runPlugin(self, filename):

pluginName = os.path.splitext(filename)[0]

print("------Import {}------".format(pluginName))

plugin = __import__("plugins." + pluginName, fromlist=[pluginName])

clazz = plugin.getPluginClass()

o = clazz()

o.setPlatform(self)

print("Run {}:".format(pluginName))

o.start()

self.plugins.append(o)

def sayHello(self, from_):

print("hello from {}.".format(from_))

def sayGoodbye(self, from_):

print("goodbye from {}.".format(from_))

def shutdown(self):

print("------Shutdown ------")

for o in self.plugins:

o.stop()

o.setPlatform(None)

self.plugins = []

if __name__ == "__main__":

platform = Platform()

platform.shutdown()

/plugins/plugin1.py

# -*- coding: UTF-8 -*-

class Plugin1:

def setPlatform(self, platform):

self.platform = platform

def start(self):

self.platform.sayHello("plugin1")

def stop(self):

self.platform.sayGoodbye("plugin1")

def getPluginClass():

return Plugin1

/plugins/plugin2.py

# -*- coding: UTF-8 -*-

class Plugin2:

def setPlatform(self, platform):

self.platform = platform

def start(self):

self.platform.sayHello("plugin2")

def stop(self):

self.platform.sayGoodbye("plugin2")

def getPluginClass():

return Plugin2

最后

以上就是清新奇异果为你收集整理的python插件化设计_Python-插件编程,python,化的全部内容,希望文章能够帮你解决python插件化设计_Python-插件编程,python,化所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部