一个简单的异步应用插件系统
aiohook的Python项目详细描述
aiohook–用于异步、三重奏和古玩的简单插件
aiohooks是一个小库,用于帮助在 python 3.7+异步应用程序开发与anyio兼容 图书馆。
快速启动
- 通过修饰一个伪
def
,声明钩子协同程序的签名。 (没有实现,或者使用默认实现)使用aiohook.spec
。 await
您希望在其中调用插件的伪协同程序。- 在单独的模块或类中实现虚拟coros,并用
aiohook.impl('reference.to.dummy')
。 - 调用
aiohook.register(reference)
来注册修饰的实现 在reference
(对象实例或模块)中。 - 未实现的钩子导致调用默认实现。
- 注册同一个钩子的多个实现会引发异常。
基本用法
在这个故事中,你的名字是华莱士,你正在实现一个很棒的异步 应用程序接受插件。你的用户,格罗米特,想利用你的 插件系统注入他的自定义解析逻辑。
应用程序设计器
必须首先定义每个钩子函数的签名,因为 在单独的pluginspecs.py文件中的实例:
fromtypingimportSequence,Optionalimportaiohook@aiohook.specasyncdeftokenize(sentence:str)->Sequence[str]:# Describe the purpose of the hook, arguments etc in the docstring"""Split string of characters into individual tokens."""# No implementation here, this is just a function spec@aiohook.specasyncdeftransform_token(word:str)->Optional[str]:"""Preprocess each raw token, for instance as a normalization step."""
在应用程序代码中,然后调用指定的函数,就好像它们 已经实施:
importsysfrompluginspecsimporttokenize,transform_tokenwithopen(sys.argv[1],'r')asf:
插件开发人员
开发
两个需求文件用于描述开发设置:
- requirements.txt文件描述了一个工作开发环境,其中 固定依赖项。
- requirements-base.txt文件仅包含直接取消固定的依赖项。
覆盖率
的测试pytest --cov=aiohook tests/