changeset 6577:69cd0c056aa1

config: initialize routes directly from RootController Let RootController directly initialize routes instead of app_cfg injecting the mapper. For test initialization, we also need a handle to the mapper. We could either recreate it (but fragile if the real mapper initialization is changed later) or obtain it from a fresh RootController. This commit opts for the latter.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Sat, 18 Mar 2017 21:37:27 +0100
parents b4c27fe6438c
children 0d4dd9380a45
files kallithea/config/app_cfg.py kallithea/controllers/root.py kallithea/tests/conftest.py
diffstat 3 files changed, 13 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/config/app_cfg.py	Tue Apr 04 22:23:20 2017 +0200
+++ b/kallithea/config/app_cfg.py	Sat Mar 18 21:37:27 2017 +0100
@@ -28,7 +28,6 @@
 from kallithea.lib.middleware.https_fixup import HttpsFixup
 from kallithea.lib.middleware.simplegit import SimpleGit
 from kallithea.lib.middleware.simplehg import SimpleHg
-from kallithea.config.routing import make_map
 from kallithea.lib.auth import set_available_permissions
 from kallithea.lib.db_manage import DbManage
 from kallithea.lib.utils import load_rcextensions, make_ui, set_app_settings, set_vcs_config, \
@@ -119,10 +118,6 @@
     kallithea.CELERY_EAGER = str2bool(config['app_conf'].get('celery.always.eager'))
     kallithea.CONFIG = config
 
-    # Provide routes mapper to the RoutedController
-    root_controller = app.find_controller('root')
-    root_controller.mapper = config['routes.map'] = make_map(config)
-
     load_rcextensions(root_path=config['here'])
 
     # FIXME move test setup code out of here
--- a/kallithea/controllers/root.py	Tue Apr 04 22:23:20 2017 +0200
+++ b/kallithea/controllers/root.py	Sat Mar 18 21:37:27 2017 +0100
@@ -12,20 +12,21 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 from tgext.routes import RoutedController
+from kallithea.config.routing import make_map
 from kallithea.lib.base import BaseController
 from kallithea.controllers.error import ErrorController
-
+from tg import config
 
-# With TurboGears, the RootController is the controller from which all routing
-# starts from. It is 'magically' found based on the fact that a controller
-# 'foo' is expected to have a class name FooController, located in a file
-# foo.py, inside config['paths']['controllers']. The name 'root' for the root
-# controller is the default name. The dictionary config['paths'] determines the
-# directories where templates, static files and controllers are found. It is
-# set up in tg.AppConfig based on AppConfig['package'] ('kallithea') and the
-# respective defaults 'templates', 'public' and 'controllers'.
-# Inherit from RoutedController to allow Kallithea to use regex-based routing.
+# This is the main Kallithea entry point; TurboGears will forward all requests
+# to an instance of 'controller.root.RootController' in the configured
+# 'application' module (set by app_cfg.py).  Requests are forwarded to
+# controllers based on the routing mapper that lives in this root instance.
+# The mapper is configured using routes defined in routing.py.  This use of the
+# 'mapper' attribute is a feature of tgext.routes, which is activated by
+# inheriting from its RoutedController class.
 class RootController(RoutedController, BaseController):
 
+    mapper = make_map(config)
+
     # the following assignment hooks in error handling
     error = ErrorController()
--- a/kallithea/tests/conftest.py	Tue Apr 04 22:23:20 2017 +0200
+++ b/kallithea/tests/conftest.py	Sat Mar 18 21:37:27 2017 +0100
@@ -8,6 +8,7 @@
 from tg import config
 
 import pytest
+from kallithea.controllers.root import RootController
 from kallithea.model.user import UserModel
 from kallithea.model.meta import Session
 from kallithea.model.db import Setting, User, UserIpMap
@@ -26,7 +27,7 @@
     kallithea.tests.base.testapp = loadapp('config:kallithea/tests/test.ini', relative_to=path)
     logging.disable(logging.NOTSET)
 
-    kallithea.tests.base.url = URLGenerator(config['routes.map'], kallithea.tests.base.environ)
+    kallithea.tests.base.url = URLGenerator(RootController().mapper, kallithea.tests.base.environ)
 
 
 @pytest.fixture