selectors.py
changeset 5561 58b05c314443
parent 5557 1a534c596bff
child 5564 d13830a2adfd
equal deleted inserted replaced
5559:6b183d860295 5561:58b05c314443
   529     * `registry`, a registry name
   529     * `registry`, a registry name
   530 
   530 
   531     * `regids`, object identifiers in this registry, one of them should be
   531     * `regids`, object identifiers in this registry, one of them should be
   532       selectable.
   532       selectable.
   533     """
   533     """
       
   534     selectable_score = 1
   534     def __init__(self, registry, *regids):
   535     def __init__(self, registry, *regids):
   535         self.registry = registry
   536         self.registry = registry
   536         self.regids = regids
   537         self.regids = regids
   537 
   538 
   538     def __call__(self, cls, req, **kwargs):
   539     def __call__(self, cls, req, **kwargs):
   539         for regid in self.regids:
   540         for regid in self.regids:
   540             try:
   541             try:
   541                 req.vreg[self.registry].select(regid, req, **kwargs)
   542                 req.vreg[self.registry].select(regid, req, **kwargs)
   542                 return 1
   543                 return self.selectable_score
   543             except NoSelectableObject:
   544             except NoSelectableObject:
   544                 return 0
   545                 return 0
   545 
   546 
   546 
   547 
   547 class adaptable(appobject_selectable):
   548 class adaptable(appobject_selectable):
   551 
   552 
   552     * `regids`, adapter identifiers (e.g. interface names) to which the context
   553     * `regids`, adapter identifiers (e.g. interface names) to which the context
   553       (usually entities) should be adaptable. One of them should be selectable
   554       (usually entities) should be adaptable. One of them should be selectable
   554       when multiple identifiers are given.
   555       when multiple identifiers are given.
   555     """
   556     """
       
   557     # implementing an interface takes precedence other special Any interface,
       
   558     # hence return 2 (implements('Any') score is 1)
       
   559     selectable_score = 2
   556     def __init__(self, *regids):
   560     def __init__(self, *regids):
   557         super(adaptable, self).__init__('adapters', *regids)
   561         super(adaptable, self).__init__('adapters', *regids)
   558 
   562 
   559 
   563 
   560 # rset selectors ##############################################################
   564 # rset selectors ##############################################################