From b8c79771a186f1032635fc640b3cecc2c9e281ad Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 20 Aug 2011 13:05:13 -0400 Subject: - The ``pyramid.request.Request.static_url`` API (and its brethren ``pyramid.request.Request.static_path``, ``pyramid.url.static_url``, and ``pyramid.url.static_path``) now accept an asbolute filename as a "path" argument. This will generate a URL to an asset as long as the filename is in a directory which was previously registered as a static view. Previously, trying to generate a URL to an asset using an absolute file path would raise a ValueError. --- CHANGES.txt | 8 ++++++++ pyramid/tests/test_url.py | 40 ++++++++++++++++++++++++++++++++++++++-- pyramid/url.py | 34 ++++++++++++++-------------------- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9b81ad526..2940a8228 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -81,6 +81,14 @@ Features - New functions in ``pyramid.url``: ``current_route_path`` and ``static_path``. +- The ``pyramid.request.Request.static_url`` API (and its brethren + ``pyramid.request.Request.static_path``, ``pyramid.url.static_url``, and + ``pyramid.url.static_path``) now accept an asbolute filename as a "path" + argument. This will generate a URL to an asset as long as the filename is + in a directory which was previously registered as a static view. + Previously, trying to generate a URL to an asset using an absolute file + path would raise a ValueError. + Internal -------- diff --git a/pyramid/tests/test_url.py b/pyramid/tests/test_url.py index 6fb67ed5a..4a822725c 100644 --- a/pyramid/tests/test_url.py +++ b/pyramid/tests/test_url.py @@ -338,8 +338,16 @@ class TestURLMethodsMixin(unittest.TestCase): self.assertRaises(ValueError, request.static_url, 'static/foo.css') def test_static_url_abspath(self): + from pyramid.interfaces import IStaticURLInfo + request = self._makeOne() + info = DummyStaticURLInfo('abc') + registry = request.registry + registry.registerUtility(info, IStaticURLInfo) + abspath = makeabs('static', 'foo.css') + result = request.static_url(abspath) + self.assertEqual(result, 'abc') + self.assertEqual(info.args, ('/static/foo.css', request, {})) request = self._makeOne() - self.assertRaises(ValueError, request.static_url, '/static/foo.css') def test_static_url_found_rel(self): from pyramid.interfaces import IStaticURLInfo @@ -373,9 +381,34 @@ class TestURLMethodsMixin(unittest.TestCase): self.assertEqual(info.args, ('pyramid.tests:static/foo.css', request, {}) ) + def test_static_url_abspath_integration_with_staticurlinfo(self): + import os + from pyramid.interfaces import IStaticURLInfo + from pyramid.static import StaticURLInfo + info = StaticURLInfo(self.config) + here = os.path.abspath(os.path.dirname(__file__)) + info.add('absstatic', here) + request = self._makeOne() + registry = request.registry + registry.registerUtility(info, IStaticURLInfo) + abspath = os.path.join(here, 'test_url.py') + result = request.static_url(abspath) + self.assertEqual(result, + 'http://example.com:5432/absstatic/test_url.py') + def test_static_path_abspath(self): + from pyramid.interfaces import IStaticURLInfo request = self._makeOne() - self.assertRaises(ValueError, request.static_path, '/static/foo.css') + request.script_name = '/foo' + info = DummyStaticURLInfo('abc') + registry = request.registry + registry.registerUtility(info, IStaticURLInfo) + abspath = makeabs('static', 'foo.css') + result = request.static_path(abspath) + self.assertEqual(result, 'abc') + self.assertEqual(info.args, ('/static/foo.css', request, + {'_app_url':'/foo'}) + ) def test_static_path_found_rel(self): from pyramid.interfaces import IStaticURLInfo @@ -592,3 +625,6 @@ class DummyStaticURLInfo: self.args = path, request, kw return self.result +def makeabs(*elements): + import os + return os.path.sep + os.path.sep.join(elements) diff --git a/pyramid/url.py b/pyramid/url.py index 1f353a4b9..58557c1e4 100644 --- a/pyramid/url.py +++ b/pyramid/url.py @@ -346,16 +346,13 @@ class URLMethodsMixin(object): definition cannot be found which matches the path specification. """ - if os.path.isabs(path): - raise ValueError('Absolute paths cannot be used to generate static ' - 'urls (use a package-relative path or an asset ' - 'specification).') - if not ':' in path: - # if it's not a package:relative/name and it's not an - # /absolute/path it's a relative/path; this means its relative - # to the package in which the caller's module is defined. - package = caller_package() - path = '%s:%s' % (package.__name__, path) + if not os.path.isabs(path): + if not ':' in path: + # if it's not a package:relative/name and it's not an + # /absolute/path it's a relative/path; this means its relative + # to the package in which the caller's module is defined. + package = caller_package() + path = '%s:%s' % (package.__name__, path) try: reg = self.registry @@ -394,16 +391,13 @@ class URLMethodsMixin(object): way. As a result, any ``_app_url`` passed within the ``**kw`` values to ``static_path`` will be ignored. """ - if os.path.isabs(path): - raise ValueError('Absolute paths cannot be used to generate static ' - 'urls (use a package-relative path or an asset ' - 'specification).') - if not ':' in path: - # if it's not a package:relative/name and it's not an - # /absolute/path it's a relative/path; this means its relative - # to the package in which the caller's module is defined. - package = caller_package() - path = '%s:%s' % (package.__name__, path) + if not os.path.isabs(path): + if not ':' in path: + # if it's not a package:relative/name and it's not an + # /absolute/path it's a relative/path; this means its relative + # to the package in which the caller's module is defined. + package = caller_package() + path = '%s:%s' % (package.__name__, path) kw['_app_url'] = self.script_name return self.static_url(path, **kw) -- cgit v1.2.3