[Zope-CVS] CVS: Products/Ape/lib/apelib/config - apeconf.py:1.6

Shane Hathaway shane at zope.com
Sat Mar 13 17:23:24 EST 2004


Update of /cvs-repository/Products/Ape/lib/apelib/config
In directory cvs.zope.org:/tmp/cvs-serv15657/config

Modified Files:
	apeconf.py 
Log Message:
Changed the structure of apeconf.xml to be more similar to ape.conf.

Ape won't use the ZConfig-based ape.conf after all, but its structure
was cleaner because it used subclassing rather than excessive 
components.


=== Products/Ape/lib/apelib/config/apeconf.py 1.5 => 1.6 ===
--- Products/Ape/lib/apelib/config/apeconf.py:1.5	Mon Feb  2 10:07:17 2004
+++ Products/Ape/lib/apelib/config/apeconf.py	Sat Mar 13 17:22:53 2004
@@ -16,6 +16,7 @@
 $Id$
 """
 from __future__ import nested_scopes
+from types import TupleType
 
 from apelib.core.mapper import Mapper, MapperConfiguration
 from apelib.core.serializers import CompositeSerializer, AnyObjectSerializer
@@ -90,22 +91,32 @@
 
 class FactoryProducer:
 
-    def __init__(self, source, factory_name, param=None):
+    def __init__(self, source, factory):
         self.source = source
-        pos = factory_name.rfind('.')
+        pos = factory.find('(')
+        if pos >= 0:
+            # Arguments specified.  Interpret as a Python expression.
+            args = eval(factory[pos:])
+            if not isinstance(args, TupleType):
+                args = (args,)
+            factory = factory[:pos]
+        else:
+            args = ()
+        pos = factory.rfind('.')
         if pos <= 0:
             raise ValueError, "Module and class name required"
-        self.module_name = factory_name[:pos]
-        self.class_name = factory_name[pos + 1:]
-        self.param = param
+        self.module_name = factory[:pos]
+        self.class_name = factory[pos + 1:]
+        self.args = args
         self.sub_producer = None
 
     def __call__(self, compsys):
         params = []
-        if self.param is not None:
-            params.append(self.param)
+        if self.args:
+            params.extend(self.args)
         if self.sub_producer is not None:
-            params.append(self.sub_producer(compsys))
+            obj = self.sub_producer(compsys)
+            params.append(obj)
         m = __import__(self.module_name, {}, {}, ('__doc__',))
         try:
             c = getattr(m, self.class_name)
@@ -123,8 +134,7 @@
             raise ValueError, "Both 'use' and 'factory' not allowed"
         return UseProducer(source, comptype, attrs['use'])
     elif attrs.has_key('factory'):
-        param = attrs.get('param')
-        return FactoryProducer(source, attrs['factory'], param)
+        return FactoryProducer(source, attrs['factory'])
     elif raise_exc:
         raise ValueError, "Need a 'use', 'factory', or 'enabled' attribute"
     else:
@@ -176,7 +186,7 @@
                 source, comptype, name, producer)
         else:
             # Composite component of a mapper
-            name = attrs['name']
+            name = attrs.get('name', '')
             directive = MapperComponent(
                 source, mapper_name, comptype,
                 name, producer, attrs.get('order', 'middle'))
@@ -330,12 +340,9 @@
 
     def addSerializers(self):
         cname = self.attrs.get('class')
-        if cname == 'any':
-            # This mapper is usable for many classes
-            s = AnyObjectSerializer()
-        elif cname == 'none':
+        if cname == 'none':
             # This mapper is abstract (usable for no classes)
-            s = CompositeSerializer(None, None)
+            module, name = None, None
         else:
             # This mapper is concrete (usable for one class only)
             if cname is None:
@@ -343,29 +350,49 @@
             pos = cname.rfind('.')
             if pos < 0:
                 raise ConfigurationError("Class name must include a module name")
-            s = CompositeSerializer(cname[:pos], cname[pos + 1:])
+            module, name = cname[:pos], cname[pos + 1:]
+        d = self.multi_comps.get('serializer', {})
 
-        d = self.multi_comps.get('serializer')
-        if d:
-            ordered = [((r.order or '').lower(), name, r)
-                       for name, r in d.items()]
-            ordered.sort()
-            for order, name, r in ordered:
-                o = r.producer(self.compsys)
-                if o is not None:
-                    s.add(str(name), o)
-                    self.subobjs.append(o)
+        # Create the main serializer
+        r = d.get('')
+        if r:
+            s = r.producer(self.compsys)
+        else:
+            s = CompositeSerializer(module, name)
+
+        # Create the contained serializers
+        ordered = [
+            ((r.order or '').lower(), name, r)
+            for name, r in d.items() if name]
+        ordered.sort()
+        for order, name, r in ordered:
+            o = r.producer(self.compsys)
+            if o is not None:
+                s.add(str(name), o)
+                self.subobjs.append(o)
+
+        # Assign it
         self.obj.serializer = s
 
     def addGateways(self):
-        g = CompositeGateway()
-        d = self.multi_comps.get('gateway')
-        if d:
-            for name, r in d.items():
+        d = self.multi_comps.get('gateway', {})
+
+        # Create the main gateway
+        r = d.get('')
+        if r:
+            g = r.producer(self.compsys)
+        else:
+            g = CompositeGateway()
+
+        # Create the contained gateways
+        for name, r in d.items():
+            if name:
                 o = r.producer(self.compsys)
                 if o is not None:
                     g.add(str(name), o)
                     self.subobjs.append(o)
+
+        # Assign it
         self.obj.gateway = g
 
     def addInitializers(self):
@@ -415,7 +442,7 @@
                     MapperAttribute, 'value', mapper_name=name, name='class')
                 if class_name is None:
                     class_name = name
-                elif class_name in ('none', 'any'):
+                elif class_name == 'none':
                     class_name = None
                 if class_name is not None:
                     key = ('class', class_name)




More information about the Zope-CVS mailing list