selectors.py
changeset 5881 57387070f612
parent 5877 0c7b7b76a84f
child 5890 141b935a38fc
equal deleted inserted replaced
5880:a022dcbed0d3 5881:57387070f612
   200 from logilab.common.compat import all, any
   200 from logilab.common.compat import all, any
   201 from logilab.common.interface import implements as implements_iface
   201 from logilab.common.interface import implements as implements_iface
   202 
   202 
   203 from yams import BASE_TYPES
   203 from yams import BASE_TYPES
   204 
   204 
   205 from cubicweb import Unauthorized, NoSelectableObject, NotAnEntity, role
   205 from cubicweb import (Unauthorized, NoSelectableObject, NotAnEntity,
       
   206                       CW_EVENT_MANAGER, role)
   206 # even if not used, let yes here so it's importable through this module
   207 # even if not used, let yes here so it's importable through this module
   207 from cubicweb.appobject import Selector, objectify_selector, lltrace, yes
   208 from cubicweb.appobject import Selector, objectify_selector, lltrace, yes
   208 from cubicweb.schema import split_expression
   209 from cubicweb.schema import split_expression
   209 
   210 
   210 from cubicweb.appobject import traced_selection # XXX for bw compat
   211 from cubicweb.appobject import traced_selection # XXX for bw compat
   696                 except KeyError:
   697                 except KeyError:
   697                     continue # entity type not in the schema
   698                     continue # entity type not in the schema
   698             score += score_interface(etypesreg, cls_or_inst, cls, iface)
   699             score += score_interface(etypesreg, cls_or_inst, cls, iface)
   699         return score
   700         return score
   700 
   701 
       
   702 def _reset_is_instance_cache(vreg):
       
   703     vreg._is_instance_selector_cache = {}
       
   704 
       
   705 CW_EVENT_MANAGER.bind('before-registry-reset', _reset_is_instance_cache)
   701 
   706 
   702 class is_instance(EClassSelector):
   707 class is_instance(EClassSelector):
   703     """Return non-zero score for entity that is an instance of the one of given
   708     """Return non-zero score for entity that is an instance of the one of given
   704     type(s). If multiple arguments are given, matching one of them is enough.
   709     type(s). If multiple arguments are given, matching one of them is enough.
   705 
   710 
   726     def score_class(self, eclass, req):
   731     def score_class(self, eclass, req):
   727         return self.score_etypes(req, eclass, eclass)
   732         return self.score_etypes(req, eclass, eclass)
   728 
   733 
   729     def score_etypes(self, req, cls_or_inst, cls):
   734     def score_etypes(self, req, cls_or_inst, cls):
   730         # cache on vreg to avoid reloading issues
   735         # cache on vreg to avoid reloading issues
   731         try:
   736         cache = req.vreg._is_instance_selector_cache
   732             cache = req.vreg.__is_instance_cache
       
   733         except AttributeError:
       
   734             cache = req.vreg.__is_instance_cache = {}
       
   735         try:
   737         try:
   736             expected_eclasses = cache[self]
   738             expected_eclasses = cache[self]
   737         except KeyError:
   739         except KeyError:
   738             # turn list of entity types as string into a list of
   740             # turn list of entity types as string into a list of
   739             #  (entity class, parent classes)
   741             #  (entity class, parent classes)