[Subversion] / PEAK / src / peak / binding / components.py  

Diff of /PEAK/src/peak/binding/components.py

Parent Directory | Revision Log

version 469, Sat Jul 13 21:02:40 2002 UTC version 475, Tue Jul 23 15:41:07 2002 UTC
Line 1 
Line 1 
 """Basic binding tools"""  """Basic binding tools"""
   
 from once import Once, New, WeakBinding  from __future__ import generators
 import meta, modules  from once import Once, New, WeakRefBinding
   import meta
   from peak.running.config.modules import setupModule
   
 from weakref import ref, WeakValueDictionary  from weakref import ref, WeakValueDictionary
   
Line 15 
Line 17 
   
 __all__ = [  __all__ = [
     'Component','AutoCreated','Provider','CachingProvider',      'Component','AutoCreated','Provider','CachingProvider',
     'bindTo', 'requireBinding', 'bindToNames', 'bindToParent', 'bindToSelf',      'bindTo', 'requireBinding', 'bindSequence', 'bindToParent', 'bindToSelf',
     'getRootComponent', 'getParentComponent', 'lookupComponent',      'getRootComponent', 'getParentComponent', 'lookupComponent',
     'acquireComponent', 'globalLookup'      'acquireComponent', 'globalLookup', 'findUtility', 'findUtilities',
       'bindToUtilities',
 ]  ]
   
   
Line 36 
Line 39 
   
   
   
   
   
   
 def Provider(callable):  def Provider(callable):
     return lambda foundIn, forObj: callable(forObj)      return lambda foundIn, forObj: callable(forObj)
   
Line 107 
Line 107 
   
 def globalLookup(name, component=None):  def globalLookup(name, component=None):
   
     """Lookup 'name' in global 'InitialContext', w/'component' in environ"""      """Lookup 'name' in global 'InitialContext', relative to 'component'"""
   
     from peak.naming.api import InitialContext      from peak.naming.api import InitialContext
   
     return InitialContext(RELATIVE_TO_COMPONENT=component).lookup(name)      return InitialContext(component).lookup(name)
   
   
   
Line 162 
Line 162 
   
   
   
   def findUtilities(component, iface, forObj=None):
   
       if forObj is None:
           forObj = component
   
       last = component
   
       while component is not None:
   
           utility = component._getUtility(iface, forObj)
   
           if utility is not None:
               yield utility
   
           last      = component
           component = component.getParentComponent()
   
   
       cfg = config.getLocal(last)
   
       if cfg is not None:
           for u in findUtilities(cfg, iface, forObj):
               yield u
   
   
   def findUtility(component, iface, forObj=None):
   
       for u in findUtilities(component, iface, forObj):
           return u
   
   
   
   
   
   
   
   
   
   
   
   
 ComponentNameSyntax = Syntax(  ComponentNameSyntax = Syntax(
     direction = 1,      direction = 1,
     separator = '/',      separator = '/',
Line 211 
Line 252 
     Interface object.  Strings and 'CompoundName' names will be interpreted      Interface object.  Strings and 'CompoundName' names will be interpreted
     as paths relative to the starting component.  An empty name will return      as paths relative to the starting component.  An empty name will return
     the starting component.  Interfaces will be lookedup using      the starting component.  Interfaces will be lookedup using
     'component.acquireUtility()'.  All other kinds of names, including URL      'findUtility(component,...)'.  All other kinds of names, including URL
     strings and 'CompositeName' instances, will be looked up using      strings and 'CompositeName' instances, will be looked up using
     'binding.globalLookup()'.      'binding.globalLookup()'.
   
Line 238 
Line 279 
         path segment."""          path segment."""
   
     if isinstance(name, InterfaceClass):      if isinstance(name, InterfaceClass):
         utility = component.acquireUtility(name)          utility = findUtility(component, name)
         if utility is None:          if utility is None:
             raise NameNotFoundException(name, resolvedObj = component)              raise NameNotFoundException(name, resolvedObj = component)
   
Line 367 
Line 408 
   
   
   
 class bindToNames(bindTo):  class bindSequence(bindTo):
   
     """Automatically look up and cache a sequence of services by name      """Automatically look up and cache a sequence of services by name
   
Line 375 
Line 416 
   
             class someClass(binding.AutoCreated):              class someClass(binding.AutoCreated):
   
                 thingINeed = binding.bindToNames(                  thingINeed = binding.bindSequence(
                     "path/to/service", "another/path", ...                      "path/to/service", "another/path", ...
                 )                  )
   
Line 408 
Line 449 
   
   
   
 class bindToParent(WeakBinding):  class bindToParent(WeakRefBinding):
   
     """Look up and cache a weak ref to the nth-level parent component      """Look up and cache a weak ref to the nth-level parent component
   
Line 464 
Line 505 
         )          )
   
   
   def bindToUtilities(iface):
       return Once(lambda s,d,a: [u for u in findUtilities(s,iface,s)])
   
   
   
Line 531 
Line 572 
   
   
   
     def acquireUtility(self, iface, forObj=None, localLookup=True):      def _getUtility(self, iface, forObj):
   
         if forObj is None:  
             forObj=self  
   
         if localLookup:  
   
             provider = self.__instance_provides__.get(iface)              provider = self.__instance_provides__.get(iface)
   
Line 552 
Line 588 
                 if utility is not NOT_FOUND:                  if utility is not NOT_FOUND:
                     return utility                      return utility
   
         parent = self.getParentComponent()  
   
         if parent is None:  
             parent = config.getLocal(self)  
   
         return parent.acquireUtility(iface,forObj)  
   
   
     def registerProvider(self, ifaces, provider):      def registerProvider(self, ifaces, provider):
Line 572 
Line 602 
   
   
   
   
   
   
   
   
   
   
   
   
   
   
 class AutoCreatable(type):  class AutoCreatable(type):
   
     """Metaclass for components which auto-create when used"""      """Metaclass for components which auto-create when used"""
Line 613 
Line 654 
   
   
   
 modules.setupModule()  setupModule()
   
   
   


Generate output suitable for use with a patch program
Legend:
Removed from v.469  
changed lines
  Added in v.475

cvs-admin@eby-sarna.com

Powered by ViewCVS 1.0-dev

ViewCVS and CVS Help