The code is illustrated as an example
Practice Environment
Python 3.6.5
pluggy 0.13.0
Example 1 Registering a class function as a plug-in function
#!/usr/bin/env python # -*- coding:utf-8 -*- import pluggy hookspec = ("myproject") # hook tags are used to mark hooks hookimpl = ("myproject") # hook implementation tag Used to mark one or more implementations of a hook class MySpec(object): """hook collection""" @hookspec def myhook(self, arg1, arg2): pass @hookspec def my_hook_func1(self, arg1, arg2): pass @hookspec def my_hook_func2(self, arg1, arg2): pass # Plugin Classes class Plugin_1(object): """hook implementation class 1""" @hookimpl def myhook(self, arg1, arg2): print("Plugin_1.myhook called") return arg1 + arg2 @hookimpl def my_hook_func2(self, arg1, arg2): print("Plugin_1.my_hook_func2 called, args:", arg1, arg2) def my_hook_func3(self, arg1, arg2): print("Plugin_1.my_hook_func3 called, args:", arg1, arg2) class Plugin_2(object): """hook implementation class 2""" @hookimpl def myhook(self, arg1, arg2): print("Plugin_2.myhook called") return arg1 - arg2 @hookimpl def my_hook_func2(self, arg1, arg2): print("Plugin_2.my_hook_func2, args:", arg1, arg2) # Initialize the PluginManager pm = ("myproject") # Register a collection of hooks (hook function declarations) pm.add_hookspecs(MySpec) # Register the plugin (hook function implementation) (Plugin_1()) (Plugin_2()) # Calling a custom hook results = (arg1=1, arg2=2) # Call a hook function with the same name in both plugin classes # The function in the later registered plugin will be called first print(results) # Output [-1, 3] results = .my_hook_func1(arg1="name", arg2="shouke") print(results) .my_hook_func2(arg1="addr", arg2="sz")
running result
Plugin_2.myhook called
Plugin_1.myhook called
[-1, 3]
[]
Plugin_2.my_hook_func2, args: addr sz
Plugin_1.my_hook_func2 called, args: addr sz
Example 2 Registering a module function as a plug-in function
,
,
,
Located in the same package directory
import pluggy hookspec = ("myproject") # hook tags are used to mark hooks hookimpl = ("myproject") # hook implementation tag Used to mark one or more implementations of a hook @hookspec def global_hook_func1(arg1, arg2): pass
import pluggy from myhookspec import hookimpl @hookimpl def global_hook_func1(arg1, arg2): print("global_hook_func1 in , args:", arg1, arg2) return ""
from myhookspec import hookimpl @hookimpl def global_hook_func1(arg1, arg2): print("global_hook_func1 in , args:", arg1, arg2) return ""
#!/usr/bin/env python # -*- coding:utf-8 -*- import sys import pluggy import myhookspec import myhookimpl import other # Initialize the PluginManager pm = ("myproject") # Registered hook collections pm.add_hookspecs(myhookspec) # Registration hook implementation (myhookimpl) # Plug-ins can also be modules (other) print(.global_hook_func1(arg1="name", arg2="shouke"))
The results of the run are as follows
global_hook_func1 in , args: name shouke
global_hook_func1 in , args: name shouke
['', '']
Example 3: Custom plugin class implementing hook function free @hookimpl decorator
import pluggy hookspec = ("myproject") @hookspec def mytest_hook_func1(arg1, arg2): pass
def mytest_hook_func1(arg1, arg2): print("global_hook_func1 in , args:", arg1, arg2) return ""
#!/usr/bin/env python # -*- coding:utf-8 -*- import inspect import pluggy import myhookspec import other class PytestPluginManager(): """ Plugin class that implements functions that don't use @HookimplMarkerInstance decoration can also be used as function bodies """ def parse_hookimpl_opts(self, plugin, name): # Specify that hooks functions that are not decorated by @hookimpl always start with mytest_ to avoid accessing non-readable properties. if not ("mytest_"): return method = getattr(plugin, name) opts = super().parse_hookimpl_opts(plugin, name) # think overhookCan only be a function(consider only actual functions for hooks) if not (method): return # Collection of untagged,in order tomytestheadlinerhookfunction (math.),(collect unmarked hooks as long as they have the `pytest_' prefix) if opts is None and ("mytest_"): opts = {} return opts # Initialize the PluginManager pm = PytestPluginManager("myproject") # Registered hook collections pm.add_hookspecs(myhookspec) # Registration hook implementation (other) .mytest_hook_func1(arg1="addr", arg2="sz")
reference connection
/project/pluggy/
to this article on the Python pluggy framework summarizes the basic usage of the article is introduced to this, more related to the use of Python pluggy content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!