From 762d7196430a681e2fd5e55c253e3e2ea04847cc Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Sat, 2 Jul 2011 13:31:39 +0900 Subject: mount subapplication --- pyramid/config.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ pyramid/tests/test_config.py | 51 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/pyramid/config.py b/pyramid/config.py index bf3793c26..934f56050 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -293,6 +293,7 @@ class Configurator(object): default_view_mapper=None, autocommit=False, exceptionresponse_view=default_exceptionresponse_view, + root_route_name=None, ): if package is None: package = caller_package() @@ -302,6 +303,7 @@ class Configurator(object): self.package = name_resolver.package self.registry = registry self.autocommit = autocommit + self.root_route_name = root_route_name if registry is None: registry = Registry(self.package_name) self.registry = registry @@ -601,6 +603,50 @@ class Configurator(object): config = self.__class__.with_context(context) c(config) + def with_root_route(self, route_name): + mapper = self.get_routes_mapper() + route = mapper.get_route(route_name) + if route is None: + raise ConfigurationError + configurator = self.__class__(registry=self.registry, + package=self.package, + autocommit=self.autocommit, + root_route_name=route_name) + return configurator + + def mount(self, function, route_name): + """ mount subapplication on route named ``route_name``. + + + .. code-block:: python + :linenos: + + from pyramid.config import Configurator + + def main(global_config, **settings): + config = Configurator() + config.add_route('admin', '/admin') + config.mount('myapp.myconfig.includeme', 'admin') + + Because the function is named ``includeme``, the function name can + also be omitted from the dotted name reference: + + .. code-block:: python + :linenos: + + from pyramid.config import Configurator + + def includeme(config): + config.add_route('projects', 'projects') + + + Subapplication's routes are registerd under the ``root_route``. + In this case, ``projects`` route is registered with ``/admin/projects`` pattern. + """ + function = self.maybe_dotted(function) + config = self.with_root_route(route_name) + function(config) + def add_directive(self, name, directive, action_wrap=True): """ Add a directive method to the configurator. @@ -1875,6 +1921,12 @@ class Configurator(object): if pattern is None: raise ConfigurationError('"pattern" argument may not be None') + if self.root_route_name is not None: + root_route = mapper.get_route(self.root_route_name) + if root_route is None: + raise ConfigurationError('route %s is not registered' % self.root_route_name) + pattern = root_route.pattern.rstrip() + '/' + pattern.lstrip() + discriminator = ['route', name, xhr, request_method, path_info, request_param, header, accept] discriminator.extend(sorted(custom_predicates)) diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 20fdd93e8..07d44f176 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -1975,6 +1975,55 @@ class ConfiguratorTests(unittest.TestCase): self._assertRoute(config, 'name', 'path') self.assertEqual(route.name, 'name') + def test_add_route_with_root_route(self): + from pyramid.interfaces import IRoutesMapper + root_config = self._makeOne(autocommit=True) + root_config.add_route('root_name', 'root') + config = self._makeOne(registry=root_config.registry, autocommit=True, root_route_name='root_name') + route = config.add_route('name', 'path') + self.assertEqual(route.name, 'name') + self.assertEqual(route.pattern, 'root/path') + + mapper = config.registry.getUtility(IRoutesMapper) + routes = mapper.get_routes() + route = routes[1] + self.assertEqual(len(routes), 2) + self.assertEqual(route.name, 'name') + self.assertEqual(route.path, 'root/path') + + def test_add_route_with_root_route_configrationerror(self): + from pyramid.exceptions import ConfigurationError + config = self._makeOne(autocommit=True, root_route_name='root_name') + try: + route = config.add_route('name', 'path') + self.fail() + except ConfigurationError: + pass + + def test_with_root_route(self): + root_config = self._makeOne(autocommit=True) + root_config.add_route('root_name', 'root') + config = root_config.with_root_route('root_name') + self.assertEqual(config.registry, root_config.registry) + self.assertEqual(config.package, root_config.package) + self.assertEqual(config.root_route_name, 'root_name') + + def test_with_root_route_configerror(self): + from pyramid.exceptions import ConfigurationError + root_config = self._makeOne(autocommit=True) + try: + config = root_config.with_root_route('root_name') + self.fail() + except ConfigurationError: + pass + + def test_mount(self): + root_config = self._makeOne(autocommit=True) + root_config.add_route('root_name', 'root') + def dummy_subapp(config): + self.assertEqual(config.root_route_name, 'root_name') + root_config.mount(dummy_subapp, 'root_name') + def test_add_route_with_factory(self): config = self._makeOne(autocommit=True) factory = object() @@ -5263,7 +5312,7 @@ def dummy_extend(config, discrim): def dummy_extend2(config, discrim): config.action(discrim, None, config.registry) - + class DummyRegistry(object): def __init__(self, adaptation=None): self.utilities = [] -- cgit v1.2.3 From 16cf0201039d26b180db7109dda77bd2c9850e9d Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Sat, 2 Jul 2011 13:33:01 +0900 Subject: sign to contributors --- CONTRIBUTORS.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index cdc011f87..a9ef27c28 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -142,3 +142,5 @@ Contributors - Michael Merickel, 2011/5/25 - Christoph Zwerschke, 2011/06/07 + +- Atsushi Odagiri, 2011/07/02 -- cgit v1.2.3 From 5e0a198348ed0056fd5127b024bc4ec76cd84b70 Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Thu, 30 Jun 2011 20:24:37 +0900 Subject: add_route with root_route --- pyramid/config.py | 16 ++++++++++++++++ pyramid/tests/test_config.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pyramid/config.py b/pyramid/config.py index 934f56050..4143f5296 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -1550,6 +1550,22 @@ class Configurator(object): DeprecationWarning, 4) + def with_root_route(self, route_name): + mapper = self.get_routes_mapper() + route = mapper.get_route(route_name) + if route is None: + raise ConfigurationError + configurator = self.__class__(registry=self.registry, + package=self.package, + autocommit=self.autocommit, + root_route_name=route_name) + return configurator + + def mount(self, function, route_name): + function = self.maybe_dotted(function) + config = self.with_root_route(route_name) + function(config) + @action_method def add_route(self, name, diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 07d44f176..706170dcb 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -5326,4 +5326,4 @@ class DummyRegistry(object): def registerAdapter(self, *arg, **kw): self.adapters.append((arg, kw)) def queryAdapter(self, *arg, **kw): - return self.adaptation + return self.adaptation \ No newline at end of file -- cgit v1.2.3 From da780fb9adbb5be06b8e2a115e04b0a533c90629 Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Thu, 30 Jun 2011 21:04:44 +0900 Subject: add test --- pyramid/tests/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 706170dcb..07d44f176 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -5326,4 +5326,4 @@ class DummyRegistry(object): def registerAdapter(self, *arg, **kw): self.adapters.append((arg, kw)) def queryAdapter(self, *arg, **kw): - return self.adaptation \ No newline at end of file + return self.adaptation -- cgit v1.2.3 From 1c6fd879acaf2461c78263b2c21711c65ed668ed Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Thu, 30 Jun 2011 21:14:09 +0900 Subject: use with includeme --- pyramid/config.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index 4143f5296..31cd44222 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -601,6 +601,7 @@ class Configurator(object): context.includepath = _context.includepath + (spec,) context.package = package_of(module) config = self.__class__.with_context(context) + config.root_route_name = self.root_route_name c(config) def with_root_route(self, route_name): @@ -1561,10 +1562,13 @@ class Configurator(object): root_route_name=route_name) return configurator - def mount(self, function, route_name): - function = self.maybe_dotted(function) + def mount(self, includeme, route_name): + """ mount subapplication on route named ``route_name``. + """ + config = self.with_root_route(route_name) - function(config) + config.include(includeme) + @action_method def add_route(self, -- cgit v1.2.3 From 1227172e2a8edf66ec0f2795fc744007938a56f2 Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Fri, 1 Jul 2011 22:20:41 +0900 Subject: garden --- pyramid/config.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index 31cd44222..78dd78a4f 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -601,7 +601,6 @@ class Configurator(object): context.includepath = _context.includepath + (spec,) context.package = package_of(module) config = self.__class__.with_context(context) - config.root_route_name = self.root_route_name c(config) def with_root_route(self, route_name): @@ -1551,24 +1550,6 @@ class Configurator(object): DeprecationWarning, 4) - def with_root_route(self, route_name): - mapper = self.get_routes_mapper() - route = mapper.get_route(route_name) - if route is None: - raise ConfigurationError - configurator = self.__class__(registry=self.registry, - package=self.package, - autocommit=self.autocommit, - root_route_name=route_name) - return configurator - - def mount(self, includeme, route_name): - """ mount subapplication on route named ``route_name``. - """ - - config = self.with_root_route(route_name) - config.include(includeme) - @action_method def add_route(self, -- cgit v1.2.3 From e58daf95b98a76f2b58a42568a15e8c4ad4f59ae Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Tue, 5 Jul 2011 19:53:30 +0900 Subject: mount subapplication with route_prefix --- pyramid/config.py | 30 ++++++++++------------------ pyramid/tests/test_config.py | 47 +++++++++++--------------------------------- 2 files changed, 21 insertions(+), 56 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index 78dd78a4f..0724d10ae 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -293,7 +293,6 @@ class Configurator(object): default_view_mapper=None, autocommit=False, exceptionresponse_view=default_exceptionresponse_view, - root_route_name=None, ): if package is None: package = caller_package() @@ -303,7 +302,6 @@ class Configurator(object): self.package = name_resolver.package self.registry = registry self.autocommit = autocommit - self.root_route_name = root_route_name if registry is None: registry = Registry(self.package_name) self.registry = registry @@ -603,19 +601,15 @@ class Configurator(object): config = self.__class__.with_context(context) c(config) - def with_root_route(self, route_name): - mapper = self.get_routes_mapper() - route = mapper.get_route(route_name) - if route is None: - raise ConfigurationError + def with_route_prefix(self, route_prefix): configurator = self.__class__(registry=self.registry, package=self.package, - autocommit=self.autocommit, - root_route_name=route_name) + autocommit=self.autocommit) + configurator.route_prefix = route_prefix return configurator - def mount(self, function, route_name): - """ mount subapplication on route named ``route_name``. + def mount_subapplication(self, function, route_prefix): + """ mount subapplication with ``route_prefix`` .. code-block:: python @@ -625,8 +619,7 @@ class Configurator(object): def main(global_config, **settings): config = Configurator() - config.add_route('admin', '/admin') - config.mount('myapp.myconfig.includeme', 'admin') + config.mount('myapp.myconfig.includeme', '/admin') Because the function is named ``includeme``, the function name can also be omitted from the dotted name reference: @@ -640,11 +633,11 @@ class Configurator(object): config.add_route('projects', 'projects') - Subapplication's routes are registerd under the ``root_route``. + Subapplication's routes are registerd with the ``route_prefix`` In this case, ``projects`` route is registered with ``/admin/projects`` pattern. """ function = self.maybe_dotted(function) - config = self.with_root_route(route_name) + config = self.with_route_prefix(route_prefix) function(config) def add_directive(self, name, directive, action_wrap=True): @@ -1922,11 +1915,8 @@ class Configurator(object): if pattern is None: raise ConfigurationError('"pattern" argument may not be None') - if self.root_route_name is not None: - root_route = mapper.get_route(self.root_route_name) - if root_route is None: - raise ConfigurationError('route %s is not registered' % self.root_route_name) - pattern = root_route.pattern.rstrip() + '/' + pattern.lstrip() + if hasattr(self, "route_prefix"): + pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') discriminator = ['route', name, xhr, request_method, path_info, request_param, header, accept] diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 07d44f176..63644da2d 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -1975,54 +1975,29 @@ class ConfiguratorTests(unittest.TestCase): self._assertRoute(config, 'name', 'path') self.assertEqual(route.name, 'name') - def test_add_route_with_root_route(self): + def test_add_route_with_route_prefix(self): from pyramid.interfaces import IRoutesMapper - root_config = self._makeOne(autocommit=True) - root_config.add_route('root_name', 'root') - config = self._makeOne(registry=root_config.registry, autocommit=True, root_route_name='root_name') + config = self._makeOne(autocommit=True) + config.route_prefix = 'root' route = config.add_route('name', 'path') self.assertEqual(route.name, 'name') self.assertEqual(route.pattern, 'root/path') - mapper = config.registry.getUtility(IRoutesMapper) - routes = mapper.get_routes() - route = routes[1] - self.assertEqual(len(routes), 2) - self.assertEqual(route.name, 'name') - self.assertEqual(route.path, 'root/path') - - def test_add_route_with_root_route_configrationerror(self): - from pyramid.exceptions import ConfigurationError - config = self._makeOne(autocommit=True, root_route_name='root_name') - try: - route = config.add_route('name', 'path') - self.fail() - except ConfigurationError: - pass + self._assertRoute(config, 'name', 'root/path') - def test_with_root_route(self): + def test_with_route_prefix(self): root_config = self._makeOne(autocommit=True) - root_config.add_route('root_name', 'root') - config = root_config.with_root_route('root_name') + config = root_config.with_route_prefix('root') self.assertEqual(config.registry, root_config.registry) self.assertEqual(config.package, root_config.package) - self.assertEqual(config.root_route_name, 'root_name') - - def test_with_root_route_configerror(self): - from pyramid.exceptions import ConfigurationError - root_config = self._makeOne(autocommit=True) - try: - config = root_config.with_root_route('root_name') - self.fail() - except ConfigurationError: - pass + self.assertEqual(config.route_prefix, 'root') - def test_mount(self): + def test_mount_subapplication(self): root_config = self._makeOne(autocommit=True) - root_config.add_route('root_name', 'root') def dummy_subapp(config): - self.assertEqual(config.root_route_name, 'root_name') - root_config.mount(dummy_subapp, 'root_name') + self.assertEqual(config.route_prefix, 'root') + root_config.mount_subapplication(dummy_subapp, + route_prefix='root') def test_add_route_with_factory(self): config = self._makeOne(autocommit=True) -- cgit v1.2.3 From d46cd6ad3bc7e7b8169bf088f6a91040bf7f6a45 Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Tue, 5 Jul 2011 20:05:12 +0900 Subject: garden --- pyramid/config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index 0724d10ae..732d2c521 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -621,8 +621,8 @@ class Configurator(object): config = Configurator() config.mount('myapp.myconfig.includeme', '/admin') - Because the function is named ``includeme``, the function name can - also be omitted from the dotted name reference: + ``myapp.myconfig`` is subapplication + and ``includeme`` is that mount point. .. code-block:: python :linenos: @@ -630,7 +630,7 @@ class Configurator(object): from pyramid.config import Configurator def includeme(config): - config.add_route('projects', 'projects') + config.add_route('projects', '/projects') Subapplication's routes are registerd with the ``route_prefix`` -- cgit v1.2.3