summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-08-10 20:20:17 -0400
committerChris McDonough <chrism@plope.com>2011-08-10 20:20:17 -0400
commit389c11dd193cb6eff707effb11bfe9589171c57c (patch)
treee5e57108a3d751d9e02cbf06ad5eca8902f4a100
parent3e3fcdf1376218a4fa6dcffec4f27a41c63d1675 (diff)
parent995466c6bc0da04f50d2db83af653362a0dadd6f (diff)
downloadpyramid-389c11dd193cb6eff707effb11bfe9589171c57c.tar.gz
pyramid-389c11dd193cb6eff707effb11bfe9589171c57c.tar.bz2
pyramid-389c11dd193cb6eff707effb11bfe9589171c57c.zip
Merge branch 'aodag-master'
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--pyramid/config.py71
-rw-r--r--pyramid/tests/test_config.py24
3 files changed, 76 insertions, 21 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index a165c0dc6..a368fb4d2 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -143,6 +143,8 @@ Contributors
- Christoph Zwerschke, 2011/06/07
+- Atsushi Odagiri, 2011/07/02
+
- Shane Hathaway, 2011/07/22
- Manuel Hermann, 2011/07/11
diff --git a/pyramid/config.py b/pyramid/config.py
index 60d406a62..3e91fb37a 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -274,7 +274,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
@@ -298,6 +302,7 @@ class Configurator(object):
default_view_mapper=None,
autocommit=False,
exceptionresponse_view=default_exceptionresponse_view,
+ route_prefix=None,
):
if package is None:
package = caller_package()
@@ -307,6 +312,7 @@ class Configurator(object):
self.package = name_resolver.package
self.registry = registry
self.autocommit = autocommit
+ self.route_prefix = route_prefix
if registry is None:
registry = Registry(self.package_name)
self.registry = registry
@@ -458,6 +464,7 @@ class Configurator(object):
registerCommonDirectives(context)
context.registry = self.registry
context.autocommit = autocommit
+ context.route_prefix = self.route_prefix
return context
# API
@@ -525,10 +532,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
@@ -537,7 +548,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
@@ -586,26 +597,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`` is supplied, any calls to
+ :meth:`pyramid.config.Configurator.add_route` within the ``callable``
+ 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 ``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:
_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)
+ 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):
"""
@@ -657,7 +683,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
@@ -1703,6 +1730,7 @@ class Configurator(object):
DeprecationWarning,
4)
+
@action_method
def add_route(self,
name,
@@ -2069,6 +2097,9 @@ class Configurator(object):
if pattern is None:
raise ConfigurationError('"pattern" argument may not be None')
+ if self.route_prefix:
+ pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/')
+
discriminator = ('route', name)
self.action(discriminator, None)
diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py
index 5d7caef03..5f5bbdc53 100644
--- a/pyramid/tests/test_config.py
+++ b/pyramid/tests/test_config.py
@@ -252,6 +252,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)
@@ -917,6 +918,18 @@ pyramid.tests.test_config.dummy_include2""",
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()
@@ -2257,6 +2270,15 @@ pyramid.tests.test_config.dummy_include2""",
self._assertRoute(config, 'name', 'path')
self.assertEqual(route.name, 'name')
+ def test_add_route_with_route_prefix(self):
+ 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')
+
+ self._assertRoute(config, 'name', 'root/path')
+
def test_add_route_discriminator(self):
config = self._makeOne()
route = config.add_route('name', 'path')
@@ -5735,7 +5757,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 = []