classHookspecMarker(object):""" Decorator helper class for marking functions as hook specifications.
You can instantiate it with a project_name to get a decorator.
Calling PluginManager.add_hookspecs later will discover all marked functions
if the PluginManager uses the same project_name.
"""def__init__(self, project_name):
self.project_name = project_name
def__call__(
self, function=None, firstresult=False, historic=False, warn_on_impl=None):""" if passed a function, directly sets attributes on the function
which will make it discoverable to add_hookspecs(). If passed no
function, returns a decorator which can be applied to a function
later using the attributes supplied.
If firstresult is True the 1:N hook call (N being the number of registered
hook implementation functions) will stop at I<=N when the I'th function
returns a non-None result.
If historic is True calls to a hook will be memorized and replayed
on later registered plugins.
"""defsetattr_hookspec_opts(func):if historic and firstresult:raise ValueError("cannot have a historic firstresult hook")setattr(
func,
self.project_name +"_spec",dict(
firstresult=firstresult,
historic=historic,
warn_on_impl=warn_on_impl,),)return func
if function isnotNone:return setattr_hookspec_opts(function)else:return setattr_hookspec_opts
pluggy代码结构按照前面demo中的代码顺序,在分析pluggy的核心逻辑之前,我们先来了解HookspecMarker、HookspecMarker的用处是什么?1.HookspecMarker的实现逻辑是什么?我们来先来看它的代码注释class HookspecMarker(object): """ Decorator helper class for marking ...