Chris McDonough
2011-08-20 b8c79771a186f1032635fc640b3cecc2c9e281ad
- 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.
3 files modified
84 ■■■■ changed files
CHANGES.txt 8 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_url.py 42 ●●●●● patch | view | raw | blame | history
pyramid/url.py 34 ●●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -81,6 +81,14 @@
- 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
--------
pyramid/tests/test_url.py
@@ -338,8 +338,16 @@
        self.assertRaises(ValueError, request.static_url, 'static/foo.css')
    def test_static_url_abspath(self):
        from pyramid.interfaces import IStaticURLInfo
        request = self._makeOne()
        self.assertRaises(ValueError, request.static_url, '/static/foo.css')
        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()
    def test_static_url_found_rel(self):
        from pyramid.interfaces import IStaticURLInfo
@@ -373,9 +381,34 @@
        self.assertEqual(info.args,
                         ('pyramid.tests:static/foo.css', request, {}) )
    def test_static_path_abspath(self):
    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()
        self.assertRaises(ValueError, request.static_path, '/static/foo.css')
        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()
        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 @@
        self.args = path, request, kw
        return self.result
    
def makeabs(*elements):
    import os
    return os.path.sep + os.path.sep.join(elements)
pyramid/url.py
@@ -346,16 +346,13 @@
        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 @@
           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)