接口实用程序
benterfaces的Python项目详细描述
#BenterFaces-接口实用程序和插件系统
[![特拉维斯](https://img.shields.io/travis/bennr01/benterfaces.svg)](
[![工作服Github](https://img.shields.io/coveralls/github/bennr01/benterfaces.svg)](
[![GitHub问题](https://img.shields.io/github/issues/bennr01/benterfaces.svg)](https://github.com/bennr01/benterfaces/issues)
[![github许可证](https://img.shields.io/github/license/bennr01/benterfaces.svg)(https://github.com/bennr01/benterfaces/blob/master/license)
`benterfaces'包含各种接口实用程序和插件系统。
创建类时的实现。
例如,如果foobar无法正确实现ifoo或ibar,则以下代码将引发brokenimplementation错误。
``python
来自zope。interfaces import implementer
来自benterfaces import verify\u implementation
来自imyproject import ifoo,IBar
@verify_implementation
@implementer(IFoo,IBar)
类FooBar(object):
…
`````
如果您的接口在开发过程中可能发生更改,这将非常有用。
此外,它是一行代码(import)所以它不需要太多的工作来添加。
插件取决于条件和已安装的模块)
-从多个路径加载插件
-从任何文件扩展名的python源文件加载插件
-从`.pyc`-文件加载插件
**plugindiscoverer(路径,extensions=[“.py”])**
这个类处理插件的发现和加载。
参数:
-`path`:指定要搜索插件的目录的字符串列表。
-`extensions`:要搜索插件的文件扩展名列表。
来自self.paths的插件最多下降“max_depth”目录。
-`loaded`:加载的插件数
-`enabled`:启用的插件数
-`clear_plugin_list()`:清除内部插件缓存。
-`reload_plugins()`:清除内部插件缓存并再次加载插件。
-`get_implementation(self,ifaceexclude=[])`:返回实现具有最高优先级的“iface”的插件,该插件不在“exclude”中如果找不到插件,则会引发“NotImplementedError”。
-`get-plugin(…)`:与“get-implementation()”相同。
-`get-u-all-u插件(self,iface,exclude=[],sorted-by-u-priority=True)`:返回实现iface的所有插件的列表,不包括“exclude”。
-`is-implemented(ifaceexclude=[]`:如果iface是由除“exclude”之外的任何插件实现的,则返回True;否则返回False。
**@requires(condition=True,modules=[])**
>此装饰函数标记插件的要求。
参数:
-`condition`:必须为“True”的布尔值,才能启用插件。
-`modules`:插件启用所需的模块名称列表。
始终启用不带`@requires(…)`decorator的插件。
**@priority(n=0)**
此decorator函数设置插件的优先级。
“n”的较高值表示较高的优先级。
不带`@priority(…)`decorator的插件是认为优先级为“0”。
创建一个包含“zope.interface.interface”定义的文件,该定义可以从项目和插件目录导入。
2.创建一个或多个插件目录
3在插件di中创建一个“.py”文件包含插件实现的目录(请记住实现接口)
4完成。
**加载插件**
若要加载插件,请执行以下操作:
1。导入“benterfaces.plugindcoverer()`
2。创建“benterfaces.plugindcoverer(paths)”的实例,将插件目录作为“paths”传递。
3.调用实例的`.load_plugins()`方法。
4。根据您的用例,使用“get_plugin(iface)”或“get_all_plugins(iface)”访问插件。
[![特拉维斯](https://img.shields.io/travis/bennr01/benterfaces.svg)](
[![工作服Github](https://img.shields.io/coveralls/github/bennr01/benterfaces.svg)](
[![GitHub问题](https://img.shields.io/github/issues/bennr01/benterfaces.svg)](https://github.com/bennr01/benterfaces/issues)
[![github许可证](https://img.shields.io/github/license/bennr01/benterfaces.svg)(https://github.com/bennr01/benterfaces/blob/master/license)
`benterfaces'包含各种接口实用程序和插件系统。
创建类时的实现。
例如,如果foobar无法正确实现ifoo或ibar,则以下代码将引发brokenimplementation错误。
``python
来自zope。interfaces import implementer
来自benterfaces import verify\u implementation
来自imyproject import ifoo,IBar
@verify_implementation
@implementer(IFoo,IBar)
类FooBar(object):
…
`````
如果您的接口在开发过程中可能发生更改,这将非常有用。
此外,它是一行代码(import)所以它不需要太多的工作来添加。
插件取决于条件和已安装的模块)
-从多个路径加载插件
-从任何文件扩展名的python源文件加载插件
-从`.pyc`-文件加载插件
**plugindiscoverer(路径,extensions=[“.py”])**
这个类处理插件的发现和加载。
参数:
-`path`:指定要搜索插件的目录的字符串列表。
-`extensions`:要搜索插件的文件扩展名列表。
来自self.paths的插件最多下降“max_depth”目录。
-`loaded`:加载的插件数
-`enabled`:启用的插件数
-`clear_plugin_list()`:清除内部插件缓存。
-`reload_plugins()`:清除内部插件缓存并再次加载插件。
-`get_implementation(self,ifaceexclude=[])`:返回实现具有最高优先级的“iface”的插件,该插件不在“exclude”中如果找不到插件,则会引发“NotImplementedError”。
-`get-plugin(…)`:与“get-implementation()”相同。
-`get-u-all-u插件(self,iface,exclude=[],sorted-by-u-priority=True)`:返回实现iface的所有插件的列表,不包括“exclude”。
-`is-implemented(ifaceexclude=[]`:如果iface是由除“exclude”之外的任何插件实现的,则返回True;否则返回False。
**@requires(condition=True,modules=[])**
>此装饰函数标记插件的要求。
参数:
-`condition`:必须为“True”的布尔值,才能启用插件。
-`modules`:插件启用所需的模块名称列表。
始终启用不带`@requires(…)`decorator的插件。
**@priority(n=0)**
此decorator函数设置插件的优先级。
“n”的较高值表示较高的优先级。
不带`@priority(…)`decorator的插件是认为优先级为“0”。
创建一个包含“zope.interface.interface”定义的文件,该定义可以从项目和插件目录导入。
2.创建一个或多个插件目录
3在插件di中创建一个“.py”文件包含插件实现的目录(请记住实现接口)
4完成。
**加载插件**
若要加载插件,请执行以下操作:
1。导入“benterfaces.plugindcoverer()`
2。创建“benterfaces.plugindcoverer(paths)”的实例,将插件目录作为“paths”传递。
3.调用实例的`.load_plugins()`方法。
4。根据您的用例,使用“get_plugin(iface)”或“get_all_plugins(iface)”访问插件。