[Zope3-checkins] SVN: Zope3/trunk/ Implemented new verb "installed <package>" that checks whether a

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Apr 22 06:47:05 EDT 2005


Log message for revision 30102:
  Implemented new verb "installed <package>" that checks whether a 
  particular pacakge is installed, by trying to import it.
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/configuration/xmlconfig.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2005-04-22 09:55:48 UTC (rev 30101)
+++ Zope3/trunk/doc/CHANGES.txt	2005-04-22 10:47:04 UTC (rev 30102)
@@ -94,10 +94,15 @@
         features can be tested with the new zcml:condition attribute.
 
       - ZCML supports conditional directives using the zcml:condition
-        attribute.  The value of the attribute is an expression of the
-        form "verb arguments".  Currently the only recognized verb is "have",
-        and it takes a single name of a feature as an argument.
+        attribute. The value of the attribute is an expression of the
+        form "verb arguments". The following verbs are recognized:
 
+        * `have` -- It takes a single name of a feature as an
+          argument. Features can be registed via the 'zcml:feature' directive.
+
+        * `installed` -- It's argument is a single package name. If the
+          package is importable, return true.
+
         If the expression is true, the element the attribute is attached to
         will be used, otherwise the element and its descendents will be
         ignored.

Modified: Zope3/trunk/src/zope/configuration/xmlconfig.py
===================================================================
--- Zope3/trunk/src/zope/configuration/xmlconfig.py	2005-04-22 09:55:48 UTC (rev 30101)
+++ Zope3/trunk/src/zope/configuration/xmlconfig.py	2005-04-22 10:47:04 UTC (rev 30102)
@@ -233,9 +233,10 @@
 
         `expression` is a string of the form "verb arguments".
 
-        Currently the only supported verb is 'have'.  It takes one argument:
-        the name of a feature.
+        Currently the two supported verba are 'have' and 'installed'.
 
+        The 'have' verb takes one argument: the name of a feature.
+
         >>> from zope.configuration.config import ConfigurationContext
         >>> context = ConfigurationContext()
         >>> context.provideFeature('apidoc')
@@ -261,6 +262,31 @@
         Traceback (most recent call last):
           ...
         ValueError: Feature name missing: 'have'
+
+
+        The 'installed' verb takes one argument: the dotted name of a
+        pacakge. If the pacakge is found, in other words, can be imported,
+        then the condition will return true.
+
+        >>> from zope.configuration.config import ConfigurationContext
+        >>> context = ConfigurationContext()
+        >>> c = ConfigurationHandler(context, testing=True)
+        >>> c.evaluateCondition('installed zope.interface')
+        True
+        >>> c.evaluateCondition('installed zope.foo')
+        False
+
+        Ill-formed expressions raise an error
+
+        >>> c.evaluateCondition("installed foo bar")
+        Traceback (most recent call last):
+          ...
+        ValueError: Only one package allowed: 'installed foo bar'
+
+        >>> c.evaluateCondition("installed")
+        Traceback (most recent call last):
+          ...
+        ValueError: Package name missing: 'installed'
         """
         arguments = expression.split(None)
         verb = arguments.pop(0)
@@ -270,6 +296,16 @@
             if len(arguments) > 1:
                 raise ValueError("Only one feature allowed: %r" % expression)
             return self.context.hasFeature(arguments[0])
+        elif verb == 'installed':
+            if not arguments:
+                raise ValueError("Package name missing: %r" % expression)
+            if len(arguments) > 1:
+                raise ValueError("Only one package allowed: %r" % expression)
+            try:
+                __import__(arguments[0])
+            except ImportError:
+                return False
+            return True
         else:
             raise ValueError("Invalid ZCML condition: %r" % expression)
 



More information about the Zope3-Checkins mailing list