[config] cleanup/refactor server sources file values handling
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 02 Jul 2013 17:07:34 +0200
changeset 9460 a2a0bc984863
parent 9459 d3016c08b4ae
child 9461 fc3b8798737c
[config] cleanup/refactor server sources file values handling * kill former `sources` method, misnamed since we've only the system source configuration in the sources file (and some default admin account information) * introduce new system_source_config and default_admin_config properties to access to the two information in this file Then use one or the other when appropriate
devtools/__init__.py
devtools/repotest.py
devtools/testlib.py
misc/migration/3.10.0_Any.py
misc/migration/3.18.0_Any.py
misc/migration/3.8.5_Any.py
misc/scripts/cwuser_ldap2system.py
misc/scripts/ldap_change_base_dn.py
misc/scripts/repair_file_1-9_migration.py
server/__init__.py
server/migractions.py
server/querier.py
server/repository.py
server/serverconfig.py
server/serverctl.py
test/unittest_migration.py
--- a/devtools/__init__.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/devtools/__init__.py	Tue Jul 02 17:07:34 2013 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -190,14 +190,14 @@
             sourcefile = super(TestServerConfiguration, self).sources_file()
         return sourcefile
 
-    def sources(self):
+    def read_sources_file(self):
         """By default, we run tests with the sqlite DB backend.  One may use its
         own configuration by just creating a 'sources' file in the test
         directory from wich tests are launched or by specifying an alternative
         sources file using self.sourcefile.
         """
         try:
-            sources = super(TestServerConfiguration, self).sources()
+            sources = super(TestServerConfiguration, self).read_sources_file()
         except ExecutionError:
             sources = {}
         if not sources:
@@ -388,7 +388,7 @@
         """return Connection object on the current repository"""
         from cubicweb.dbapi import _repo_connect
         repo = self.get_repo()
-        sources = self.config.sources()
+        sources = self.config.read_sources_file()
         login  = unicode(sources['admin']['login'])
         password = sources['admin']['password'] or 'xxx'
         cnx = _repo_connect(repo, login, password=password)
@@ -409,8 +409,7 @@
 
     @property
     def system_source(self):
-        sources = self.config.sources()
-        return sources['system']
+        return self.config.system_source_config
 
     @property
     def dbname(self):
@@ -691,8 +690,8 @@
     def absolute_dbfile(self):
         """absolute path of current database file"""
         dbfile = join(self._ensure_test_backup_db_dir(),
-                      self.config.sources()['system']['db-name'])
-        self.config.sources()['system']['db-name'] = dbfile
+                      self.system_source['db-name'])
+        self.system_source['db-name'] = dbfile
         return dbfile
 
     def process_cache_entry(self, directory, dbname, db_id, entry):
@@ -822,8 +821,7 @@
     handler = HCACHE.get(config)
     if handler is not None:
         return handler
-    sources = config.sources()
-    driver = sources['system']['db-driver']
+    driver = config.system_source_config['db-driver']
     key = (driver, config)
     handlerkls = HANDLERS.get(driver, None)
     if handlerkls is not None:
--- a/devtools/repotest.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/devtools/repotest.py	Tue Jul 02 17:07:34 2013 +0200
@@ -141,7 +141,7 @@
 
 from rql import RQLHelper
 
-from cubicweb.devtools.fake import FakeRepo, FakeSession
+from cubicweb.devtools.fake import FakeRepo, FakeConfig, FakeSession
 from cubicweb.server import set_debug, debugged
 from cubicweb.server.querier import QuerierHelper
 from cubicweb.server.session import Session
@@ -159,7 +159,7 @@
                 raise SkipTest(str(ex))
 
     def setUp(self):
-        self.repo = FakeRepo(self.schema)
+        self.repo = FakeRepo(self.schema, config=FakeConfig(apphome=self.datadir))
         self.repo.system_source = mock_object(dbdriver=self.backend)
         self.rqlhelper = RQLHelper(self.schema, special_relations={'eid': 'uid',
                                                                    'has_text': 'fti'},
--- a/devtools/testlib.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/devtools/testlib.py	Tue Jul 02 17:07:34 2013 +0200
@@ -276,7 +276,7 @@
 
     @deprecated('[3.19] explicitly use RepoAccess object in test instead')
     def set_cnx(self, cnx):
-        """"""
+        """ """
         # XXX we want to deprecate this
         assert getattr(cnx, '_session', None) is not None
         if cnx is self._admin_clt_cnx:
@@ -371,8 +371,7 @@
         db_handler.restore_database(self.test_db_id)
         self.repo = db_handler.get_repo(startup=True)
         # get an admin session (without actual login)
-        sources = db_handler.config.sources()
-        login = unicode(sources['admin']['login'])
+        login = unicode(db_handler.config.default_admin_config['login'])
         self.admin_access = self.new_access(login)
         self._admin_session = self.admin_access._session
         self._admin_clt_cnx = repoapi.ClientConnection(self._admin_session)
@@ -460,7 +459,7 @@
 
         Otherwise, consider to use a different :class:`ApptestConfiguration`
         defined in the `configcls` class attribute"""
-        source = config.sources()['system']
+        source = config.system_source_config
         cls.admlogin = unicode(source['db-user'])
         cls.admpassword = source['db-password']
         # uncomment the line below if you want rql queries to be logged
--- a/misc/migration/3.10.0_Any.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/misc/migration/3.10.0_Any.py	Tue Jul 02 17:07:34 2013 +0200
@@ -1,6 +1,6 @@
 from cubicweb.server.session import hooks_control
 
-for uri, cfg in config.sources().items():
+for uri, cfg in config.read_sources_file().items():
     if uri in ('system', 'admin'):
         continue
     repo.sources_by_uri[uri] = repo.get_source(cfg['adapter'], uri, cfg.copy())
@@ -18,7 +18,7 @@
     'WHERE s.cw_name=e.type')
 commit()
 
-for uri, cfg in config.sources().items():
+for uri, cfg in config.read_sources_file().items():
     if uri in ('system', 'admin'):
         continue
     repo.sources_by_uri.pop(uri)
--- a/misc/migration/3.18.0_Any.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/misc/migration/3.18.0_Any.py	Tue Jul 02 17:07:34 2013 +0200
@@ -1,4 +1,4 @@
-driver = config.sources()['system']['db-driver']
+driver = config.system_source_config['db-driver']
 if not (driver == 'postgres' or driver.startswith('sqlserver')):
     import sys
     print >>sys.stderr, 'This migration is not supported for backends other than sqlserver or postgres (yet).'
--- a/misc/migration/3.8.5_Any.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/misc/migration/3.8.5_Any.py	Tue Jul 02 17:07:34 2013 +0200
@@ -1,5 +1,5 @@
 def migrate_varchar_to_nvarchar():
-    dbdriver  = config.sources()['system']['db-driver']
+    dbdriver  = config.system_source_config['db-driver']
     if dbdriver != "sqlserver2005":
         return
 
--- a/misc/scripts/cwuser_ldap2system.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/misc/scripts/cwuser_ldap2system.py	Tue Jul 02 17:07:34 2013 +0200
@@ -1,7 +1,7 @@
 import base64
 from cubicweb.server.utils import crypt_password
 
-dbdriver  = config.sources()['system']['db-driver']
+dbdriver  = config.system_source_config['db-driver']
 from logilab.database import get_db_helper
 dbhelper = get_db_helper(driver)
 
--- a/misc/scripts/ldap_change_base_dn.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/misc/scripts/ldap_change_base_dn.py	Tue Jul 02 17:07:34 2013 +0200
@@ -6,7 +6,7 @@
     print
     print 'you should not have updated your sources file yet'
 
-olddn = repo.config.sources()[uri]['user-base-dn']
+olddn = repo.sources_by_uri[uri].config['user-base-dn']
 
 assert olddn != newdn
 
--- a/misc/scripts/repair_file_1-9_migration.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/misc/scripts/repair_file_1-9_migration.py	Tue Jul 02 17:07:34 2013 +0200
@@ -15,11 +15,11 @@
 from cubicweb import cwconfig, dbapi
 from cubicweb.server.session import hooks_control
 
-sourcescfg = repo.config.sources()
+defaultadmin = repo.config.default_admin_config
 backupcfg = cwconfig.instance_configuration(backupinstance)
 backupcfg.repairing = True
-backuprepo, backupcnx = dbapi.in_memory_repo_cnx(backupcfg, sourcescfg['admin']['login'],
-                                                 password=sourcescfg['admin']['password'],
+backuprepo, backupcnx = dbapi.in_memory_repo_cnx(backupcfg, defaultadmin['login'],
+                                                 password=defaultadmin['password'],
                                                  host='localhost')
 backupcu = backupcnx.cursor()
 
--- a/server/__init__.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/server/__init__.py	Tue Jul 02 17:07:34 2013 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -216,7 +216,7 @@
     # only enable the system source at initialization time
     repo = Repository(config, vreg=vreg)
     schema = repo.schema
-    sourcescfg = config.sources()
+    sourcescfg = config.read_sources_file()
     source = sourcescfg['system']
     driver = source['db-driver']
     sqlcnx = repo.system_source.get_connection()
--- a/server/migractions.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/server/migractions.py	Tue Jul 02 17:07:34 2013 +0200
@@ -265,10 +265,9 @@
         try:
             return self._cnx
         except AttributeError:
-            sourcescfg = self.repo.config.sources()
             try:
-                login = sourcescfg['admin']['login']
-                pwd = sourcescfg['admin']['password']
+                login = self.repo.config.default_admin_config['login']
+                pwd = self.repo.config.default_admin_config['password']
             except KeyError:
                 login, pwd = manager_userpasswd()
             while True:
--- a/server/querier.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/server/querier.py	Tue Jul 02 17:07:34 2013 +0200
@@ -516,7 +516,7 @@
         self.solutions = repo.vreg.solutions
         rqlhelper = repo.vreg.rqlhelper
         # set backend on the rql helper, will be used for function checking
-        rqlhelper.backend = repo.config.sources()['system']['db-driver']
+        rqlhelper.backend = repo.config.system_source_config['db-driver']
         self._parse = rqlhelper.parse
         self._annotate = rqlhelper.annotate
         # rql planner
--- a/server/repository.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/server/repository.py	Tue Jul 02 17:07:34 2013 +0200
@@ -189,7 +189,7 @@
         self.shutting_down = False
         # sources (additional sources info in the system database)
         self.system_source = self.get_source('native', 'system',
-                                             config.sources()['system'].copy())
+                                             config.system_source_config.copy())
         self.sources_by_uri = {'system': self.system_source}
         # querier helper, need to be created after sources initialization
         self.querier = querier.QuerierHelper(self, self.schema)
--- a/server/serverconfig.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/server/serverconfig.py	Tue Jul 02 17:07:34 2013 +0200
@@ -297,13 +297,16 @@
     # configuration file (#16102)
     @cached
     def read_sources_file(self):
+        """return a dictionary of values found in the sources file"""
         return read_config(self.sources_file(), raise_if_unreadable=True)
 
-    def sources(self):
-        """return a dictionnaries containing sources definitions indexed by
-        sources'uri
-        """
-        return self.read_sources_file()
+    @property
+    def system_source_config(self):
+        return self.read_sources_file()['system']
+
+    @property
+    def default_admin_config(self):
+        return self.read_sources_file()['admin']
 
     def source_enabled(self, source):
         if self.sources_mode is not None:
--- a/server/serverctl.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/server/serverctl.py	Tue Jul 02 17:07:34 2013 +0200
@@ -136,8 +136,8 @@
     from cubicweb.dbapi import in_memory_repo_cnx
     from cubicweb.server.utils import manager_userpasswd
     try:
-        login = config.sources()['admin']['login']
-        pwd = config.sources()['admin']['password']
+        login = config.default_admin_config['login']
+        pwd = config.default_admin_config['password']
     except KeyError:
         login, pwd = manager_userpasswd()
     while True:
@@ -221,7 +221,7 @@
     def cleanup(self):
         """remove instance's configuration and database"""
         from logilab.database import get_db_helper
-        source = self.config.sources()['system']
+        source = self.config.system_source_config
         dbname = source['db-name']
         helper = get_db_helper(source['db-driver'])
         if ASK.confirm('Delete database %s ?' % dbname):
@@ -334,7 +334,7 @@
         automatic = self.get('automatic')
         appid = args.pop()
         config = ServerConfiguration.config_for(appid)
-        source = config.sources()['system']
+        source = config.system_source_config
         dbname = source['db-name']
         driver = source['db-driver']
         helper = get_db_helper(driver)
@@ -441,7 +441,7 @@
         appid = args[0]
         config = ServerConfiguration.config_for(appid)
         try:
-            system = config.sources()['system']
+            system = config.system_source_config
             extra_args = system.get('db-extra-arguments')
             extra = extra_args and {'extra_args': extra_args} or {}
             get_connection(
@@ -544,7 +544,7 @@
         from cubicweb.server.sqlutils import sqlexec, sqlgrants
         appid, user = args
         config = ServerConfiguration.config_for(appid)
-        source = config.sources()['system']
+        source = config.system_source_config
         set_owner = self.config.set_owner
         cnx = system_source_cnx(source, special_privs='GRANT')
         cursor = cnx.cursor()
--- a/test/unittest_migration.py	Wed Jan 22 12:14:56 2014 +0100
+++ b/test/unittest_migration.py	Tue Jul 02 17:07:34 2013 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -99,7 +99,7 @@
     def test_db_creation(self):
         """make sure database can be created"""
         config = ApptestConfiguration('data', apphome=self.datadir)
-        source = config.sources()['system']
+        source = config.system_source_config
         self.assertEqual(source['db-driver'], 'sqlite')
         handler = get_test_db_handler(config)
         handler.init_test_database()