测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pluggy源码解读----HookspecMarker类和HookimplMarker类源码分析
收藏本文
作者:redrose2100 类别: 日期:2022-12-09 13:40:35 阅读:835 次 消耗积分:0 分
[TOC] ![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/7cd47362-951c-11ee-986a-0242ac110004.png) 从pluggy模块应用方法实例可以看出,首先是对HookspecMarker类和HookimplMarker类进行了实例化,因此这里首先解读一下此两个类的源码。 HookspecMarker类和HookimplMarker的定义如下所示,首先看HookspecMarker类的定义,类中只有一个__init__函数和__call__函数,__init__函数很简单,就是一个赋值的操作,即在HookspecMarker实例化的时候传入一个名称,然后对象就拥有了一个project_name的属性,属性值即为传入的值。HookimplMarker同样也是只有一个__init__函数和一个__call__函数,__init__函数同样也是给实例的project_name赋值的。 ```python 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 ```python 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字典。 ```python class MySpec: @hookspec def myhook(self, arg1, arg2): pass ``` 同理,对于在插件定义即实现接口的类中,比如如下代码,也是同样的原理,即调用HookimplMarker中的__call__方法,而此方法中同样function参数的值在这里为myhook,function不为空值的时候,就是给function即给myhook设置一个self.project_name + "_impl"即myhook_impl的属性,同样属性值是这里的dict字典数据。 ```python class Plugin_1: @hookimpl def myhook(self, arg1, arg2): print("in Plugin_1.myhook()") return arg1 + arg2 ``` 至此HookspecMarker类和HookimplMarker类的源码就解析完了,这里需要注意的是需要理解python语言中__call__魔法函数用来做装饰器的用法,掌握了这一点那么这两个类的代码定义就很容易理解了。
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/490
上一篇:
Pluggy源码解读----pluggy源码解读基础准备
下一篇:
Pluggy源码解读----PluginManager类实例化
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件