[Zope-Checkins] CVS: Zope/lib/python/ComponentArchitecture - IToNRegistry.py:1.1.2.1 Presentation.py:1.1.4.2 Service.py:1.1.4.2

Shane Hathaway shane@digicool.com
Thu, 13 Sep 2001 17:33:37 -0400


Update of /cvs-repository/Zope/lib/python/ComponentArchitecture
In directory cvs.zope.org:/tmp/cvs-serv11581

Modified Files:
      Tag: ComponentArchitecture-branch
	Presentation.py Service.py 
Added Files:
      Tag: ComponentArchitecture-branch
	IToNRegistry.py 
Log Message:
Moving things around to fit the latest models.  See the
UserGoalsAndUseCases of the ComponentArchitecture Wiki.


=== Added File Zope/lib/python/ComponentArchitecture/IToNRegistry.py ===

"""
Registry of components that are registered as requiring
an interface and providing a name.
"""

class IToNRegistry:  # Interface to Name
    
    def __init__(self):
        self._reg = {}

    def register(self, require, name, c):
        '''
        Registers a component.
        '''
        self._reg[(require, name)] = c

    def get(self, ob_interface, name):
        """
        Finds a registered component. Returns None if not found.
        """
        c = self._reg.get((ob_interface, name), None)
        if c is not None:
            return c
        bases = getattr(ob_interface, '__bases__', ())
        if bases:
            # 'require' might be a subinterface of a required interface
            # for a registered adapter.
            for base in bases:
                c = self.get(base, name)
                if c is not None:
                    return c
        return None


=== Zope/lib/python/ComponentArchitecture/Presentation.py 1.1.4.1 => 1.1.4.2 ===
 # 
 ##############################################################################
-"""(simple) Presentation component management
 """
+Presentation component management
+"""
+# XXX Presentation output interface hierarchy is ignored for now.
 
 import Errors
 import Interface
-from InputToName import InputToNameService, InputToNameComponent, \
-     GlobalInputToNameRegistry, getRealization
+from Interface import objectImplements
 
+from Service import Service, findComponent
+from IToNRegistry import IToNRegistry
 
-PRESENTATION_SERVICE_NAME = 'presentation'
+_marker = []  # Create a new marker object.
 
-class PresentationService (InputToNameService):
-    '''
-    '''
 
+PRESENTATION_SERVICE_NAME = 'Presentation'
 
-class PresentationComponent (InputToNameComponent):
-    '''
-    '''
+class PresentationService (Service):
+    """
+    """
 
+    def getUnboundComponent(inputs, name, output):
+        """
+        Returns a presentation component.
+        """
 
-global_reg = GlobalInputToNameRegistry()
 
+_global_regs = {}  # output -> IToNRegistry
 
-_marker = []  # Create a new marker object.
+def registerPresentation(input, name, output, unbound_comp):
+    reg = _global_regs.get(output, None)
+    if reg is None:
+        _global_regs[output] = reg = IToNRegistry()
+    reg.register(input, name, unbound_comp)
 
-def getPresentation(object, name, default=_marker):
-    '''
+def getPresentation(object, name, output, default=_marker):
+    """
     Finds a presentation for an object by examining what it implements.
     Searches in services then the global registry.
-    '''
-    r = getRealization(PRESENTATION_SERVICE_NAME, global_reg, object, name)
-    if r is None:
+    """
+    inputs = objectImplements(object)
+    c = findComponent(PRESENTATION_SERVICE_NAME, object,
+                      (inputs, name, output))
+    if c is None:
+        reg = _global_regs.get(pres_type, None)
+        if reg is not None:
+            for i in inputs:
+                c = reg.get(i, name)
+                if c is not None:
+                    break
+    if c is None:
         if default is not _marker:
             return default
         else:
             raise Errors.PresentationNotFound(object, name)
-    return r(object)
+    # Bind the component.
+    return c(object)


=== Zope/lib/python/ComponentArchitecture/Service.py 1.1.4.1 => 1.1.4.2 ===
 SERVICE_MANAGER_NAME = '_service_manager'
 
+_marker = []  # Create a new marker object.
+
+
+class ServiceManager (Interface.Base):
+    """
+    """
+
+    def getService(service_name, default=None):
+        """
+        """
+
+    def addService(service_name, object, id):
+        """
+        """
+
+    def listServiceNames():
+        """
+        """
+
 
 class Service (Interface.Base):
-    '''
+    """
     Interface for all Services.
-    '''
+    """
+
     def getServiceName(self):
-        '''
-        '''
+        """
+        Returns the name of the service provided, independent of
+        the ID of the object.
+        """
+
+    def getUnboundComponent(*args):
+        """
+        Returns an unbound component, which is a callable object.
+        The object gets called with the object to bind it to as the
+        only argument.  You can't necessarily do anything else with
+        an unbound component.
+        """
 
 
 def _findService(x_orig, x_ob, x_name, sm, (service_name, result)):
-    '''
+    """
     This is an acquisition filter callback.
-    '''
+    """
     s = sm.getService(service_name, None)
     if s is not None:
         result.append(s)
@@ -27,12 +57,10 @@
     return 0        
 
 
-_marker = []  # Create a new marker object.
-
 def getService(object, service_name, default=_marker):
-    '''
+    """
     Finds one implementation of a service in a place.
-    '''
+    """
     result = []
     try:
         aq_acquire(object, SERVICE_MANAGER_NAME, _findService,
@@ -46,3 +74,35 @@
             raise Errors.ServiceNotFound(object, service_name)
         else:
             return default
+
+
+def _findComponentCallback(x_orig, x_ob, x_name, sm, (service_name,
+                                                      result, args)):
+    """
+    This is an acquisition filter callback.
+    """
+    s = sm.getService(service_name, None)
+    if s is not None:
+        c = s.getUnboundComponent(*args)
+        if c is not None:
+            result.append(c)
+            return 1
+    return 0        
+
+
+def findComponent(service_name, object, args):
+    """
+    Finds an unbound component, returning None if not found.
+    """
+    result = []
+    try:
+        aq_acquire(object, SERVICE_MANAGER_NAME, _findComponentCallback,
+                   (service_name, result, args), 1, None, 1)
+    except AttributeError:
+        pass
+    if result:
+        return result[0]
+    else:
+        return None
+
+