[repoapi, cwconfig] give a convenience method to get a repository from a config
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 09 Jul 2014 16:32:14 +0200
changeset 10568 3914388b2d0f
parent 10567 c316e0049b9c
child 10569 af47954c1015
[repoapi, cwconfig] give a convenience method to get a repository from a config This is a very common need. The private repoapi._get_inmemory_repo becomes config.repository(...)
cwconfig.py
devtools/__init__.py
repoapi.py
web/webconfig.py
--- a/cwconfig.py	Wed Jul 09 15:50:23 2014 +0200
+++ b/cwconfig.py	Wed Jul 09 16:32:14 2014 +0200
@@ -994,6 +994,13 @@
         rtdir = abspath(os.environ.get('CW_RUNTIME_DIR', default))
         return join(rtdir, '%s-%s.pid' % (self.appid, self.name))
 
+    # config -> repository
+
+    def repository(self, vreg=None):
+        from cubicweb.server.repository import Repository
+        from cubicweb.server.utils import TasksManager
+        return Repository(self, TasksManager(), vreg=vreg)
+
     # instance methods used to get instance specific resources #############
 
     def __init__(self, appid, debugmode=False, creating=False):
--- a/devtools/__init__.py	Wed Jul 09 15:50:23 2014 +0200
+++ b/devtools/__init__.py	Wed Jul 09 16:32:14 2014 +0200
@@ -399,9 +399,8 @@
 
     def _new_repo(self, config):
         """Factory method to create a new Repository Instance"""
-        from cubicweb.repoapi import _get_inmemory_repo
         config._cubes = None
-        repo = _get_inmemory_repo(config)
+        repo = config.repository()
         # extending Repository class
         repo._has_started = False
         repo._needs_refresh = False
--- a/repoapi.py	Wed Jul 09 15:50:23 2014 +0200
+++ b/repoapi.py	Wed Jul 09 16:32:14 2014 +0200
@@ -17,21 +17,14 @@
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 """Official API to access the content of a repository
 """
+from warnings import warn
 from logilab.common.deprecation import class_deprecated
 
 from cubicweb.utils import parse_repo_uri
-from cubicweb import ConnectionError, AuthenticationError
+from cubicweb import AuthenticationError
 from cubicweb.server.session import Connection
 
 
-### private function for specific method ############################
-
-def _get_inmemory_repo(config, vreg=None):
-    from cubicweb.server.repository import Repository
-    from cubicweb.server.utils import TasksManager
-    return Repository(config, TasksManager(), vreg=vreg)
-
-
 ### public API ######################################################
 
 def get_repository(uri=None, config=None, vreg=None):
@@ -41,16 +34,11 @@
     The returned repository may be an in-memory repository or a proxy object
     using a specific RPC method, depending on the given URI.
     """
-    if uri is None:
-        return _get_inmemory_repo(config, vreg)
-
-    protocol, hostport, appid = parse_repo_uri(uri)
+    if uri is not None:
+        warn('[3.22] get_repository only wants a config')
 
-    if protocol == 'inmemory':
-        # me may have been called with a dummy 'inmemory://' uri ...
-        return _get_inmemory_repo(config, vreg)
-
-    raise ConnectionError('unknown protocol: `%s`' % protocol)
+    assert config is not None, 'get_repository(config=config)'
+    return config.repository(vreg)
 
 def connect(repo, login, **kwargs):
     """Take credential and return associated Connection.
--- a/web/webconfig.py	Wed Jul 09 15:50:23 2014 +0200
+++ b/web/webconfig.py	Wed Jul 09 16:32:14 2014 +0200
@@ -280,18 +280,6 @@
                 continue
             yield key, pdef
 
-    # don't use @cached: we want to be able to disable it while this must still
-    # be cached
-    def repository(self, vreg=None):
-        """return the instance's repository object"""
-        try:
-            return self.__repo
-        except AttributeError:
-            from cubicweb.repoapi import get_repository
-            repo = get_repository(config=self, vreg=vreg)
-            self.__repo = repo
-            return repo
-
     def vc_config(self):
         return self.repository().get_versions()