cwvreg.py
branchtls-sprint
changeset 666 8ad9885ea45a
parent 631 99f5852f8604
child 717 54b873918b48
equal deleted inserted replaced
665:1305da1ce3f9 666:8ad9885ea45a
    46                 if not key in ('propertydefs', 'propertyvalues')]
    46                 if not key in ('propertydefs', 'propertyvalues')]
    47     
    47     
    48     def reset(self):
    48     def reset(self):
    49         self._registries = {}
    49         self._registries = {}
    50         self._lastmodifs = {}
    50         self._lastmodifs = {}
    51         # two special registries, propertydefs which care all the property definitions, and
    51         self._needs_iface = {}
    52         # propertyvals which contains values for those properties
    52         # two special registries, propertydefs which care all the property
       
    53         # definitions, and propertyvals which contains values for those
       
    54         # properties
    53         self._registries['propertydefs'] = {}
    55         self._registries['propertydefs'] = {}
    54         self._registries['propertyvalues'] = self.eprop_values = {}
    56         self._registries['propertyvalues'] = self.eprop_values = {}
    55         for key, propdef in self.config.eproperty_definitions():
    57         for key, propdef in self.config.eproperty_definitions():
    56             self.register_property(key, **propdef)
    58             self.register_property(key, **propdef)
    57             
    59             
    71             if registry in ('propertydefs', 'propertyvalues'):
    73             if registry in ('propertydefs', 'propertyvalues'):
    72                 continue
    74                 continue
    73             for objects in regcontent.values():
    75             for objects in regcontent.values():
    74                 for obj in objects:
    76                 for obj in objects:
    75                     obj.schema = schema
    77                     obj.schema = schema
       
    78 
       
    79     def register_if_interface_found(self, obj, ifaces, **kwargs):
       
    80         """register an object but remove it if no entity class implements one of
       
    81         the given interfaces
       
    82         """
       
    83         if not isinstance(ifaces,  (tuple, list)):
       
    84             self._needs_iface[obj] = frozenset((ifaces,))
       
    85         else:
       
    86             self._needs_iface[obj] = frozenset(ifaces)
       
    87         self.register(obj, **kwargs)
       
    88 
       
    89     def register(self, obj, **kwargs):
       
    90         super(CubicWebRegistry, self).register(obj, **kwargs)
       
    91         # XXX bw compat
       
    92         ifaces = getattr(obj, 'accepts_interfaces', None)
       
    93         if ifaces:
       
    94             self._needs_iface[obj] = frozenset(ifaces)
    76         
    95         
    77     def register_objects(self, path, force_reload=None):
    96     def register_objects(self, path, force_reload=None):
    78         """overriden to handle type class cache issue"""
    97         """overriden to remove objects requiring a missing interface"""
    79         if  super(CubicWebRegistry, self).register_objects(path, force_reload):
    98         if super(CubicWebRegistry, self).register_objects(path, force_reload):
    80             # clear etype cache if you don't want to run into deep weirdness
    99             # clear etype cache if you don't want to run into deep weirdness
    81             clear_cache(self, 'etype_class')
   100             clear_cache(self, 'etype_class')
       
   101             # we may want to keep interface dependent objects (e.g.for i18n
       
   102             # catalog generation)
       
   103             if not self.config.cleanup_interface_sobjects:
       
   104                 return
    82             # remove vobjects that don't support any available interface
   105             # remove vobjects that don't support any available interface
    83             interfaces = set()
   106             interfaces = set()
    84             for classes in self.get('etypes', {}).values():
   107             for classes in self.get('etypes', {}).values():
    85                 for cls in classes:
   108                 for cls in classes:
    86                     interfaces.update(cls.__implements__)
   109                     interfaces.update(cls.__implements__)
    87             if not self.config.cleanup_interface_sobjects:
   110             for obj, ifaces in self._needs_iface.items():
    88                 return
   111                 if not ifaces & interfaces:
    89             for registry, regcontent in self._registries.items():
   112                     self.debug('kicking vobject %s (unsupported interface)', obj)
    90                 if registry in ('propertydefs', 'propertyvalues', 'etypes'):
   113                     self.unregister(obj)
    91                     continue
       
    92                 for oid, objects in regcontent.items():
       
    93                     for obj in reversed(objects[:]):
       
    94                         if not obj in objects:
       
    95                             continue # obj has been kicked by a previous one
       
    96                         accepted = set(getattr(obj, 'accepts_interfaces', ()))
       
    97                         if accepted:
       
    98                             for accepted_iface in accepted:
       
    99                                 for found_iface in interfaces:
       
   100                                     if issubclass(found_iface, accepted_iface):
       
   101                                         # consider priority if necessary
       
   102                                         if hasattr(obj.__registerer__, 'remove_all_equivalents'):
       
   103                                             registerer = obj.__registerer__(self, obj)
       
   104                                             registerer.remove_all_equivalents(objects)
       
   105                                         break
       
   106                                 else:
       
   107                                     self.debug('kicking vobject %s (unsupported interface)', obj)
       
   108                                     objects.remove(obj)
       
   109                     # if objects is empty, remove oid from registry
       
   110                     if not objects:
       
   111                         del regcontent[oid]
       
   112 
   114 
   113     def eid_rset(self, cursor, eid, etype=None):
   115     def eid_rset(self, cursor, eid, etype=None):
   114         """return a result set for the given eid without doing actual query
   116         """return a result set for the given eid without doing actual query
   115         (we have the eid, we can suppose it exists and user has access to the
   117         (we have the eid, we can suppose it exists and user has access to the
   116         entity)
   118         entity)