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 25025a7186c2676a378361212df46d9a3d365dab 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 3ad872e27..eb32ca9b9 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 @@ -602,6 +604,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. @@ -1930,6 +1976,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 fa1ad2b88..7f103251b 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -1996,6 +1996,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() @@ -5379,7 +5428,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 54aa947127436e1cb9317c4b96f3901525459c42 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 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 From 0b13e1ae46ec25722f5651c73b8510e4336dedc5 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Tue, 5 Jul 2011 18:06:34 -0500 Subject: Refactored the mount code into config.include. --- pyramid/config.py | 110 +++++++++++++++++++------------------------ pyramid/tests/test_config.py | 28 +++++------ 2 files changed, 61 insertions(+), 77 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index 61463eb86..b39cb365e 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -269,7 +269,11 @@ class Configurator(object): Pyramid's caller. By default, the ``pyramid.httpexceptions.default_exceptionresponse_view`` function is used as the ``exceptionresponse_view``. This argument is new - in Pyramid 1.1. """ + in Pyramid 1.1. + + If ``route_prefix`` is passed, all routes added with + :meth:`pyramid.config.Configurator.add_route` will have the specified path + prepended to their pattern. This parameter is new in Pyramid 1.x.""" manager = manager # for testing injection venusian = venusian # for testing injection @@ -293,7 +297,7 @@ class Configurator(object): default_view_mapper=None, autocommit=False, exceptionresponse_view=default_exceptionresponse_view, - root_route_name=None, + route_prefix=None, ): if package is None: package = caller_package() @@ -303,7 +307,7 @@ class Configurator(object): self.package = name_resolver.package self.registry = registry self.autocommit = autocommit - self.root_route_name = root_route_name + self.route_prefix = route_prefix if registry is None: registry = Registry(self.package_name) self.registry = registry @@ -455,6 +459,7 @@ class Configurator(object): registerCommonDirectives(context) context.registry = self.registry context.autocommit = autocommit + context.route_prefix = self.route_prefix return context # API @@ -522,10 +527,14 @@ class Configurator(object): # unwrap and reset the context self._ctx = None - def include(self, *callables): - """Include one or more configuration callables, to support imperative + def include(self, callable, route_prefix=None): + """Include a configuration callables, to support imperative application extensibility. + .. warning:: In versions of :app:`Pyramid` prior to 1.x, this + function accepted ``*callables``, but this has been changed + to support only a single callable. + A configuration callable should be a callable that accepts a single argument named ``config``, which will be an instance of a :term:`Configurator` (be warned that it will not be the same @@ -534,7 +543,7 @@ class Configurator(object): methods on the configurator passed to it which add configuration state. The return value of a callable will be ignored. - Values allowed to be presented via the ``*callables`` argument to + Values allowed to be presented via the ``callable`` argument to this method: any callable Python object or any :term:`dotted Python name` which resolves to a callable Python object. It may also be a Python :term:`module`, in which case, the module will be searched for @@ -583,65 +592,41 @@ class Configurator(object): Included configuration statements will be overridden by local configuration statements if an included callable causes a configuration conflict by registering something with the same - configuration parameters.""" + configuration parameters. + + If the ``route_prefix`` argument is supplied, any calls to + :meth:`pyramid.config.Configurator.add_route` within the ``callable`` + will have their pattern be prefixed by ``route_prefix``. This can + be used to help mount a set of routes at a different location than + the author intended while still keeping the same route names. This + parameter is new as of Pyramid 1.x.""" _context = self._ctx if _context is None: _context = self._ctx = self._make_context(self.autocommit) - for c in callables: - c = self.maybe_dotted(c) - module = inspect.getmodule(c) - if module is c: - c = getattr(module, 'includeme') - spec = module.__name__ + ':' + c.__name__ - sourcefile = inspect.getsourcefile(c) - if _context.processSpec(spec): - context = GroupingContextDecorator(_context) - context.basepath = os.path.dirname(sourcefile) - context.includepath = _context.includepath + (spec,) - context.package = package_of(module) - config = self.__class__.with_context(context) - c(config) - - def with_route_prefix(self, route_prefix): - configurator = self.__class__(registry=self.registry, - package=self.package, - autocommit=self.autocommit) - configurator.route_prefix = route_prefix - return configurator - - def mount_subapplication(self, function, route_prefix): - """ mount subapplication with ``route_prefix`` - - - .. code-block:: python - :linenos: - - from pyramid.config import Configurator - - def main(global_config, **settings): - config = Configurator() - config.mount('myapp.myconfig.includeme', '/admin') - - ``myapp.myconfig`` is subapplication - and ``includeme`` is that mount point. - - .. code-block:: python - :linenos: - - from pyramid.config import Configurator - - def includeme(config): - config.add_route('projects', '/projects') - - - 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_route_prefix(route_prefix) - function(config) + if self.route_prefix: + old_prefix = self.route_prefix.rstrip('/') + '/' + else: + old_prefix = '' + + if route_prefix: + route_prefix = old_prefix + route_prefix.lstrip('/') + + c = self.maybe_dotted(callable) + module = inspect.getmodule(c) + if module is c: + c = getattr(module, 'includeme') + spec = module.__name__ + ':' + c.__name__ + sourcefile = inspect.getsourcefile(c) + if _context.processSpec(spec): + context = GroupingContextDecorator(_context) + context.basepath = os.path.dirname(sourcefile) + context.includepath = _context.includepath + (spec,) + context.package = package_of(module) + context.route_prefix = route_prefix + config = self.__class__.with_context(context) + c(config) def add_directive(self, name, directive, action_wrap=True): """ @@ -689,7 +674,8 @@ class Configurator(object): :meth:`pyramid.config.Configurator.include` to obtain a configurator with 'the right' context. Returns a new Configurator instance.""" configurator = cls(registry=context.registry, package=context.package, - autocommit=context.autocommit) + autocommit=context.autocommit, + route_prefix=context.route_prefix) configurator._ctx = context return configurator @@ -1972,7 +1958,7 @@ class Configurator(object): if pattern is None: raise ConfigurationError('"pattern" argument may not be None') - if hasattr(self, "route_prefix"): + if self.route_prefix: pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') discriminator = ['route', name, xhr, request_method, path_info, diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 3423b9784..9a92cd052 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -251,6 +251,7 @@ class ConfiguratorTests(unittest.TestCase): config._ctx = DummyContext() config._ctx.registry = None config._ctx.autocommit = True + config._ctx.route_prefix = None newconfig = config.with_package(pyramid.tests) self.assertEqual(newconfig.package, pyramid.tests) @@ -735,6 +736,18 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(context_after.includepath, ()) self.assertTrue(context_after is context_before) + def test_include_with_route_prefix(self): + root_config = self._makeOne(autocommit=True) + def dummy_subapp(config): + self.assertEqual(config.route_prefix, 'root') + root_config.include(dummy_subapp, route_prefix='root') + + def test_include_with_nested_route_prefix(self): + root_config = self._makeOne(autocommit=True, route_prefix='root') + def dummy_subapp(config): + self.assertEqual(config.route_prefix, 'root/nested') + root_config.include(dummy_subapp, route_prefix='nested') + def test_with_context(self): config = self._makeOne() ctx = config._make_context() @@ -1997,7 +2010,6 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(route.name, 'name') def test_add_route_with_route_prefix(self): - from pyramid.interfaces import IRoutesMapper config = self._makeOne(autocommit=True) config.route_prefix = 'root' route = config.add_route('name', 'path') @@ -2006,20 +2018,6 @@ class ConfiguratorTests(unittest.TestCase): self._assertRoute(config, 'name', 'root/path') - def test_with_route_prefix(self): - root_config = self._makeOne(autocommit=True) - config = root_config.with_route_prefix('root') - self.assertEqual(config.registry, root_config.registry) - self.assertEqual(config.package, root_config.package) - self.assertEqual(config.route_prefix, 'root') - - def test_mount_subapplication(self): - root_config = self._makeOne(autocommit=True) - def dummy_subapp(config): - 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) factory = object() -- cgit v1.2.3 From 4880e19588da868f6075fc73d1f6ee1eb1e6ba6e Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Tue, 5 Jul 2011 20:54:00 -0500 Subject: garden --- pyramid/config.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index b39cb365e..bad12fc20 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -594,12 +594,12 @@ class Configurator(object): configuration conflict by registering something with the same configuration parameters. - If the ``route_prefix`` argument is supplied, any calls to + If the ``route_prefix`` is supplied, any calls to :meth:`pyramid.config.Configurator.add_route` within the ``callable`` - will have their pattern be prefixed by ``route_prefix``. This can + will have their pattern prefixed with ``route_prefix``. This can be used to help mount a set of routes at a different location than - the author intended while still keeping the same route names. This - parameter is new as of Pyramid 1.x.""" + the ``callable``-author intended while still maintaining the same + route names. This parameter is new as of Pyramid 1.x.""" _context = self._ctx if _context is None: -- cgit v1.2.3