Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of entry_point usage

I discoverd entry_points of setuptools:

http://pythonhosted.org/setuptools/setuptools.html#dynamic-discovery-of-services-and-plugins

quote: setuptools supports creating libraries that “plug in” to extensible applications and frameworks, by letting you register “entry points” in your project that can be imported by the application or framework.

But I have not seen a project using them.

Are there examples of projects which use them?

If not, why are they not used?

like image 790
guettli Avatar asked Oct 14 '13 09:10

guettli


People also ask

What is an entry point script?

In NetSuite terminology, an "Entry Point Script" is the module you write that will be assigned as the source file on a Script record. If your module requires an @NScriptType tag, then it is most certainly an Entry Point Script.

What is the use of setup py?

Use of Setup.py It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on. Secondly, it serves as the command line interface via which packaging commands may be executed.

What is Load_entry_point?

exit( load_entry_point('rss2sms==0.0.1', 'console_scripts', 'rss2sms')() ) This executable is just a simple python module which, when we call it, uses the pkg_resources library to look up what python module our setup.py says we should call.

What is console_scripts?

The console_scripts Entry Point Setuptools allows modules to register entrypoints which other packages can hook into to provide certain functionality. It also provides a few itself, including the console_scripts entry point.


2 Answers

There are loads of examples. Any project that defines console scripts uses them, for example. A quick search on GitHub gives you plenty to browse through.

I'll focus on one specific example (one that is not on GitHub): Babel.

Babel uses both entry_points for both console scripts and to define extension points for translatable text extraction. See their setup.py source:

if have_setuptools:
    extra_arguments = dict(
        zip_safe = False,
        test_suite = 'babel.tests.suite',
        tests_require = ['pytz'],

        entry_points = """
        [console_scripts]
        pybabel = babel.messages.frontend:main

        [distutils.commands]
        compile_catalog = babel.messages.frontend:compile_catalog
        extract_messages = babel.messages.frontend:extract_messages
        init_catalog = babel.messages.frontend:init_catalog
        update_catalog = babel.messages.frontend:update_catalog

        [distutils.setup_keywords]
        message_extractors = babel.messages.frontend:check_message_extractors

        [babel.checkers]
        num_plurals = babel.messages.checkers:num_plurals
        python_format = babel.messages.checkers:python_format

        [babel.extractors]
        ignore = babel.messages.extract:extract_nothing
        python = babel.messages.extract:extract_python
        javascript = babel.messages.extract:extract_javascript
        """,
    )

Tools like pip and zc.buildout use the console_scripts entry point to create commandline scripts (one called pybabel, running the main() callable in the babel.messages.frontend module).

The distutils.commands entry points defines additional commands you can use when running setup.py; these can be used in your own projects to invoke Babel command-line utilities right from your setup script.

Last, but not least, it registers its own checkers and extractors. The babel.extractors entry point is loaded by the babel.messages.extract.extract function, using the setuptools pkg_resources module, giving access to all installed Python projects that registered that entry point. The following code looks for a specific extractor in those entries:

try:
    from pkg_resources import working_set
except ImportError:
    pass
else:
    for entry_point in working_set.iter_entry_points(GROUP_NAME,
                                                     method):
        func = entry_point.load(require=True)
        break

This lets any project register additional extractors; simply add an entry point in your setup.py and Babel can make use of it.

like image 91
Martijn Pieters Avatar answered Sep 25 '22 15:09

Martijn Pieters


Sentry is a good example. Sentry's author even created a django package named Logan to convert standard django management commands to console scripts.

like image 41
Leonardo.Z Avatar answered Sep 22 '22 15:09

Leonardo.Z