题
ZCML 可以包含以下形式的条件指令
<configure zcml:condition="installed some.python.package">
(conditional configuration directives)
</configure>
表达式语法是什么 condition
?允许“或”吗?
解决方案
我也总是要查一下这个。语法非常简单,并且 or
恐怕不是语法的一部分。
正如你可以看到的 zope.configuration 源代码中的文档, ,语法始终为以下形式 verb arguments
, ,其中动词是其中之一 have
, not-have
, installed
和 not-installed
.
have
和 not-have
测试已注册的功能。注册的功能只是一个已注册的不透明字符串 <meta:provides feature="something" />
标签。使用它来标记某些内容已被包含,而不将其绑定到特定的实现。例子:
<configure zcml:condition="have apidoc">
<!-- only when the apidoc feature has been provided -->
</configure>
installed
和 not-installed
只需尝试导入指定的包;如果导入成功,那么 installed
测试。例子:
<configure zcml:condition="installed sqlalchemy">
<!-- only when the sqlalchemy module can be imported -->
</configure>
不隶属于 StackOverflow