8

ZCML can include conditional directives of the form

<configure zcml:condition="installed some.python.package">
    (conditional configuration directives)
</configure>

What is the expression syntax for condition? Is 'or' allowed?

joeforker
  • 40,459
  • 37
  • 151
  • 246

1 Answers1

15

I always have to look this up too. The syntax is very simple, and or is not part of the syntax, I am afraid.

As you can see from the documentation in the zope.configuration source code, the syntax is always of the form verb arguments, where verb is one of have, not-have, installed and not-installed.

have and not-have test for a registered feature. A registered feature is simply an opaque string that has been registered with a <meta:provides feature="something" /> tag. Use it to flag that something has been included without tying it to a particular implementation. Example:

<configure zcml:condition="have apidoc">
    <!-- only when the apidoc feature has been provided -->
</configure>

installed and not-installed simply try to import the named package; if the import succeeds so does the installed test. Example:

<configure zcml:condition="installed sqlalchemy"> 
    <!-- only when the sqlalchemy module can be imported -->
</configure>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    I wrote a component that can use multiple implementations of a crypto function depending on which is installed. For my 'or' I can probably put the same `` in two zcml:condition blocks and then register my component in a third. Thanks! – joeforker Oct 21 '09 at 12:58
  • So what about multiple conditions? How does that work? – b4oshany Aug 23 '18 at 19:32
  • @b4oshany: there is no facility to specify multiple conditions on a single tag. You'd have to find a way to nest tags on which to put `zcml:condition` attributes (you can't repeat attributes on a single tag in XML). – Martijn Pieters Aug 24 '18 at 18:50
  • Well, I was thinking about `and` condition. I guess `zcml:condition` doesn't support and condition. – b4oshany Sep 13 '18 at 02:16
  • Exactly, there are no boolean operators, only the syntax I documented in my answer. – Martijn Pieters Sep 13 '18 at 08:34