赞
踩
【原文链接】Pluggy源码解读----HookspecMarker类和HookimplMarker类源码分析
从pluggy模块应用方法实例可以看出,首先是对HookspecMarker类和HookimplMarker类进行了实例化,因此这里首先解读一下此两个类的源码。
HookspecMarker类和HookimplMarker的定义如下所示,首先看HookspecMarker类的定义,类中只有一个__init__函数和__call__函数,__init__函数很简单,就是一个赋值的操作,即在HookspecMarker实例化的时候传入一个名称,然后对象就拥有了一个project_name的属性,属性值即为传入的值。HookimplMarker同样也是只有一个__init__函数和一个__call__函数,__init__函数同样也是给实例的project_name赋值的。
class HookspecMarker:
"""Decorator helper class for marking functions as hook specifications.
You can instantiate it with a project_name to get a decorator.
Calling :py:meth:`.PluginManager.add_hookspecs` later will discover all marked functions
if the :py:class:`.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 :py:meth:`.PluginManager.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.
"""
def setattr_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 is not None:
return setattr_hookspec_opts(function)
else:
return setattr_hookspec_opts
class HookimplMarker:
"""Decorator helper class for marking functions as hook implementations.
You can instantiate with a ``project_name`` to get a decorator.
Calling :py:meth:`.PluginManager.register` later will discover all marked functions
if the :py:class:`.PluginManager` uses the same project_name.
"""
def __init__(self, project_name):
self.project_name = project_name
def __call__(
self,
function=None,
hookwrapper=False,
optionalhook=False,
tryfirst=False,
trylast=False,
specname=None,
):
"""if passed a function, directly sets attributes on the function
which will make it discoverable to :py:meth:`.PluginManager.register`.
If passed no function, returns a decorator which can be applied to a
function later using the attributes supplied.
If ``optionalhook`` is ``True`` a missing matching hook specification will not result
in an error (by default it is an error if no matching spec is found).
If ``tryfirst`` is ``True`` this hook implementation will run as early as possible
in the chain of N hook implementations for a specification.
If ``trylast`` is ``True`` this hook implementation will run as late as possible
in the chain of N hook implementations.
If ``hookwrapper`` is ``True`` the hook implementations needs to execute exactly
one ``yield``. The code before the ``yield`` is run early before any non-hookwrapper
function is run. The code after the ``yield`` is run after all non-hookwrapper
function have run. The ``yield`` receives a :py:class:`.callers._Result` object
representing the exception or result outcome of the inner calls (including other
hookwrapper calls).
If ``specname`` is provided, it will be used instead of the function name when
matching this hook implementation to a hook specification during registration.
"""
def setattr_hookimpl_opts(func):
setattr(
func,
self.project_name + "_impl",
dict(
hookwrapper=hookwrapper,
optionalhook=optionalhook,
tryfirst=tryfirst,
trylast=trylast,
specname=specname,
),
)
return func
if function is None:
return setattr_hookimpl_opts
else:
return setattr_hookimpl_opts(function)
因此在应用实例中如下两行代码实际就是hookspec这个对象有一个属性project_name,而此属性的值就是myproject,而hookimpl对象也有一个属性project_name,而此属性的值也是myproject
hookspec = pluggy.HookspecMarker("myproject")
hookimpl = pluggy.HookimplMarker("myproject")
而应用实例中的如下定义接口的代码,这里使用hookspec作为装饰器作用在myhook方法上,其实就是调用HookspecMarker中的__call__方法,而此方法中的function参数就是myhook方法,其他几个参数默认为None,而在HookspecMarker中的__call__方法中可以看出,当function存在值时,实际是为function设置了一个属性,即为myhook方法设置了一个self.project_name + "_spec"即myproject_spec属性,值就是代码中的这个dict字典。
class MySpec:
@hookspec
def myhook(self, arg1, arg2):
pass
同理,对于在插件定义即实现接口的类中,比如如下代码,也是同样的原理,即调用HookimplMarker中的__call__方法,而此方法中同样function参数的值在这里为myhook,function不为空值的时候,就是给function即给myhook设置一个self.project_name + "_impl"即myhook_impl的属性,同样属性值是这里的dict字典数据。
class Plugin_1:
@hookimpl
def myhook(self, arg1, arg2):
print("in Plugin_1.myhook()")
return arg1 + arg2
至此HookspecMarker类和HookimplMarker类的源码就解析完了,这里需要注意的是需要理解python语言中__call__魔法函数用来做装饰器的用法,掌握了这一点那么这两个类的代码定义就很容易理解了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。