[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services - Configuration.py:1.6 ConfigurationInterfaces.py:1.8

Steve Alexander steve@cat-box.net
Wed, 18 Dec 2002 15:23:33 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services
In directory cvs.zope.org:/tmp/cvs-serv12654/lib/python/Zope/App/OFS/Services

Modified Files:
	Configuration.py ConfigurationInterfaces.py 
Log Message:
SteveA and Marius G.

We refactored the NamedComponentConfiguration and ComponentConfiguration.
Now, we have NamedConfiguration and NamedComponentConfiguration.

NamedConfigurable is now split into two parts, as appropriate to that
refactoring.

Cleaned up various extraneous imports, and changed various 
self --> wrapped_self. (Interestingly, this act revealed one ContextMethod
that did not in fact need to be a ContextMethod.)
 


=== Zope3/lib/python/Zope/App/OFS/Services/Configuration.py 1.5 => 1.6 ===
--- Zope3/lib/python/Zope/App/OFS/Services/Configuration.py:1.5	Thu Dec 12 10:28:16 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/Configuration.py	Wed Dec 18 15:23:02 2002
@@ -13,19 +13,16 @@
 ##############################################################################
 """Component registration support for services
 
-This module provides constant definitions for the three registration states,
-Unregistered, Registered, and Active.
-
 $Id$
 """
 __metaclass__ = type
 
 from Persistence import Persistent
 from ConfigurationInterfaces import IConfigurationRegistry, IConfiguration
-from ConfigurationInterfaces import IComponentConfiguration
+from ConfigurationInterfaces import INamedConfiguration
 from ConfigurationInterfaces import INamedComponentConfiguration
 from ConfigurationInterfaces import INameConfigurable
-from Zope.Schema import Text
+from ConfigurationInterfaces import INameComponentConfigurable
 from Zope.ComponentArchitecture import getService, queryService
 from Zope.ComponentArchitecture import getServiceManager
 from Zope.ComponentArchitecture import getAdapter
@@ -98,7 +95,6 @@
                 registry.activate(configuration)
 
 
-
 class ConfigurationRegistry(Persistent):
 
     __implements__ = IConfigurationRegistry
@@ -122,27 +118,24 @@
 
         return rpath
 
-    _id = ContextMethod(_id)
-
-    def register(self, configuration):
-        cid = self._id(configuration)
+    def register(wrapped_self, configuration):
+        cid = wrapped_self._id(configuration)
 
-        if self._data:
-            if cid in self._data:
+        if wrapped_self._data:
+            if cid in wrapped_self._data:
                 return # already registered
         else:
             # Nothing registered. Need to stick None in front so that nothing
             # is active.
-            self._data = (None, )
-
-        self._data += (cid, )
+            wrapped_self._data = (None, )
 
+        wrapped_self._data += (cid, )
     register = ContextMethod(register)
 
-    def unregister(self, configuration):
-        cid = self._id(configuration)
+    def unregister(wrapped_self, configuration):
+        cid = wrapped_self._id(configuration)
 
-        data = self._data
+        data = wrapped_self._data
         if data:
             if data[0] == cid:
                 # It's active, we need to switch in None
@@ -158,19 +151,17 @@
         if len(data) == 1 and data[0] is None:
             data = ()
 
-        self._data = data
-
+        wrapped_self._data = data
     unregister = ContextMethod(unregister)
 
-    def registered(self, configuration):
-        cid = self._id(configuration)
-        return cid in self._data
-
+    def registered(wrapped_self, configuration):
+        cid = wrapped_self._id(configuration)
+        return cid in wrapped_self._data
     registered = ContextMethod(registered)
 
-    def activate(self, configuration):
-        cid = self._id(configuration)
-        data = self._data
+    def activate(wrapped_self, configuration):
+        cid = wrapped_self._id(configuration)
+        data = wrapped_self._data
 
         if cid in data:
 
@@ -182,12 +173,12 @@
                 data = data[1:]
             else:
                 # We need to deactivate the currently active component
-                sm = getServiceManager(self)
+                sm = getServiceManager(wrapped_self)
                 old = traverse(sm, 'Packages/'+data[0])
                 old.deactivated()
 
 
-            self._data = (cid, ) + tuple(
+            wrapped_self._data = (cid, ) + tuple(
                 [item for item in data if item != cid]
                 )
 
@@ -197,19 +188,18 @@
             raise ValueError(
                 "Configuration to be activated is not registered",
                 configuration)
-
     activate = ContextMethod(activate)
 
-    def deactivate(self, configuration):
-        cid = self._id(configuration)
+    def deactivate(wrapped_self, configuration):
+        cid = wrapped_self._id(configuration)
 
-        if cid in self._data:
+        if cid in wrapped_self._data:
 
-            if self._data[0] != cid:
+            if wrapped_self._data[0] != cid:
                 return # already inactive
 
             # Just stick None on the front
-            self._data = (None, ) + self._data
+            wrapped_self._data = (None, ) + wrapped_self._data
 
             configuration.deactivated()
 
@@ -217,33 +207,31 @@
             raise ValueError(
                 "Configuration to be deactivated is not registered",
                 configuration)
-
     deactivate = ContextMethod(deactivate)
 
-    def active(self):
-        if self._data:
-            path = self._data[0]
+    def active(wrapped_self):
+        if wrapped_self._data:
+            path = wrapped_self._data[0]
             if path is not None:
                 # Make sure we can traverse to it.
-                sm = getServiceManager(self)
+                sm = getServiceManager(wrapped_self)
                 configuration = traverse(sm, 'Packages/'+path)
                 return configuration
 
         return None
-
     active = ContextMethod(active)
 
     def __nonzero__(self):
         return bool(self._data)
 
-    def info(self):
-        sm = getServiceManager(self)
+    def info(wrapped_self):
+        sm = getServiceManager(wrapped_self)
 
         result = [{'id': path,
                    'active': False,
                    'configuration': (path and traverse(sm, 'Packages/'+path))
                    }
-                  for path in self._data
+                  for path in wrapped_self._data
                   ]
 
         if result:
@@ -253,11 +241,9 @@
                 result[0]['active'] = True
 
         return result
-
     info = ContextMethod(info)
 
 
-
 class SimpleConfiguration(Persistent):
     """Configuration objects that just contain configuration data
     """
@@ -286,25 +272,38 @@
             configuration.status = Unregistered
 
 
-class ComponentConfiguration(SimpleConfiguration):
-    """Component configuration
+class NamedConfiguration(SimpleConfiguration):
+    """Named configuration
+    """
+
+    __implements__ = INamedConfiguration, SimpleConfiguration.__implements__
+
+    def __init__(self, name, *args, **kw):
+        self.name = name
+        super(NamedConfiguration, self).__init__(*args, **kw)
+
+
+class NamedComponentConfiguration(NamedConfiguration):
+    """Named component configuration
 
     Subclasses should define a getInterface() method returning the interface
     of the component.
     """
 
-    __implements__ = (IComponentConfiguration,
-                      SimpleConfiguration.__implements__, IAddNotifiable)
+    __implements__ = (INamedComponentConfiguration,
+                      NamedConfiguration.__implements__, IAddNotifiable)
+
+    # XXX is all this '*args, **kw' business the right way to use super?
 
-    def __init__(self, component_path, permission=None):
+    def __init__(self, name, component_path, permission=None, *args, **kw):
         self.componentPath = component_path
         if permission == 'Zope.Public':
             permission = CheckerPublic
-
         self.permission = permission
+        super(NamedComponentConfiguration, self).__init__(name, *args, **kw)
 
-    def getComponent(self):
-        service_manager = getServiceManager(self)
+    def getComponent(wrapped_self):
+        service_manager = getServiceManager(wrapped_self)
 
         # We have to be clever here. We need to do an honest to
         # god unrestricted traveral, which means we have to
@@ -317,21 +316,20 @@
 
         # get the root and unproxy it.
         root = removeAllProxies(getPhysicalRoot(service_manager))
-        component = traverse(root, self.componentPath)
+        component = traverse(root, wrapped_self.componentPath)
 
-        if self.permission:
+        if wrapped_self.permission:
             if type(component) is Proxy:
                 # XXX what is this?
                 component = removeSecurityProxy(component)
 
-            interface = self.getInterface()
+            interface = wrapped_self.getInterface()
 
-            checker = InterfaceChecker(interface, self.permission)
+            checker = InterfaceChecker(interface, wrapped_self.permission)
 
             component = Proxy(component, checker)
 
         return component
-
     getComponent = ContextMethod(getComponent)
 
     def manage_afterAdd(self, configuration, container):
@@ -343,7 +341,7 @@
 
     def manage_beforeDelete(self, configuration, container):
         "See Zope.App.OFS.Container.IDeleteNotifiable"
-        super(ComponentConfiguration, self
+        super(NamedComponentConfiguration, self
               ).manage_beforeDelete(configuration, container)
         component = configuration.getComponent()
         dependents = getAdapter(component, IDependable)
@@ -351,69 +349,65 @@
         dependents.removeDependent(objectpath)
 
 
-class NamedComponentConfiguration(ComponentConfiguration):
-    """Named component configuration
-    """
-
-    __implements__ = (INamedComponentConfiguration,
-                      ComponentConfiguration.__implements__)
-
-    def __init__(self, name, *args, **kw):
-        self.name = name
-        super(NamedComponentConfiguration, self).__init__(*args, **kw)
-
-
 class NameConfigurable:
     """Mixin for implementing INameConfigurable
     """
 
     __implements__ = INameConfigurable
 
-    def __init__(self):
+    def __init__(self, *args, **kw):
         self._bindings = {}
+        super(NameConfigurable, self).__init__(*args, **kw)
 
-    def queryConfigurationsFor(self, cfg, default=None):
+    def queryConfigurationsFor(wrapped_self, cfg, default=None):
         """See Zope.App.OFS.Services.ConfigurationInterfaces.IConfigurable"""
-        return self.queryConfigurations(cfg.name, default)
-
+        return wrapped_self.queryConfigurations(cfg.name, default)
     queryConfigurationsFor = ContextMethod(queryConfigurationsFor)
 
-    def queryConfigurations(self, name, default=None):
-        """See Zope.App.OFS.Services.ConfigurationInterfaces.INameConfigurable"""
-        registry = self._bindings.get(name, default)
-        return ContextWrapper(registry, self)
-
+    def queryConfigurations(wrapped_self, name, default=None):
+        """See
+        Zope.App.OFS.Services.ConfigurationInterfaces.INameConfigurable"""
+        registry = wrapped_self._bindings.get(name, default)
+        return ContextWrapper(registry, wrapped_self)
     queryConfigurations = ContextMethod(queryConfigurations)
 
-    def createConfigurationsFor(self, cfg):
+    def createConfigurationsFor(wrapped_self, cfg):
         """See Zope.App.OFS.Services.ConfigurationInterfaces.IConfigurable"""
-        return self.createConfigurations(cfg.name)
-
+        return wrapped_self.createConfigurations(cfg.name)
     createConfigurationsFor = ContextMethod(createConfigurationsFor)
 
-    def createConfigurations(self, name):
-        """See Zope.App.OFS.Services.ConfigurationInterfaces.INameConfigurable"""
+    def createConfigurations(wrapped_self, name):
+        """See
+        Zope.App.OFS.Services.ConfigurationInterfaces.INameConfigurable"""
         try:
-            registry = self._bindings[name]
+            registry = wrapped_self._bindings[name]
         except KeyError:
-            self._bindings[name] = registry = ConfigurationRegistry()
-            self._p_changed = 1
-        return ContextWrapper(registry, self)
-
+            wrapped_self._bindings[name] = registry = ConfigurationRegistry()
+            wrapped_self._p_changed = 1
+        return ContextWrapper(registry, wrapped_self)
     createConfigurations = ContextMethod(createConfigurations)
 
-    def listConfigurationNames(self):
-        """See Zope.App.OFS.Services.ConfigurationInterfaces.INameConfigurable"""
-        return filter(self._bindings.get, self._bindings.keys())
-
-    def queryActiveComponent(self, name, default=None):
-        """See Zope.App.OFS.Services.ConfigurationInterfaces.INameConfigurable"""
-        registry = self.queryConfigurations(name)
+    def listConfigurationNames(wrapped_self):
+        """See
+        Zope.App.OFS.Services.ConfigurationInterfaces.INameConfigurable"""
+        return filter(wrapped_self._bindings.get,
+                      wrapped_self._bindings.keys())
+
+
+class NameComponentConfigurable(NameConfigurable):
+    """Mixin for implementing INameComponentConfigurable
+    """
+
+    __implements__ = INameComponentConfigurable
+
+    def queryActiveComponent(wrapped_self, name, default=None):
+        """See Zope.App.OFS.Services.ConfigurationInterfaces
+               .INameComponentConfigurable"""
+        registry = wrapped_self.queryConfigurations(name)
         if registry:
             configuration = registry.active()
             if configuration is not None:
                 return configuration.getComponent()
         return default
-
     queryActiveComponent = ContextMethod(queryActiveComponent)
 


=== Zope3/lib/python/Zope/App/OFS/Services/ConfigurationInterfaces.py 1.7 => 1.8 ===
--- Zope3/lib/python/Zope/App/OFS/Services/ConfigurationInterfaces.py:1.7	Thu Dec 12 10:28:16 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/ConfigurationInterfaces.py	Wed Dec 18 15:23:02 2002
@@ -57,8 +57,8 @@
     interface.
     """
 
-    description = Text(title = u"Description",
-                       description = u"Detailed description",
+    description = Text(title=u"Description",
+                       description=u"Detailed description",
                        )
 
     def activated():
@@ -69,26 +69,27 @@
         """Method called when a configuration is made inactive
         """
 
-class IComponentConfiguration(IConfiguration):
-    """Configuration object that configures a component
+
+class INamedConfiguration(IConfiguration):
+    """Configuration object that is registered by name
     """
 
-    componentPath = Attribute("The physical path to the component")
+    name = Attribute("The name that is registered")
+
+    label = TextLine(title=u"Label",
+                     description=u"Descriptive label of the configuration"
+                                 u" type (e.g. Service, Connection)")
 
-    def getComponent():
-        """Return the component named in the configuration.
-        """
 
-class INamedComponentConfiguration(IComponentConfiguration):
+class INamedComponentConfiguration(INamedConfiguration):
     """Configuration object that configures a component associated with a name
     """
 
-    name = Attribute("The name of the component")
-
-    label = TextLine(title=u"Label",
-                     description=u"Descriptive label of the configuration type"
-                                 u" (e.g. Service, Connection)")
+    componentPath = Attribute("The physical path to the component")
 
+    def getComponent():
+        """Return the component named in the configuration.
+        """
 
 
 class IConfigurationRegistry(Interface):
@@ -166,6 +167,7 @@
 
 
 class IConfigurable(Interface):
+    """A component that can be configured using a configuration manager."""
 
     def queryConfigurationsFor(configuration, default=None):
         """Return an IConfigurationRegistry for the configuration
@@ -200,7 +202,12 @@
 
 
 class INameConfigurable(IConfigurable):
-    # XXX docstring
+    """An IConfigurable, where a name is used to decide which registry to
+    return for methods in IConfigurable.
+
+    All configurations that pass through queryConfigurationsFor and
+    createConfigurationsFor are expected to implement INamedConfiguration.
+    """
 
     def queryConfigurations(name, default=None):
         """Return an IConfigurationRegistry for the configuration name
@@ -221,6 +228,13 @@
         """Return a list of all registered configuration names
         """
 
+class INameComponentConfigurable(INameConfigurable):
+    """An INameConfigurable where the configurations refer to components.
+
+    All configurations that pass through queryConfigurationsFor and
+    createConfigurationsFor are expected to implement
+    INamedComponentConfiguration.
+    """
     def queryActiveComponent(name, default=None):
         """Finds the configuration registry for a given name, checks if it has
         an active configuration, and if so, returns its component.  Otherwise