[Zope3-checkins] SVN: Zope3/branches/srichter-twisted-integration2/src/zope/app/appsetup/appsetup.py Added new argument to config() that allows us to add ZCML features to the

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Sep 9 10:12:12 EDT 2005


Log message for revision 38427:
  Added new argument to config() that allows us to add ZCML features to the 
  configuration context before the ZCML file is executed. This has the 
  advantage that we can send in ZCML features from the *.conf files or even 
  just from Python code.
  
  

Changed:
  U   Zope3/branches/srichter-twisted-integration2/src/zope/app/appsetup/appsetup.py

-=-
Modified: Zope3/branches/srichter-twisted-integration2/src/zope/app/appsetup/appsetup.py
===================================================================
--- Zope3/branches/srichter-twisted-integration2/src/zope/app/appsetup/appsetup.py	2005-09-09 14:10:06 UTC (rev 38426)
+++ Zope3/branches/srichter-twisted-integration2/src/zope/app/appsetup/appsetup.py	2005-09-09 14:12:12 UTC (rev 38427)
@@ -31,9 +31,61 @@
     principal = system_user
     interaction = None
 
+
 _configured = False
-def config(file, execute=True):
-    """Configure site globals"""
+def config(file, features=(), execute=True):
+    r"""Execute the ZCML configuration file.
+
+    This procedure defines the global site setup. Optionally you can also
+    provide a list of features that are inserted in the configuration context
+    before the execution is started.
+
+    Let's create a trivial sample ZCML file.
+
+      >>> import tempfile
+      >>> fn = tempfile.mktemp('.zcml')
+      >>> zcml = open(fn, 'w')
+      >>> zcml.write('''
+      ... <configure xmlns:meta="http://namespaces.zope.org/meta"
+      ...            xmlns:zcml="http://namespaces.zope.org/zcml">
+      ...   <meta:provides feature="myFeature" />
+      ...   <configure zcml:condition="have myFeature2">
+      ...     <meta:provides feature="myFeature4" />
+      ...   </configure>
+      ... </configure>
+      ... ''')
+      >>> zcml.close()
+
+    We can now pass the file into the `config()` function:
+
+      # End an old interaction first
+      >>> from zope.security.management import endInteraction
+      >>> endInteraction()
+
+      >>> context = config(fn, features=('myFeature2', 'myFeature3'))
+      >>> context.hasFeature('myFeature')
+      True
+      >>> context.hasFeature('myFeature2')
+      True
+      >>> context.hasFeature('myFeature3')
+      True
+      >>> context.hasFeature('myFeature4')
+      True
+
+    Further, we should have access to the configuration file name and context
+    now:
+
+      >>> getConfigSource() is fn
+      True
+      >>> getConfigContext() is context
+      True
+
+    Let's now clean up by removing the temporary file:
+
+      >>> import os
+      >>> os.remove(fn)
+
+    """
     global _configured
     global __config_source
     __config_source = file
@@ -41,7 +93,7 @@
     if _configured:
         return
 
-    from zope.configuration import xmlconfig
+    from zope.configuration import xmlconfig, config
 
     # Set user to system_user, so we can do anything we want
     from zope.security.management import newInteraction
@@ -51,7 +103,11 @@
     zope.app.component.hooks.setHooks()
 
     # Load server-independent site config
-    context = xmlconfig.file(file, execute=execute)
+    context = config.ConfigurationMachine()
+    xmlconfig.registerCommonDirectives(context)
+    for feature in features:
+        context.provideFeature(feature)
+    context = xmlconfig.file(file, context=context, execute=execute)
 
     # Reset user
     from zope.security.management import endInteraction
@@ -64,6 +120,7 @@
 
     return context
 
+
 def database(db):
     """Load ZODB database from Python module or FileStorage file"""
     if type(db) is str:



More information about the Zope3-Checkins mailing list