pkg_resources----Entry Points为程序提供扩展点

编程技术  /  houtizong 发布于 2年前   187

pkg_resources----Entry Points为程序提供扩展点 - babykick - 博客园

pkg_resources----Entry Points为程序提供扩展点

官方文档对Entry Points的介绍

Entry Points

Entry points are a simple way for distributions to “advertise” Python objects (such as functions or classes) for use by other distributions.  Extensible applications and frameworks can search for entry points with a particular name or group, either from a specific distribution or from all active distributions on sys.path, and then inspect or load the advertised objects at will.

Entry points belong to “groups” which are named with a dotted name similar to a Python package or module name.  For example, the setuptools package uses an entry point named distutils.commands in order to find commands defined by distutils extensions.  setuptools treats the names of entry points defined in that group as the acceptable commands for a setup script.

In a similar way, other packages can define their own entry point groups, either using dynamic names within the group (like distutils.commands), or possibly using predefined names within the group.  For example, a blogging framework that offers various pre- or post-publishing hooks might define an entry point group and look for entry points named “pre_process” and“post_process” within that group.

To advertise an entry point, a project needs to use setuptools and provide an entry_points argument to setup() in its setup script, so that the entry points will be included in the distribution’s metadata.  For more details, see the setuptools documentation.  (XXX link here to setuptools)

Each project distribution can advertise at most one entry point of a given name within the same entry point group.  For example, a distutils extension could advertise two different distutils.commands entry points, as long as they had different names.  However, there is nothing that prevents differentprojects from advertising entry points of the same name in the same group.  In some cases, this is a desirable thing, since the application or framework that uses the entry points may be calling them as hooks, or in some other way combining them.  It is up to the application or framework to decide what to do if multiple distributions advertise an entry point; some possibilities include using both entry points, displaying an error message, using the first one found in sys.path order, etc.

 

 

例如在script里面经常会看到用pkg_resources.run_script或者pkg_resources.load_entry_point来执行命令行,这就是定义了script的一个框架,当安装新的包的时候,只要setup.py指定好entry_points,指明从哪里开始调用函数或者模块(上面举了distutils.commands例子),然后 load_entry_point或者run_script就可以了。这样做的好处是将调用和具体实现分离开,只需要指明入口entry就可以了。

复制代码

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客