python entry points 例子

 pbr的介绍不多, http://ju.outofmemory.cn/entry/156745

 

$ mkdir entry_test; cd entry_test; git init

$ mkdir  -p mypackage/api/v1/

$ touch mypackage/__init__.py; touch mypackage/api/__init__.py; touch mypackage/api/v1/__init__.py;

$ tree mypackage

.
├── mypackage
│   ├── api
│   │   ├── __init__.py
│   │   └── v1
│   │       ├── databases.py
│   │       ├── hello.py
│   │       ├── __init__.py
│   ├── __init__.py
├── setup.cfg
└── setup.py

$ cat mypackage/api/v1/databases.py

def main():
    print "this is databases main"

 

$ cat mypackage/api/v1/hello.py

def main():
    print "this is hello main"

 

$ cat  setup.cfg

[metadata]
name = mypackage 
version = 12.0.0
summary = Cloud computing fabric controller

[files]
packages =
    mypackage 

[entry_points]
mypackage.api.v1 = 
    databases = mypackage.api.v1.databases:main
    hello = mypackage.api.v1.hello:main

[wheel]
universal = 1

[pbr]
autodoc_index_modules = 0
warnerrors = true

$ cat setup.py

import setuptools

# In python < 2.7.4, a lazy loading of package `pbr` will break
# setuptools if some other modules registered functions in `atexit`.
# solution from: http://bugs.python.org/issue15881#msg170215

setuptools.setup(
    name='mypackage',
    packages=['mypackage'],
    package_dir={'mypackage': 'mypackage'},
    setup_requires=['pbr'],
    pbr=True,
    entry_points={
        'mypackage.api.v1':[
            'databases=mypackage.api.v1.databases:main',
            'hello=mypackage.api.v1.hello:main',
            ],
    }
)

 

调用方法1:

令贤的blog介绍 stevedore: http://blog.csdn.net/lynn_kong/article/details/9704413

opensatck 社区介绍stevedore   http://docs.openstack.org/developer/stevedore/tutorial/index.html 

教程:  https://github.com/openstack/stevedore/tree/master/doc/source/tutorial

from stevedore import extension

def test_detect_plugins():
    em = extension.ExtensionManager('mypackage.api.v1')
    names = sorted(em.names())
    print names
    em1 = extension.ExtensionManager('mypackage.api.v1')
    eps1 = [ext.plugin for ext in em1]  #plugin是被映射的函数,用于调用
    em1 = extension.ExtensionManager('mypackage.api.v1')
    eps1 = [ext.entry_point for ext in em1]

调用方法2:

import pkg_resources

def run_entry_point(*argv):
    group = 'mypackage.api.v1'
    for entrypoint in pkg_resources.iter_entry_points(group=group):
        # Grab the function that is the actual plugin.
        plugin = entrypoint.load()
        print plugin
        type(plugin)
        plugin(*argv)

 

调用方法3:

from pkg_resources import load_entry_point
load_entry_point('mypackage', 'mypackage.api.v1', 'database')()

 

在我的test 例子中需要导入pbr 才能工作,否则有些源代码打包不了。 

http://blog.oddbit.com/2014/09/27/integrating-custom-code-with-n/

https://github.com/larsks/demo_nova_hooks

没有导入pbr, 也可以, 需要研究。

 

posted @ 2015-07-06 16:10  lvmxh  阅读(3576)  评论(0编辑  收藏  举报