server/serverconfig.py
branchtls-sprint
changeset 1666 bf4bdc601779
parent 1263 01152fffd593
child 1977 606923dff11b
equal deleted inserted replaced
1665:14a8f9d434c8 1666:bf4bdc601779
    79           ' to be done when entity are added/modified by users, activate this '
    79           ' to be done when entity are added/modified by users, activate this '
    80           'option and setup a job using cubicweb-ctl db-rebuild-fti on your '
    80           'option and setup a job using cubicweb-ctl db-rebuild-fti on your '
    81           'system (using cron for instance).',
    81           'system (using cron for instance).',
    82           'group': 'main', 'inputlevel': 1,
    82           'group': 'main', 'inputlevel': 1,
    83           }),
    83           }),
    84         
    84 
    85         # email configuration
    85         # email configuration
    86         ('default-recipients-mode',
    86         ('default-recipients-mode',
    87          {'type' : 'choice',
    87          {'type' : 'choice',
    88           'choices' : ('default-dest-addrs', 'users', 'none'),
    88           'choices' : ('default-dest-addrs', 'users', 'none'),
    89           'default': 'default-dest-addrs',
    89           'default': 'default-dest-addrs',
   121           'default': None,
   121           'default': None,
   122           'help': 'identifier of the repository in the pyro name server',
   122           'help': 'identifier of the repository in the pyro name server',
   123           'group': 'pyro-server', 'inputlevel': 2,
   123           'group': 'pyro-server', 'inputlevel': 2,
   124           }),
   124           }),
   125         ) + CubicWebConfiguration.options)
   125         ) + CubicWebConfiguration.options)
   126         
   126 
   127     # read the schema from the database
   127     # read the schema from the database
   128     read_application_schema = True
   128     read_application_schema = True
   129     bootstrap_schema = True
   129     bootstrap_schema = True
   130     
   130 
   131     # check user's state at login time
   131     # check user's state at login time
   132     consider_user_state = True
   132     consider_user_state = True
   133     
   133 
   134     # hooks registration configuration
   134     # hooks registration configuration
   135     # all hooks should be activated during normal execution
   135     # all hooks should be activated during normal execution
   136     core_hooks = True
   136     core_hooks = True
   137     usergroup_hooks = True
   137     usergroup_hooks = True
   138     schema_hooks = True
   138     schema_hooks = True
   140     security_hooks = True
   140     security_hooks = True
   141     application_hooks = True
   141     application_hooks = True
   142 
   142 
   143     # should some hooks be deactivated during [pre|post]create script execution
   143     # should some hooks be deactivated during [pre|post]create script execution
   144     free_wheel = False
   144     free_wheel = False
   145     
   145 
   146     # list of enables sources when sources restriction is necessary
   146     # list of enables sources when sources restriction is necessary
   147     # (eg repository initialization at least)
   147     # (eg repository initialization at least)
   148     _enabled_sources = None
   148     _enabled_sources = None
   149     @wproperty
   149     @wproperty
   150     def enabled_sources(self, sourceuris=None):
   150     def enabled_sources(self, sourceuris=None):
   151         self._enabled_sources = sourceuris
   151         self._enabled_sources = sourceuris
   152         clear_cache(self, 'sources')
   152         clear_cache(self, 'sources')
   153         
   153 
   154     @classmethod
   154     @classmethod
   155     def schemas_lib_dir(cls):
   155     def schemas_lib_dir(cls):
   156         """application schema directory"""
   156         """application schema directory"""
   157         return env_path('CW_SCHEMA_LIB', cls.SCHEMAS_LIB_DIR, 'schemas')
   157         return env_path('CW_SCHEMA_LIB', cls.SCHEMAS_LIB_DIR, 'schemas')
   158 
   158 
   170             self.init_cubes(self.expand_cubes(get_csv(line)))
   170             self.init_cubes(self.expand_cubes(get_csv(line)))
   171             break
   171             break
   172         else:
   172         else:
   173             # no cubes
   173             # no cubes
   174             self.init_cubes(())
   174             self.init_cubes(())
   175         
   175 
   176     def write_bootstrap_cubes_file(self, cubes):
   176     def write_bootstrap_cubes_file(self, cubes):
   177         stream = file(join(self.apphome, 'bootstrap_cubes'), 'w')
   177         stream = file(join(self.apphome, 'bootstrap_cubes'), 'w')
   178         stream.write('# this is a generated file only used for bootstraping\n')
   178         stream.write('# this is a generated file only used for bootstraping\n')
   179         stream.write('# you should not have to edit this\n')
   179         stream.write('# you should not have to edit this\n')
   180         stream.write('%s\n' % ','.join(cubes))
   180         stream.write('%s\n' % ','.join(cubes))
   181         stream.close()
   181         stream.close()
   182         
   182 
   183     def sources_file(self):
   183     def sources_file(self):
   184         return join(self.apphome, 'sources')
   184         return join(self.apphome, 'sources')
   185     
   185 
   186     # this method has to be cached since when the server is running using a
   186     # this method has to be cached since when the server is running using a
   187     # restricted user, this user usually don't have access to the sources
   187     # restricted user, this user usually don't have access to the sources
   188     # configuration file (#16102)
   188     # configuration file (#16102)
   189     @cached
   189     @cached
   190     def sources(self):
   190     def sources(self):
   194         allsources = read_config(self.sources_file())
   194         allsources = read_config(self.sources_file())
   195         if self._enabled_sources is None:
   195         if self._enabled_sources is None:
   196             return allsources
   196             return allsources
   197         return dict((uri, config) for uri, config in allsources.items()
   197         return dict((uri, config) for uri, config in allsources.items()
   198                     if uri in self._enabled_sources or uri == 'admin')
   198                     if uri in self._enabled_sources or uri == 'admin')
   199     
   199 
   200     def pyro_enabled(self):
   200     def pyro_enabled(self):
   201         """pyro is always enabled in standalone repository configuration"""
   201         """pyro is always enabled in standalone repository configuration"""
   202         return True
   202         return True
   203         
   203 
   204     def load_hooks(self, vreg):
   204     def load_hooks(self, vreg):
   205         hooks = {}
   205         hooks = {}
   206         for path in reversed([self.apphome] + self.cubes_path()):
   206         for path in reversed([self.apphome] + self.cubes_path()):
   207             hooksfile = join(path, 'application_hooks.py')
   207             hooksfile = join(path, 'application_hooks.py')
   208             if exists(hooksfile):
   208             if exists(hooksfile):
   209                 self.warning('application_hooks.py is deprecated, use dynamic '
   209                 self.warning('application_hooks.py is deprecated, use dynamic '
   210                              'objects to register hooks (%s)', hooksfile)
   210                              'objects to register hooks (%s)', hooksfile)
   211                 context = {}
   211                 context = {}
   212                 # Use execfile rather than `load_module_from_name` because 
   212                 # Use execfile rather than `load_module_from_name` because
   213                 # the latter gets fooled by the `sys.modules` cache when 
   213                 # the latter gets fooled by the `sys.modules` cache when
   214                 # loading different configurations one after the other
   214                 # loading different configurations one after the other
   215                 # (another fix would have been to do :
   215                 # (another fix would have been to do :
   216                 #    sys.modules.pop('applications_hooks')
   216                 #    sys.modules.pop('applications_hooks')
   217                 #  or to modify load_module_from_name so that it provides
   217                 #  or to modify load_module_from_name so that it provides
   218                 #  a use_cache optional parameter
   218                 #  a use_cache optional parameter
   229                 if ertype == 'Any':
   229                 if ertype == 'Any':
   230                     ertype = ''
   230                     ertype = ''
   231                 cb = hookdef.make_callback(event)
   231                 cb = hookdef.make_callback(event)
   232                 hooks.setdefault(event, {}).setdefault(ertype, []).append(cb)
   232                 hooks.setdefault(event, {}).setdefault(ertype, []).append(cb)
   233         return hooks
   233         return hooks
   234     
   234 
   235     def load_schema(self, expand_cubes=False, construction_mode='strict'):
   235     def load_schema(self, expand_cubes=False, **kwargs):
   236         from cubicweb.schema import CubicWebSchemaLoader
   236         from cubicweb.schema import CubicWebSchemaLoader
   237         if expand_cubes:
   237         if expand_cubes:
   238             # in case some new dependencies have been introduced, we have to
   238             # in case some new dependencies have been introduced, we have to
   239             # reinitialize cubes so the full filesystem schema is read
   239             # reinitialize cubes so the full filesystem schema is read
   240             origcubes = self.cubes()
   240             origcubes = self.cubes()
   241             self._cubes = None
   241             self._cubes = None
   242             self.init_cubes(self.expand_cubes(origcubes))
   242             self.init_cubes(self.expand_cubes(origcubes))
   243         schema = CubicWebSchemaLoader().load(self, construction_mode=construction_mode)
   243         schema = CubicWebSchemaLoader().load(self, **kwargs)
   244         if expand_cubes:
   244         if expand_cubes:
   245             # restaure original value
   245             # restaure original value
   246             self._cubes = origcubes
   246             self._cubes = origcubes
   247         return schema
   247         return schema
   248     
   248 
   249     def load_bootstrap_schema(self):
   249     def load_bootstrap_schema(self):
   250         from cubicweb.schema import BootstrapSchemaLoader
   250         from cubicweb.schema import BootstrapSchemaLoader
   251         schema = BootstrapSchemaLoader().load(self)
   251         schema = BootstrapSchemaLoader().load(self)
   252         schema.name = 'bootstrap'
   252         schema.name = 'bootstrap'
   253         return schema
   253         return schema
   254     
   254 
   255     def set_sources_mode(self, sources):
   255     def set_sources_mode(self, sources):
   256         if 'migration' in sources:
   256         if 'migration' in sources:
   257             from cubicweb.server.sources import source_adapter
   257             from cubicweb.server.sources import source_adapter
   258             assert len(sources) == 1
   258             assert len(sources) == 1
   259             enabled_sources = []
   259             enabled_sources = []
   272             for uri in sources:
   272             for uri in sources:
   273                 assert uri in known_sources, uri
   273                 assert uri in known_sources, uri
   274             enabled_sources = sources
   274             enabled_sources = sources
   275         self._enabled_sources = enabled_sources
   275         self._enabled_sources = enabled_sources
   276         clear_cache(self, 'sources')
   276         clear_cache(self, 'sources')
   277         
   277 
   278     def migration_handler(self, schema=None, interactive=True,
   278     def migration_handler(self, schema=None, interactive=True,
   279                           cnx=None, repo=None, connect=True):
   279                           cnx=None, repo=None, connect=True):
   280         """return a migration handler instance"""
   280         """return a migration handler instance"""
   281         from cubicweb.server.migractions import ServerMigrationHelper
   281         from cubicweb.server.migractions import ServerMigrationHelper
   282         return ServerMigrationHelper(self, schema, interactive=interactive,
   282         return ServerMigrationHelper(self, schema, interactive=interactive,