[Zope-Checkins] CVS: Packages/ComponentArchitecture - Component.py:1.1.2.1 FactoryComponents.py:1.1.2.2 InputToName.py:1.1.2.2 InterfaceComponents.py:1.1.2.3 Presentation.py:1.1.2.6 Service.py:1.1.2.2 __init__.py:1.1.2.6

Shane Hathaway shane@digicool.com
Thu, 2 Aug 2001 18:24:11 -0400


Update of /cvs-repository/Packages/ComponentArchitecture
In directory cvs.zope.org:/tmp/cvs-serv22623

Modified Files:
      Tag: NR-branch
	FactoryComponents.py InputToName.py InterfaceComponents.py 
	Presentation.py Service.py __init__.py 
Added Files:
      Tag: NR-branch
	Component.py 
Log Message:
Changed Presentation to make use of InputToName abstraction.  And more... :-)

 
=== Added File Packages/ComponentArchitecture/Component.py ===


=== Packages/ComponentArchitecture/FactoryComponents.py 1.1.2.1 => 1.1.2.2 ===
 import Interface
 from InputToName import InputToNameService, InputToNameComponent, \
-     GlobalInputToNameRegistry, getBinder
+     GlobalInputToNameRegistry, getBinder, listComponentNames
 
 
 FACTORY_SERVICE_NAME = 'factories'
@@ -120,4 +120,8 @@
         else:
             raise Errors.FactoryNotFound(object, name)
     return b(object)
+
+def listFactoryNames(object, inputs=None):
+    return listComponentNames(FACTORY_SERVICE_NAME, global_reg,
+                              object, inputs)
 


=== Packages/ComponentArchitecture/InputToName.py 1.1.2.1 => 1.1.2.2 ===
+from types import ListType
+
+from Acquisition import aq_acquire
 import Interface
 from Service import Service, SERVICE_MANAGER_NAME
 from InterfaceComponents import objectImplements
+from Component import Component
 
 
 class InputToNameService (Service):
@@ -14,6 +18,11 @@
         argument, returns the component bound to content.
         '''
 
+    def listComponentNames(inputs):
+        '''
+        Returns a list of component names.
+        '''
+
 
 class InputToNameContainer (Interface.Base):
     '''
@@ -34,13 +43,11 @@
         '''
 
 
-class InputToNameComponent (Interface.Base):
+class InputToNameComponent (Component):
     '''
+    A component that requires some set of interfaces and provides
+    a name.
     '''
-    def getId():
-        '''
-        '''
-
     def getInputs():
         '''
         '''
@@ -98,6 +105,19 @@
             b = self._getBinder(None, name)
         return c
 
+    def listComponentNames(self, inputs):
+        '''
+        Find the names of everything that can operate on
+        any of the given inputs.
+        '''
+        # inputs is assumed to be a flat list.
+        res = []
+        for base_input, name in self._binders.keys():
+            for i in inputs:
+                if i == base_input or i.extends(base_input):
+                    res.append(name)
+        return res
+
 
 def _findBinder(x_orig, x_ob, x_name, sm, (service_name,
                                            inputs, name, result)):
@@ -114,8 +134,8 @@
 
 def getBinder(service_name, global_reg, object, name):
     '''
-    Finds a factory for children of an object by examining what the
-    parent implements.  Searches in services then the global registry.
+    Finds a component to apply to an object by examining what it
+    implements.  Searches in services then the global registry.
     Returns None if not found.
     '''
     inputs = tuple(objectImplements(object))
@@ -130,3 +150,35 @@
     else:
         b = global_reg.getComponentBinder(inputs, name)
     return b
+
+def _listNames(x_orig, x_ob, x_name, sm, (service_name, inputs, result)):
+    '''
+    This is an acquisition filter callback.
+    '''
+    s = sm.getService(service_name, None)
+    if s is not None:
+        lst = s.listComponentNames(inputs)
+        if lst:
+            if type(lst) is not ListType:
+                lst = list(lst)
+            result.extend(lst)
+    return 0        
+
+def listComponentNames(service_name, global_reg, object, inputs=None):
+    '''
+    '''
+    if inputs is None:
+        inputs = tuple(objectImplements(object))
+    result = []
+    try:
+        aq_acquire(object, SERVICE_MANAGER_NAME, _listNames,
+                   (service_name, inputs, result), 1, None, 1)
+    except AttributeError:
+        pass
+    lst = global_reg.listComponentNames(inputs)
+    if lst:
+        if type(lst) is not ListType:
+            lst = list(lst)
+        result.extend(lst)
+    return result
+


=== Packages/ComponentArchitecture/InterfaceComponents.py 1.1.2.2 => 1.1.2.3 ===
 
 
-def _flattenWithNames(object, interfaces, append):
+def flattenInterfaceList(object, interfaces, append):
     # Helper for objectImplements().
     for i in interfaces:
         if isinstance(i, Interface):
@@ -178,7 +178,7 @@
             if iface is not None:
                 append(iface)
         else:
-            _flattenWithNames(object, i, append)
+            flattenInterfaceList(object, i, append)
 
 
 _realObjectImplements = objectImplements
@@ -187,4 +187,4 @@
     '''
     Dereferences local named interfaces.
     '''
-    return _realObjectImplements(object, flattenfunc=_flattenWithNames)
+    return _realObjectImplements(object, flattenfunc=flattenInterfaceList)


=== Packages/ComponentArchitecture/Presentation.py 1.1.2.5 => 1.1.2.6 ===
 """
 
-from types import TupleType
-
 import Errors
-from Service import Service, SERVICE_MANAGER_NAME
 import Interface
-from Acquisition import aq_acquire
-from InterfaceComponents import objectImplements
+from InputToName import InputToNameService, InputToNameComponent, \
+     GlobalInputToNameRegistry, getBinder
 
 
 PRESENTATION_SERVICE_NAME = 'presentation'
 
-class PresentationService (Service):
+class PresentationService (InputToNameService):
     '''
     '''
 
-    def getBinder(inputs, name):
-        '''
-
-        Returns None if unable to provide presentation.
-        '''
 
-
-class PresentationBinder (Interface.Base):
+class PresentationComponent (InputToNameComponent):
     '''
     '''
 
-    def __call__(content):
-        '''
-        Returns a presentation bound to content.
-        '''
-
 
-class PresentationComponent (Interface.Base):
-    '''
-    Holds a PresentationBinder.
-    '''
+global_reg = GlobalInputToNameRegistry()
 
 
 _marker = []  # Create a new marker object.
 
-class GlobalPresentationRegistry:
-    
-    def __init__(self):
-        self._pres = {}
-
-    def providePresentation(self, input, name, p):
-        '''
-        Registers a presentation component.
-        '''
-        self._pres[(input, name)] = p
-
-    def _getPresentation(self, input, name):
-        '''
-        Finds a registered presentation given an interface and a name.
-        '''
-        p = self._pres.get((input, name), None)
-        if p is not None:
-            return p
-        bases = getattr(input, '__bases__', ())
-        if bases:
-            for base in bases:
-                p = self._getPresentation(base, name)
-                if p is not None:
-                    return p
-        return None
-
-    def _getPresentationForInterfaces(self, inputs, name):
-        '''
-        Finds a registered presentaion given a hierarchy of input interfaces
-        and a presentation name.
-        '''
-        if type(inputs) is TupleType:
-            for input in inputs:
-                p = self._getPresentationForInterfaces(input, name)
-                if p is not None:
-                    return p
-        else:
-            # inputs is an interface object.
-            return self._getPresentation(inputs, name)
-
-    def getBinder(self, inputs, name):
-        if inputs:
-            p = self._getPresentationForInterfaces(inputs, name)
-        else:
-            # No input interfaces known.
-            p = self._getPresentation(None, name)
-        return p
-
-
-global_reg = GlobalPresentationRegistry()
-
-def _findPresentation(x_orig, x_ob, x_name, sm, (inputs, name, result)):
-    '''
-    This is an acquisition filter callback.
-    '''
-    ps = sm.getService(PRESENTATION_SERVICE_NAME, None)
-    if ps is not None:
-        pres = ps.getBinder(inputs, name)
-        if pres is not None:
-            result.append(pres)
-            return 1
-    return 0        
-
 def getPresentation(object, name, default=_marker):
     '''
     Finds a presentation for an object by examining what it implements.
     Searches in services then the global registry.
     '''
-    inputs = tuple(objectImplements(object))
-    result = []
-    try:
-        aq_acquire(object, SERVICE_MANAGER_NAME, _findPresentation,
-                   (inputs, name, result), 1, None, 1)
-    except AttributeError:
-        pass
-    if result:
-        p = result[0]
-    else:
-        p = global_reg.getBinder(inputs, name)
-        if p is None:
-            if default is not _marker:
-                return default
-            else:
-                raise Errors.PresentationNotFound(object, name)
-    assert PresentationBinder.isImplementedBy(p)
-    return p(object)
+    b = getBinder(PRESENTATION_SERVICE_NAME, global_reg, object, name)
+    if b is None:
+        if default is not _marker:
+            return default
+        else:
+            raise Errors.PresentationNotFound(object, name)
+    return b(object)


=== Packages/ComponentArchitecture/Service.py 1.1.2.1 => 1.1.2.2 ===
 import Interface
-##from Acquisition import aq_acquire, aq_base
+
 
 SERVICE_MANAGER_NAME = '_service_manager'
 
+
 class Service (Interface.Base):
     '''
     Interface for all Services.
     '''
-
-
-##def _filterService(orig, ob, name, value, service_name):
-##    return hasattr(aq_base(ob), service_name)
-
-##def getService(ob, name):
-##    try:
-##        sm = aq_acquire(ob, SERVICE_MANAGER_NAME, _filterService, name,
-##                        1, None, 1)
-##    except AttributeError:
-##        return None
-##    return getattr(sm, name)
-


=== Packages/ComponentArchitecture/__init__.py 1.1.2.5 => 1.1.2.6 ===
 import Presentation
 getPresentation = Presentation.getPresentation
-providePresentation = Presentation.global_reg.providePresentation
+providePresentation = Presentation.global_reg.provideBinder
 
 
 __all__ = (