简体   繁体   English

entry_point用法示例

[英]Examples of entry_point usage

I discoverd entry_points of setuptools: 我发现了setuptools的entry_points:

http://pythonhosted.org/setuptools/setuptools.html#dynamic-discovery-of-services-and-plugins 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. quote:setuptools支持创建可“插入”可扩展应用程序和框架的库,方法是让您在项目中注册“入口点”,该入口点可以由应用程序或框架导入。

But I have not seen a project using them. 但是我还没有看到一个使用它们的项目。

Are there examples of projects which use them? 有使用它们的项目的例子吗?

If not, why are they not used? 如果没有,为什么不使用它们?

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. 在GitHub上快速搜索可让您浏览大量内容。

I'll focus on one specific example (one that is not on GitHub): Babel . 我将集中讨论一个具体示例( 不在 GitHub上): Babel

Babel uses both entry_points for both console scripts and to define extension points for translatable text extraction. Babel同时使用两个控制台脚本的entry_points和定义可翻译文本提取的扩展点。 See their setup.py source : 查看他们的setup.py来源

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). 诸如pipzc.buildout类的工具使用console_scripts入口点来创建命令行脚本(一个名为pybabel ,运行babel.messages.frontend模块中可调用的main() )。

The distutils.commands entry points defines additional commands you can use when running setup.py ; distutils.commands入口点定义了在运行setup.py时可以使用的其他命令; these can be used in your own projects to invoke Babel command-line utilities right from your setup script. 您可以在自己的项目中使用它们来直接从安装脚本中调用Babel命令行实用程序。

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. babel.extractors入口点由babel.messages.extract.extract函数使用setuptools pkg_resources模块加载,从而可以访问所有已安装的注册该入口点的Python项目。 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. 只需在setup.py添加一个入口点,Babel便可以使用它。

Sentry is a good example. 哨兵就是一个很好的例子。 Sentry's author even created a django package named Logan to convert standard django management commands to console scripts. Sentry的作者甚至创建了一个名为Logan的django包,以将标准django管理命令转换为控制台脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM