server/serverconfig.py
changeset 6427 c8a5ac2d1eaa
parent 6333 e3994fcc21c3
child 6478 8098e1295cf0
--- a/server/serverconfig.py	Sat Oct 09 00:05:50 2010 +0200
+++ b/server/serverconfig.py	Sat Oct 09 00:05:52 2010 +0200
@@ -19,10 +19,11 @@
 
 __docformat__ = "restructuredtext en"
 
+import sys
 from os.path import join, exists
+from StringIO import StringIO
 
-from logilab.common.configuration import REQUIRED, Method, Configuration, \
-     ini_format_section
+import logilab.common.configuration as lgconfig
 from logilab.common.decorators import wproperty, cached
 
 from cubicweb.toolsutils import read_config, restrict_perms_to_user
@@ -38,13 +39,13 @@
                'level': 0,
                }),
     ('password', {'type' : 'password',
-                  'default': REQUIRED,
+                  'default': lgconfig.REQUIRED,
                   'help': "cubicweb manager account's password",
                   'level': 0,
                   }),
     )
 
-class SourceConfiguration(Configuration):
+class SourceConfiguration(lgconfig.Configuration):
     def __init__(self, appconfig, options):
         self.appconfig = appconfig # has to be done before super call
         super(SourceConfiguration, self).__init__(options=options)
@@ -54,54 +55,36 @@
         return self.appconfig.appid
 
     def input_option(self, option, optdict, inputlevel):
-        if self['db-driver'] == 'sqlite':
-            if option in ('db-user', 'db-password'):
-                return
-            if option == 'db-name':
-                optdict = optdict.copy()
-                optdict['help'] = 'path to the sqlite database'
-                optdict['default'] = join(self.appconfig.appdatahome,
-                                          self.appconfig.appid + '.sqlite')
+        try:
+            dbdriver = self['db-driver']
+        except lgconfig.OptionError:
+            pass
+        else:
+            if dbdriver == 'sqlite':
+                if option in ('db-user', 'db-password'):
+                    return
+                if option == 'db-name':
+                    optdict = optdict.copy()
+                    optdict['help'] = 'path to the sqlite database'
+                    optdict['default'] = join(self.appconfig.appdatahome,
+                                              self.appconfig.appid + '.sqlite')
         super(SourceConfiguration, self).input_option(option, optdict, inputlevel)
 
 
-def generate_sources_file(appconfig, sourcesfile, sourcescfg, keys=None):
-    """serialize repository'sources configuration into a INI like file
+
+def ask_source_config(appconfig, type, inputlevel=0):
+    options = SOURCE_TYPES[type].options
+    sconfig = SourceConfiguration(appconfig, options=options)
+    sconfig.input_config(inputlevel=inputlevel)
+    return sconfig
 
-    the `keys` parameter may be used to sort sections
-    """
-    if keys is None:
-        keys = sourcescfg.keys()
-    else:
-        for key in sourcescfg:
-            if not key in keys:
-                keys.append(key)
-    stream = open(sourcesfile, 'w')
-    for uri in keys:
-        sconfig = sourcescfg[uri]
-        if isinstance(sconfig, dict):
-            # get a Configuration object
-            if uri == 'admin':
-                options = USER_OPTIONS
-            else:
-                options = SOURCE_TYPES[sconfig['adapter']].options
-            _sconfig = SourceConfiguration(appconfig, options=options)
-            for attr, val in sconfig.items():
-                if attr == 'uri':
-                    continue
-                if attr == 'adapter':
-                    _sconfig.adapter = val
-                else:
-                    _sconfig.set_option(attr, val)
-            sconfig = _sconfig
-        optsbysect = list(sconfig.options_by_section())
-        assert len(optsbysect) == 1, 'all options for a source should be in the same group'
-        ini_format_section(stream, uri, optsbysect[0][1])
-        if hasattr(sconfig, 'adapter'):
-            print >> stream
-            print >> stream, '# adapter for this source (YOU SHOULD NOT CHANGE THIS)'
-            print >> stream, 'adapter=%s' % sconfig.adapter
-        print >> stream
+def generate_source_config(sconfig):
+    """serialize a repository source configuration as text"""
+    stream = StringIO()
+    optsbysect = list(sconfig.options_by_section())
+    assert len(optsbysect) == 1, 'all options for a source should be in the same group'
+    lgconfig.ini_format(stream, optsbysect[0][1], sys.stdin.encoding)
+    return stream.getvalue()
 
 
 class ServerConfiguration(CubicWebConfiguration):
@@ -121,7 +104,7 @@
           }),
         ('pid-file',
          {'type' : 'string',
-          'default': Method('default_pid_file'),
+          'default': lgconfig.Method('default_pid_file'),
           'help': 'repository\'s pid file',
           'group': 'main', 'level': 2,
           }),
@@ -282,16 +265,43 @@
         """
         return self.read_sources_file()
 
-    def source_enabled(self, uri):
-        return not self.enabled_sources or uri in self.enabled_sources
+    def source_enabled(self, source):
+        if self.sources_mode is not None:
+            if 'migration' in self.sources_mode:
+                assert len(self.sources_mode) == 1
+                if source.connect_for_migration:
+                    return True
+                print 'not connecting to source', uri, 'during migration'
+                return False
+            if 'all' in self.sources_mode:
+                assert len(self.sources_mode) == 1
+                return True
+            return source.uri in self.sources_mode
+        if self.quick_start:
+            return False
+        return (not source.disabled and (
+            not self.enabled_sources or source.uri in self.enabled_sources))
 
     def write_sources_file(self, sourcescfg):
+        """serialize repository'sources configuration into a INI like file"""
         sourcesfile = self.sources_file()
         if exists(sourcesfile):
             import shutil
             shutil.copy(sourcesfile, sourcesfile + '.bak')
-        generate_sources_file(self, sourcesfile, sourcescfg,
-                              ['admin', 'system'])
+        stream = open(sourcesfile, 'w')
+        for section in ('admin', 'system'):
+            sconfig = sourcescfg[section]
+            if isinstance(sconfig, dict):
+                # get a Configuration object
+                assert section == 'system'
+                _sconfig = SourceConfiguration(
+                    self, options=SOURCE_TYPES['native'].options)
+                for attr, val in sconfig.items():
+                    _sconfig.set_option(attr, val)
+                sconfig = _sconfig
+            print >> stream, '[%s]' % section
+            print >> stream, generate_source_config(sconfig)
+            print >> stream
         restrict_perms_to_user(sourcesfile)
 
     def pyro_enabled(self):
@@ -318,27 +328,9 @@
         schema.name = 'bootstrap'
         return schema
 
+    sources_mode = None
     def set_sources_mode(self, sources):
-        if 'migration' in sources:
-            from cubicweb.server.sources import source_adapter
-            assert len(sources) == 1
-            enabled_sources = []
-            for uri, config in self.sources().iteritems():
-                if uri == 'admin':
-                    continue
-                if source_adapter(config).connect_for_migration:
-                    enabled_sources.append(uri)
-                else:
-                    print 'not connecting to source', uri, 'during migration'
-        elif 'all' in sources:
-            assert len(sources) == 1
-            enabled_sources = None
-        else:
-            known_sources = self.sources()
-            for uri in sources:
-                assert uri in known_sources, uri
-            enabled_sources = sources
-        self.enabled_sources = enabled_sources
+        self.sources_mode = sources
 
     def migration_handler(self, schema=None, interactive=True,
                           cnx=None, repo=None, connect=True, verbosity=None):