Pkg_resources -- entry points provides extension points for Programs

Source: Internet
Author: User
Document directory
  • Introduction to entry points
Official introduction to 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 particle 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,SetuptoolsPackage uses an entry point namedDistutils. CommandsIn order to find commands defined by distutils extensions.SetuptoolsTreats 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 (likeDistutils. 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 useSetuptoolsAnd provideEntry_pointsArgumentSetup ()In its setup script, so that the entry points will be encoded in the distribution's metadata. For more details, seeSetuptoolsDocumentation. (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 cocould advertise two differentDistutils. CommandsEntry points, as long as they had different names. However, there is nothing that preventsDifferentProjects 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.

 

 

For example, pkg_resources is often used in scripts.Run_scriptOr pkg_resources.load_entry_point to execute the command line. This defines a script framework. When installing a new package, you only need to setup. PY specifies entry_points, indicating where to call a function or module (Distutils. commands example ),Then load_entry_point orRun_script. The advantage of doing so is to separate the call from the specific implementation. You only need to specify the 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)
The first parameter is directed to site-packages/pastescript-1.7.5-py2.7.egg, and then searches for egg-info/entry_points.txt, which has two rows defined, the name and location of this entry_point

[Console_scripts]

Paster = paste. Script. Command: Run

Lele_scripts is the group name.

Paster is the name parameter, which actually points to the run function of the paste. Script. command module.

 

Here is another example of a PPT at this pycon Conference:

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

The group that defines entry_points is qipaionweb. games, that is, the games package under qipaionweb. Games.

Then define a function

def get_game_impl_class(game_name): 
group = 'qipaionweb.games'
prj = game_name
    return pkg_resources.load_entry_point(prj, group, game_name)

Prj is a third-party game package. The doudizhu of setup. py above is used as an example.

The unified group name is qipaionweb. Games, which corresponds to [qipaionweb. Games] In setup. py.

Game_name is the specific game name, corresponding to setup. pyDoudizhu=

The function returns doudizhu. game_impl: gameimpl, which is the gameimpl class of the game_impl module in the doudizhu package. gameimpl is the specific implementation of the game.

This design is to separate game_interface from game_impl, and game_impl implements game_interface, so that third-party development plug-ins can develop game applications.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.