python的插件和钩子调用机制
aioplugg的Python项目详细描述
请read the docs了解更多!
一个明确的例子
importaiopluggy,asynciohookspec=aiopluggy.HookspecMarker("myproject")hookimpl=aiopluggy.HookimplMarker("myproject")classMySpec(object):"""A hook specification namespace. """@hookspecdefmyhook(self,arg1,arg2):"""My special little hook that you can customize. """classPlugin_1(object):"""A hook implementation namespace. """@hookimpl.asyncioasyncdefmyhook(self,arg1,arg2):print("inside Plugin_1.myhook()")returnarg1+arg2classPlugin_2(object):"""A 2nd hook implementation namespace. """@hookimpldefmyhook(self,arg1,arg2):print("inside Plugin_2.myhook()")returnarg1-arg2asyncdefmain():# create a manager and add the specpm=aiopluggy.PluginManager("myproject")pm.register_specs(MySpec)# register pluginsawaitpm.register(Plugin_1())awaitpm.register(Plugin_2())# call our `myhook` hookresults=awaitpm.hook.myhook(arg1=1,arg2=2)print(results)asyncio.get_event_loop.run_until_complete(main())