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就可以了。

1 #!D:\develop\Python27\python.exe
2 # EASY-INSTALL-ENTRY-SCRIPT: 'pastescript==1.7.5','console_scripts','paster'
3 __requires__ = 'pastescript==1.7.5'
4 import sys
5 from pkg_resources import load_entry_point
6
7 if __name__ == '__main__':
8 sys.exit(
9 load_entry_point('pastescript==1.7.5', 'console_scripts', 'paster')()
10 )

iter_entry_points(group,
name=None)
第一个参数定向到site-packages/pastescript-1.7.5-py2.7.egg ,然后搜索EGG-INFO/entry_points.txt,里面有两行定义了,这个entry_point的名称和位置
[console_scripts]
paster=paste.script.command:run
console_scripts就是group名称
paster就是name参数,实际指向paste.script.command模块的run函数
再举一个这届pycon大会赖永浩的ppt中例子:

#setup.py
entry_points="""
# -*- Entry points: -*-
[qipaionweb.games]
doudizhu = doudizhu.game_impl:GameImpl
"""

定义entry_points的group是qipaionweb.games,也就是qipaionweb.games下面的games包
然后定义一个函数
def get_game_impl_class(game_name):
group = 'qipaionweb.games'
prj = game_name
return pkg_resources.load_entry_point(prj, group, game_name)
prj 就是第三方开发的游戏包以上面setup.py的doudizhu为例
统一的group名是qipaionweb.games,对应setup.py中[qipaionweb.games]
game_name 就是具体游戏名,对应setup.py中doudizhu=
函数返回的是doudizhu.game_impl:GameImpl ,即doudizhu包里面game_impl模块的GameImpl类,GameImpl是游戏的具体实现。
这个设计就是要把game_interface和game_impl分离,game_impl实现game_interface,让第三方开发插件一样开发游戏应用。
pkg_resources----Entry Points为程序提供扩展点的更多相关文章
- spring mvc 提供的几个常用的扩展点
转载 :http://blog.csdn.net/gufachongyang02/article/details/43836105 这是spring3 mvc的核心流程图: SpirngMVC的第 ...
- 玩转Asp.net MVC 的八个扩展点
MVC模型以低耦合.可重用.可维护性高等众多优点已逐渐代替了WebForm模型.能够灵活使用MVC提供的扩展点可以达到事半功倍的效果,另一方面Asp.net MVC优秀的设计和高质量的代码也值得我们去 ...
- Asp.net MVC 的八个扩展点
http://www.cnblogs.com/richieyang/p/5180939.html MVC模型以低耦合.可重用.可维护性高等众多优点已逐渐代替了WebForm模型.能够灵活使用MVC提供 ...
- MVC中你必须知道的13个扩展点
MVC中你必须知道的13个扩展点 pasting 转:http://www.cnblogs.com/kirinboy/archive/2009/06/01/13-asp-net-mvc-extensi ...
- [转]ASP.NET MVC中你必须知道的13个扩展点
本文转自:http://www.cnblogs.com/ejiyuan/archive/2010/03/09/1681442.html ScottGu在其最新的博文中推荐了Simone Chiaret ...
- net MVC 的八个扩展点
net MVC 的八个扩展点 MVC模型以低耦合.可重用.可维护性高等众多优点已逐渐代替了WebForm模型.能够灵活使用MVC提供的扩展点可以达到事半功倍的效果,另一方面Asp.net MVC优秀的 ...
- Spring-IOC 扩展点 BeanFactoryPostProcessor及其子接口解析
BeanFactoryPostProcessor 接口的英文描述: Allows for custom modification of an application context's bean de ...
- ASP.NET MVC中你必须知道的13个扩展点
ScottGu在其最新的博文中推荐了Simone Chiaretta的文章13 ASP.NET MVC extensibility points you have to know,该文章为我 ...
- atitit.eclipse有多少api 扩展点,以及扩展点的设计
atitit.eclipse有多少api 扩展点,以及扩展点的设计 不赞成使用的.作废的以及内部的扩展点 [扩展]页显示了几个你不应该在你的插件中使用的扩展点.在附表C.1的[描述]栏中,我们使用如 ...
随机推荐
- 【WPF】ComboBox:根据绑定选取、设置固定集合中的值
问题场景 我有一个对象,里面有一个属性叫Limit,int类型.虽然int可取的范围很大,我想要在用户界面上限制Limit可取的值,暂且限制为5.10.15.20. 所以ComboBox绑定不是绑定常 ...
- C#泛型<T>说明
泛型:即通过参数化类型来实现在同一份代码上操作多种数据类型.泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用. C#泛型的作用概述 C#泛型赋予了代码更强的类型安全,更 ...
- Emgu安装配置及使用
前言:项目需要,需使用图像处理来完成机械臂从运动的皮带上抓取物体的功能,所以又重拾视觉与图像处理内容. 内容:Emgu是OpenCV的一个跨平台的.NET封装,结构如下图所示: 下载地址:http:/ ...
- Python 爬虫常见的坑和解决方法
1.请求时出现HTTP Error 403: Forbidden headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23. ...
- Python中文编码过程中遇到的一些问题
首先,要明确encode()和decode()的差别 encode()的作用是将Unicode编码的字符串转换为其它编码格式. 比如:st1.encode("utf-8") 这句 ...
- java jdbc oracle ORA-01795: 列表中的最大表达式数为 1000
在操作SQL中存在In的数量如果超过1000条会提示 ORA-01795: 列表中的最大表达式数为 1000 归纳有几种方式出现的: 第一种是:我在上一个 [jdbc 同时执行 查询和删除操]作中 ...
- Python 深入剖析SocketServer模块(一)(V2.7.11)
一.简介(翻译) 通用socket server 类 该模块尽力从各种不同的方面定义server: 对于socket-based servers: -- address family: ...
- Mybatis之增删改查操作
准备工作 建立整体项目目录 新建一个java工程,创建如下工程目录 其中com.kang.pojo中存放pojo类,com.kang.test中存放测试类. 源码目录config中存放Mybatis的 ...
- Spring IOC 容器源码分析(转)
原文地址 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢 ...
- php 文件头部(header)
发布:sunday01 来源:net [大 中 小] 有关php文件头部信息(header)的详细介绍,是脚本学堂见过的最详细的一篇,有需要的朋友,千万不要错过这么好的文章. php文件头 ...