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,讓第三方開發插件一樣開發游戲應用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM