cwvreg.py
branchstable
changeset 5278 f912e40324c4
parent 5275 32fbb13c06d3
child 5302 dfd147de06b2
child 5306 763319a51e72
equal deleted inserted replaced
5277:92b827b3830e 5278:f912e40324c4
   183 
   183 
   184 from rql import RQLHelper
   184 from rql import RQLHelper
   185 
   185 
   186 from cubicweb import (ETYPE_NAME_MAP, Binary, UnknownProperty, UnknownEid,
   186 from cubicweb import (ETYPE_NAME_MAP, Binary, UnknownProperty, UnknownEid,
   187                       ObjectNotFound, NoSelectableObject, RegistryNotFound,
   187                       ObjectNotFound, NoSelectableObject, RegistryNotFound,
   188                       RegistryOutOfDate, CW_EVENT_MANAGER, onevent)
   188                       CW_EVENT_MANAGER, onevent)
   189 from cubicweb.utils import dump_class
   189 from cubicweb.utils import dump_class
   190 from cubicweb.vregistry import VRegistry, Registry, class_regid
   190 from cubicweb.vregistry import VRegistry, Registry, class_regid
   191 from cubicweb.rtags import RTAGS
   191 from cubicweb.rtags import RTAGS
   192 
   192 
   193 def clear_rtag_objects():
   193 def clear_rtag_objects():
   454     def values(self):
   454     def values(self):
   455         return [value for key, value in self.items()]
   455         return [value for key, value in self.items()]
   456     def itervalues(self):
   456     def itervalues(self):
   457         return (value for key, value in self.items())
   457         return (value for key, value in self.items())
   458 
   458 
   459     def reset(self, path=None, force_reload=None):
   459     def reset(self):
   460         super(CubicWebVRegistry, self).reset(path, force_reload)
   460         super(CubicWebVRegistry, self).reset()
   461         self._needs_iface = {}
   461         self._needs_iface = {}
   462         # two special registries, propertydefs which care all the property
   462         # two special registries, propertydefs which care all the property
   463         # definitions, and propertyvals which contains values for those
   463         # definitions, and propertyvals which contains values for those
   464         # properties
   464         # properties
   465         if not self.initialized:
   465         if not self.initialized:
   466             self['propertydefs'] = {}
   466             self['propertydefs'] = {}
   467             self['propertyvalues'] = self.eprop_values = {}
   467             self['propertyvalues'] = self.eprop_values = {}
   468             for key, propdef in self.config.eproperty_definitions():
   468             for key, propdef in self.config.eproperty_definitions():
   469                 self.register_property(key, **propdef)
   469                 self.register_property(key, **propdef)
   470         if path is not None and force_reload:
   470 
       
   471     def set_schema(self, schema):
       
   472         """set instance'schema and load application objects"""
       
   473         self._set_schema(schema)
       
   474         # now we can load application's web objects
       
   475         self.reload(self.config.vregistry_path(), force_reload=False)
       
   476         # map lowered entity type names to their actual name
       
   477         self.case_insensitive_etypes = {}
       
   478         for eschema in self.schema.entities():
       
   479             etype = str(eschema)
       
   480             self.case_insensitive_etypes[etype.lower()] = etype
       
   481             clear_cache(eschema, 'ordered_relations')
       
   482             clear_cache(eschema, 'meta_attributes')
       
   483 
       
   484     def reload_if_needed(self):
       
   485         path = self.config.vregistry_path()
       
   486         if self.is_reload_needed(path):
       
   487             self.reload(path)
       
   488 
       
   489     def reload(self, path, force_reload=True):
       
   490         """modification detected, reset and reload the vreg"""
       
   491         CW_EVENT_MANAGER.emit('before-registry-reload')
       
   492         if force_reload:
   471             cleanup_sys_modules(path)
   493             cleanup_sys_modules(path)
   472             cubes = self.config.cubes()
   494             cubes = self.config.cubes()
   473             # if the fs code use some cubes not yet registered into the instance
   495             # if the fs code use some cubes not yet registered into the instance
   474             # we should cleanup sys.modules for those as well to avoid potential
   496             # we should cleanup sys.modules for those as well to avoid potential
   475             # bad class reference pb after reloading
   497             # bad class reference pb after reloading
   476             cfg = self.config
   498             cfg = self.config
   477             for cube in cfg.expand_cubes(cubes, with_recommends=True):
   499             for cube in cfg.expand_cubes(cubes, with_recommends=True):
   478                 if not cube in cubes:
   500                 if not cube in cubes:
   479                     cpath = cfg.build_vregistry_cube_path([cfg.cube_dir(cube)])
   501                     cpath = cfg.build_vregistry_cube_path([cfg.cube_dir(cube)])
   480                     cleanup_sys_modules(cpath)
   502                     cleanup_sys_modules(cpath)
   481 
   503         self.reset()
   482     def set_schema(self, schema):
   504         self.register_objects(path, force_reload)
   483         """set instance'schema and load application objects"""
       
   484         self._set_schema(schema)
       
   485         # now we can load application's web objects
       
   486         self._reload(self.config.vregistry_path(), force_reload=False)
       
   487         # map lowered entity type names to their actual name
       
   488         self.case_insensitive_etypes = {}
       
   489         for eschema in self.schema.entities():
       
   490             etype = str(eschema)
       
   491             self.case_insensitive_etypes[etype.lower()] = etype
       
   492             clear_cache(eschema, 'ordered_relations')
       
   493             clear_cache(eschema, 'meta_attributes')
       
   494 
       
   495     def _reload(self, path, force_reload):
       
   496         CW_EVENT_MANAGER.emit('before-registry-reload')
       
   497         # modification detected, reset and reload
       
   498         self.reset(path, force_reload)
       
   499         super(CubicWebVRegistry, self).register_objects(
       
   500             path, force_reload, self.config.extrapath)
       
   501         CW_EVENT_MANAGER.emit('after-registry-reload')
   505         CW_EVENT_MANAGER.emit('after-registry-reload')
   502 
   506 
   503     def _set_schema(self, schema):
   507     def _set_schema(self, schema):
   504         """set instance'schema"""
   508         """set instance'schema"""
   505         self.schema = schema
   509         self.schema = schema
   540         # XXX bw compat
   544         # XXX bw compat
   541         ifaces = use_interfaces(obj)
   545         ifaces = use_interfaces(obj)
   542         if ifaces:
   546         if ifaces:
   543             self._needs_iface[obj] = ifaces
   547             self._needs_iface[obj] = ifaces
   544 
   548 
   545     def register_objects(self, path, force_reload=None):
   549     def register_objects(self, path, force_reload=False):
   546         """overriden to remove objects requiring a missing interface"""
   550         """overriden to remove objects requiring a missing interface"""
   547         if force_reload is None:
   551         super(CubicWebVRegistry, self).register_objects(
   548             force_reload = self.config.debugmode
   552             path, force_reload, self.config.extrapath)
   549         try:
       
   550             super(CubicWebVRegistry, self).register_objects(
       
   551                 path, force_reload, self.config.extrapath)
       
   552         except RegistryOutOfDate:
       
   553             self._reload(path, force_reload)
       
   554 
   553 
   555     def initialization_completed(self):
   554     def initialization_completed(self):
   556         """cw specific code once vreg initialization is completed:
   555         """cw specific code once vreg initialization is completed:
   557 
   556 
   558         * remove objects requiring a missing interface, unless
   557         * remove objects requiring a missing interface, unless