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
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')()