[Checkins] SVN: zope.app.appsetup/trunk/src/zope/app/appsetup/ add a function that can provide product-config support from a ZConfig string;

Fred L. Drake, Jr. fdrake at gmail.com
Tue Aug 19 12:41:20 EDT 2008


Log message for revision 90010:
  add a function that can provide product-config support from a ZConfig string;
  only product-configuration will be accepted (for use in test harnesses)
  

Changed:
  U   zope.app.appsetup/trunk/src/zope/app/appsetup/product.py
  U   zope.app.appsetup/trunk/src/zope/app/appsetup/product.txt
  A   zope.app.appsetup/trunk/src/zope/app/appsetup/schema/productconfig.xml
  U   zope.app.appsetup/trunk/src/zope/app/appsetup/schema/schema.xml
  A   zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/
  A   zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/__init__.py
  A   zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/component.xml
  U   zope.app.appsetup/trunk/src/zope/app/appsetup/tests.py

-=-
Modified: zope.app.appsetup/trunk/src/zope/app/appsetup/product.py
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/product.py	2008-08-19 16:39:58 UTC (rev 90009)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/product.py	2008-08-19 16:41:20 UTC (rev 90010)
@@ -3,9 +3,12 @@
 """
 __docformat__ = "reStructuredText"
 
+import ZConfig
+import os.path
 import zope.testing.cleanup
 
 _configs = {}
+_schema = None
 
 zope.testing.cleanup.addCleanUp(_configs.clear)
 
@@ -48,6 +51,17 @@
     _configs.update(state)
 
 
+def loadConfiguration(file, url=None):
+    global _schema
+    if _schema is None:
+        here = os.path.dirname(os.path.abspath(__file__))
+        path = os.path.join(here, "schema", "productconfig.xml")
+        _schema = ZConfig.loadSchema(path)
+    data, handlers = ZConfig.loadConfigFile(_schema, file, url=url)
+    return dict((sect.getSectionName(), sect.mapping)
+                for sect in data.product_config)
+
+
 class FauxConfiguration(object):
     """Configuration object that can be use from tests.
 

Modified: zope.app.appsetup/trunk/src/zope/app/appsetup/product.txt
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/product.txt	2008-08-19 16:39:58 UTC (rev 90009)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/product.txt	2008-08-19 16:41:20 UTC (rev 90010)
@@ -174,3 +174,61 @@
     None
     >>> print product.getProductConfiguration("second")
     {'you': 'there'}
+
+There's an additional function that can be used to load product configuration
+from a file object; only product configuration components are accepted.  The
+function returns a mapping of names to configuration objects suitable for
+passing to ``setProductConfiguration``.  Using this with
+``setProductConfigurations`` would require constructing ``FauxConfiguration``
+objects.
+
+Let's create some sample configuration text:
+
+    >>> product_config = '''
+    ... <product-config product1>
+    ...   key1 product1-value1
+    ...   key2 product1-value2
+    ... </product-config>
+    ...
+    ... <product-config product2>
+    ...   key1 product2-value1
+    ...   key3 product2-value2
+    ... </product-config>
+    ... '''
+
+We can now load the configuration using the ``loadConfiguration`` function:
+
+    >>> import StringIO
+    >>> import pprint
+
+    >>> sio = StringIO.StringIO(product_config)
+    >>> config = product.loadConfiguration(sio)
+
+    >>> pprint.pprint(config, width=1)
+    {'product1': {'key1': 'product1-value1',
+                  'key2': 'product1-value2'},
+     'product2': {'key1': 'product2-value1',
+                  'key3': 'product2-value2'}}
+
+Extensions that provide product configurations can be used as well:
+
+    >>> product_config = '''
+    ... %import zope.app.appsetup.testproduct
+    ...
+    ... <testproduct foobar>
+    ... </testproduct>
+    ...
+    ... <testproduct barfoo>
+    ...   key1 value1
+    ...   key2 value2
+    ... </testproduct>
+    ... '''
+
+    >>> sio = StringIO.StringIO(product_config)
+    >>> config = product.loadConfiguration(sio)
+
+    >>> pprint.pprint(config, width=1)
+    {'barfoo': {'key1': 'value1',
+                'key2': 'value2',
+                'product-name': 'barfoo'},
+     'foobar': {'product-name': 'foobar'}}

Added: zope.app.appsetup/trunk/src/zope/app/appsetup/schema/productconfig.xml
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/schema/productconfig.xml	                        (rev 0)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/schema/productconfig.xml	2008-08-19 16:41:20 UTC (rev 90010)
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<schema>
+
+  <abstracttype name="zope.product.base">
+    <!-- Poor name inherited from Zope 2.
+         This can't be changed since components refer to this in a
+         similar way as to public base classes.  Components that want
+         to work with both Zope 2 and Zope 3 need this to match the
+         existing Zope 2 name.
+         -->
+    <description>
+      Base type for component-specific configuration sections.
+
+      Specific products should implement configuration sections by
+      defining sections types that implement this abstract type and
+      using their own schema component to define meaningful settings.
+
+    </description>
+  </abstracttype>
+
+  <sectiontype name="product-config" implements="zope.product.base">
+    <description>
+    Component-specific configuration, expressed as arbitrary name-value pairs.
+    </description>
+
+    <key name="+"
+         attribute="mapping"
+         required="no"
+         />
+  </sectiontype>
+
+  <multisection type="zope.product.base" name="+"
+                attribute="product_config">
+    <description>
+      Component-specific configuration stanzas.
+
+      Products may use the &lt;product-config&gt; section type, or may supply
+      a component.xml which defines section types with their own schemas.
+
+      All sections for this multisection will be collected together into the
+      'product_config' attribute of the configuration object.
+    </description>
+  </multisection>
+
+</schema>

Modified: zope.app.appsetup/trunk/src/zope/app/appsetup/schema/schema.xml
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/schema/schema.xml	2008-08-19 16:39:58 UTC (rev 90009)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/schema/schema.xml	2008-08-19 16:41:20 UTC (rev 90010)
@@ -1,4 +1,4 @@
-<schema>
+<schema extends="package:zope.app.appsetup:schema/productconfig.xml">
   <description>
     Zope 3 Application Server configuration schema.
 
@@ -13,47 +13,6 @@
   <!-- logging configuration -->
   <import package="ZConfig.components.logger" />
 
-  <abstracttype name="zope.product.base">
-    <!-- Poor name inherited from Zope 2.
-         This can't be changed since components refer to this in a
-         similar way as to public base classes.  Components that want
-         to work with both Zope 2 and Zope 3 need this to match the
-         existing Zope 2 name.
-         -->
-    <description>
-      Base type for component-specific configuration sections.
-
-      Specific products should implement configuration sections by
-      defining sections types that implement this abstract type and
-      using their own schema component to define meaningful settings.
-
-    </description>
-  </abstracttype>
-
-  <sectiontype name="product-config" implements="zope.product.base">
-    <description>
-    Component-specific configuration, expressed as arbitrary name-value pairs.
-    </description>
-
-    <key name="+"
-         attribute="mapping"
-         required="no"
-         />
-  </sectiontype>
-
-  <multisection type="zope.product.base" name="+"
-                attribute="product_config">
-    <description>
-      Component-specific configuration stanzas.
-
-      Products may use the &lt;product-config&gt; section type, or may supply
-      a component.xml which defines section types with their own schemas.
-
-      All sections for this multisection will be collected together into the
-      'product_config' attribute of the configuration object.
-    </description>
-  </multisection>
-
   <multisection type="ZODB.database" name="*" required="yes"
            attribute="databases">
     <description>

Added: zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/__init__.py
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/__init__.py	                        (rev 0)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/__init__.py	2008-08-19 16:41:20 UTC (rev 90010)
@@ -0,0 +1,17 @@
+"""\
+Sample section data type for product configuration tests.
+
+"""
+
+import ZConfig.components.basic.mapping
+import zope.app.appsetup.product
+
+
+def sampleProductConfig(section):
+    mapping = ZConfig.components.basic.mapping.mapping(section)
+    mapping["product-name"] = section.getSectionName()
+
+    # Since this is a product config, we need a product configuration object,
+    # not a bare mapping:
+    return zope.app.appsetup.product.FauxConfiguration(
+        mapping["product-name"], mapping)


Property changes on: zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/__init__.py
___________________________________________________________________
Name: svn:mime-type
   + text/x-python
Name: svn:eol-style
   + native

Added: zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/component.xml
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/component.xml	                        (rev 0)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/testproduct/component.xml	2008-08-19 16:41:20 UTC (rev 90010)
@@ -0,0 +1,9 @@
+<component>
+  <import package="ZConfig.components.basic" file="mapping.xml"/>
+  <sectiontype
+      name="testproduct"
+      implements="zope.product.base"
+      extends="ZConfig.basic.mapping"
+      datatype="zope.app.appsetup.testproduct.sampleProductConfig"
+      />
+</component>

Modified: zope.app.appsetup/trunk/src/zope/app/appsetup/tests.py
===================================================================
--- zope.app.appsetup/trunk/src/zope/app/appsetup/tests.py	2008-08-19 16:39:58 UTC (rev 90009)
+++ zope.app.appsetup/trunk/src/zope/app/appsetup/tests.py	2008-08-19 16:41:20 UTC (rev 90010)
@@ -172,6 +172,11 @@
             urlpath = urlpath[1:]
         return "file:///" + urlpath
 
+    def test_productconfig_xml(self):
+        path = os.path.join(self.schema_dir, "productconfig.xml")
+        url = self.path2url(path)
+        schema = ZConfig.loadSchema(url)
+
     def test_schema_xml(self):
         path = os.path.join(self.schema_dir, "schema.xml")
         url = self.path2url(path)



More information about the Checkins mailing list