From 9b24401605e0470388a65c80a0964cba2bf64caf Mon Sep 17 00:00:00 2001 From: Marcel Hellkamp Date: Tue, 29 May 2012 17:45:11 +0200 Subject: api: Bottle.mount() not adds 'mountpoint' meta-data to the created routes to allow introspection of the wrapped sub-application. Thanks to David Buxton for the feature request and patch. --- bottle.py | 24 ++++++++++++++---------- test/test_mount.py | 9 +++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/bottle.py b/bottle.py index 77a9210..6015d8f 100644 --- a/bottle.py +++ b/bottle.py @@ -578,14 +578,11 @@ class Bottle(object): prefix, app = app, prefix depr('Parameter order of Bottle.mount() changed.') # 0.10 - parts = [p for p in prefix.split('/') if p] - if not parts: raise ValueError('Empty path prefix.') - path_depth = len(parts) - options.setdefault('skip', True) - options.setdefault('method', 'ANY') + segments = [p for p in prefix.split('/') if p] + if not segments: raise ValueError('Empty path prefix.') + path_depth = len(segments) - @self.route('/%s/:#.*#' % '/'.join(parts), **options) - def mountpoint(): + def mountpoint_wrapper(): try: request.path_shift(path_depth) rs = BaseResponse([], 200) @@ -593,13 +590,20 @@ class Bottle(object): rs.status = status for name, value in header: rs.add_header(name, value) return rs.body.append - rs.body = itertools.chain(rs.body, app(request.environ, start_response)) - return HTTPResponse(rs.body, rs.status_code, rs.headers) + body = app(request.environ, start_response) + body = itertools.chain(rs.body, body) + return HTTPResponse(body, rs.status_code, rs.headers) finally: request.path_shift(-path_depth) + options.setdefault('skip', True) + options.setdefault('method', 'ANY') + options.setdefault('mountpoint', {'prefix': prefix, 'target': app}) + options['callback'] = mountpoint_wrapper + + self.route('/%s/<:re:.*>' % '/'.join(segments), **options) if not prefix.endswith('/'): - self.route('/' + '/'.join(parts), callback=mountpoint, **options) + self.route('/' + '/'.join(segments), **options) def merge(self, routes): ''' Merge the routes of another :class:`Bottle` application or a list of diff --git a/test/test_mount.py b/test/test_mount.py index fc24638..b67d5d8 100644 --- a/test/test_mount.py +++ b/test/test_mount.py @@ -20,6 +20,15 @@ class TestAppMounting(ServerTestBase): self.assertStatus(200, '/test/test/bar') self.assertBody('bar', '/test/test/bar') + def test_mount_meta(self): + self.app.mount('/test/', self.subapp) + self.assertEqual( + self.app.routes[0].config.mountpoint['prefix'], + '/test/') + self.assertEqual( + self.app.routes[0].config.mountpoint['target'], + self.subapp) + def test_no_slash_prefix(self): self.app.mount('/test', self.subapp) self.assertStatus(404, '/') -- cgit v1.2.1