Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/pyramid/config/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def filtered_sources(self, resource_name):
if o is not None:
yield o

def get_spec(self, resource_name):
for source, path in self.filtered_sources(resource_name):
result = source.get_spec(path)
if result is not None:
return result

def get_filename(self, resource_name):
for source, path in self.filtered_sources(resource_name):
result = source.get_filename(path)
Expand Down Expand Up @@ -223,6 +229,11 @@ def __init__(self, package, prefix):
def get_path(self, resource_name):
return f'{self.prefix}{resource_name}'

def get_spec(self, resource_name):
path = self.get_path(resource_name)
if pkg_resources.resource_exists(self.pkg_name, path):
return f'{self.pkg_name}:{path}'

def get_filename(self, resource_name):
path = self.get_path(resource_name)
if pkg_resources.resource_exists(self.pkg_name, path):
Expand Down Expand Up @@ -270,6 +281,9 @@ def get_path(self, resource_name):
path = self.prefix
return path

def get_spec(self, resource_name):
return self.get_filename(resource_name)

def get_filename(self, resource_name):
path = self.get_path(resource_name)
if os.path.exists(path):
Expand Down
10 changes: 2 additions & 8 deletions src/pyramid/config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2338,14 +2338,7 @@ def _bust_asset_path(self, request, spec, subpath, kw):
pathspec = f'{pkg_name}:{pkg_subpath}{subpath}'
overrides = registry.queryUtility(IPackageOverrides, name=pkg_name)
if overrides is not None:
resource_name = posixpath.join(pkg_subpath, subpath)
sources = overrides.filtered_sources(resource_name)
for source, filtered_path in sources:
rawspec = source.get_path(filtered_path)
if hasattr(source, 'pkg_name'):
rawspec = f'{source.pkg_name}:{rawspec}'
break

rawspec = overrides.get_spec(f'{pkg_subpath}{subpath}')
else:
pathspec = pkg_subpath + subpath

Expand All @@ -2354,6 +2347,7 @@ def _bust_asset_path(self, request, spec, subpath, kw):

kw['pathspec'] = pathspec
kw['rawspec'] = rawspec
print(kw)
for spec_, cachebust, explicit in reversed(self.cache_busters):
if (explicit and rawspec.startswith(spec_)) or (
not explicit and pathspec.startswith(spec_)
Expand Down
8 changes: 8 additions & 0 deletions src/pyramid/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,14 @@ def get_filename(fullname):
class IPackageOverrides(IPEP302Loader):
"""Utility for pkg_resources overrides"""

def get_spec(resource_name):
"""Return a specifier for the resource.

The specifier may be a dotted Python name or an absolute path on the
filesystem.

"""


# VH_ROOT_KEY is an interface; its imported from other packages (e.g.
# traversalwrapper)
Expand Down
Empty file.
Binary file added tests/test_config/pkgs/cachebust/override/foo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/test_config/pkgs/cachebust/path/foo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions tests/test_config/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,28 @@ def test_filtered_sources(self):
po.overrides = overrides
self.assertEqual(list(po.filtered_sources('whatever')), ['foo'])

def test_get_spec(self):
source = DummyAssetSource(spec='test:foo.pt')
overrides = [DummyOverride(None), DummyOverride((source, ''))]
package = DummyPackage('package')
po = self._makeOne(package)
po.overrides = overrides
result = po.get_spec('whatever')
self.assertEqual(result, 'test:foo.pt')
self.assertEqual(source.resource_name, '')

def test_get_spec_file_doesnt_exist(self):
source = DummyAssetSource(spec=None)
overrides = [
DummyOverride(None),
DummyOverride((source, 'wont_exist')),
]
package = DummyPackage('package')
po = self._makeOne(package)
po.overrides = overrides
self.assertEqual(po.get_spec('whatever'), None)
self.assertEqual(source.resource_name, 'wont_exist')

def test_get_filename(self):
source = DummyAssetSource(filename='foo.pt')
overrides = [DummyOverride(None), DummyOverride((source, ''))]
Expand Down Expand Up @@ -916,6 +938,24 @@ def _makeOne(self, prefix, package='tests.test_config'):
klass = self._getTargetClass()
return klass(package, prefix)

def test_get_spec(self):
source = self._makeOne('')
self.assertEqual(
source.get_spec('test_assets.py'),
'tests.test_config:test_assets.py',
)

def test_get_spec_with_prefix(self):
source = self._makeOne('test_assets.py')
self.assertEqual(
source.get_spec(''),
'tests.test_config:test_assets.py',
)

def test_get_spec_file_doesnt_exist(self):
source = self._makeOne('')
self.assertIsNone(source.get_spec('wont_exist'))


class TestFSAssetSource(AssetSourceIntegrationTests, unittest.TestCase):
def _getTargetClass(self):
Expand All @@ -927,6 +967,23 @@ def _makeOne(self, prefix, base_prefix=here):
klass = self._getTargetClass()
return klass(os.path.join(base_prefix, prefix))

def test_get_spec(self):
source = self._makeOne('')
self.assertEqual(
source.get_spec('test_assets.py'),
os.path.join(here, 'test_assets.py'),
)

def test_get_spec_with_prefix(self):
source = self._makeOne('test_assets.py')
self.assertEqual(
source.get_spec(''), os.path.join(here, 'test_assets.py')
)

def test_get_spec_file_doesnt_exist(self):
source = self._makeOne('')
self.assertEqual(source.get_spec('wont_exist'), None)


class TestDirectoryOverride(unittest.TestCase):
def _getTargetClass(self):
Expand Down Expand Up @@ -1018,6 +1075,10 @@ class DummyAssetSource:
def __init__(self, **kw):
self.kw = kw

def get_spec(self, resource_name):
self.resource_name = resource_name
return self.kw['spec']

def get_filename(self, resource_name):
self.resource_name = resource_name
return self.kw['filename']
Expand Down
25 changes: 19 additions & 6 deletions tests/test_config/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4065,9 +4065,12 @@ def test_generate_url_cachebust_with_overrides(self):
config = testing.setUp()
try:
request = testing.DummyRequest()
config.add_static_view('static', 'path')
config.add_static_view(
'static', 'tests.test_config.pkgs.cachebust:path/'
)
config.override_asset(
'tests.test_config:path/', 'tests.test_config:other_path/'
'tests.test_config.pkgs.cachebust:path/',
'tests.test_config.pkgs.cachebust:override/',
)

def cb(val):
Expand All @@ -4077,11 +4080,21 @@ def cb_(request, subpath, kw):

return cb_

config.add_cache_buster('path', cb('foo'))
result = request.static_url('path/foo.png')
config.add_cache_buster(
'tests.test_config.pkgs.cachebust:path/', cb('foo')
)
result = request.static_url(
'tests.test_config.pkgs.cachebust:path/foo.png'
)
self.assertEqual(result, 'http://example.com/static/foo.png?x=foo')
config.add_cache_buster('other_path', cb('bar'), explicit=True)
result = request.static_url('path/foo.png')
config.add_cache_buster(
'tests.test_config.pkgs.cachebust:override/',
cb('bar'),
explicit=True,
)
result = request.static_url(
'tests.test_config.pkgs.cachebust:path/foo.png'
)
self.assertEqual(result, 'http://example.com/static/foo.png?x=bar')
finally:
testing.tearDown()
Expand Down
Loading