From 9026c4c42a1fc0f4ec376add6af2a7c05ec126e9 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Tue, 6 Feb 2024 22:16:11 -0700 Subject: [PATCH 1/2] use AssetResolver internally --- src/pyramid/asset.py | 8 ++------ src/pyramid/static.py | 8 ++++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/pyramid/asset.py b/src/pyramid/asset.py index bdf90ef5e..cba6e5b9a 100644 --- a/src/pyramid/asset.py +++ b/src/pyramid/asset.py @@ -1,7 +1,6 @@ import os -import pkg_resources -from pyramid.path import package_name, package_path +from pyramid.path import AssetResolver, package_name, package_path def resolve_asset_spec(spec, pname='__main__'): @@ -37,7 +36,4 @@ def asset_spec_from_abspath(abspath, package): def abspath_from_asset_spec(spec, pname='__main__'): if pname is None: return spec - pname, filename = resolve_asset_spec(spec, pname) - if pname is None: - return filename - return pkg_resources.resource_filename(pname, filename) + return AssetResolver(pname).resolve(spec).abspath() diff --git a/src/pyramid/static.py b/src/pyramid/static.py index 100d17676..403143aa8 100644 --- a/src/pyramid/static.py +++ b/src/pyramid/static.py @@ -5,9 +5,9 @@ from os.path import exists, getmtime, getsize, isdir, join, normcase, normpath from pkg_resources import resource_exists, resource_filename, resource_isdir -from pyramid.asset import abspath_from_asset_spec, resolve_asset_spec +from pyramid.asset import resolve_asset_spec from pyramid.httpexceptions import HTTPMovedPermanently, HTTPNotFound -from pyramid.path import caller_package +from pyramid.path import AssetResolver, caller_package from pyramid.response import FileResponse, _guess_type from pyramid.traversal import traversal_path_info @@ -381,8 +381,8 @@ class ManifestCacheBuster: def __init__(self, manifest_spec, reload=False): package_name = caller_package().__name__ - self.manifest_path = abspath_from_asset_spec( - manifest_spec, package_name + self.manifest_path = ( + AssetResolver(package_name).resolve(manifest_spec).abspath() ) self.reload = reload From 9bd1eb5fd38aa0bb41c0fb83b036a447e1eab3a7 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Tue, 6 Feb 2024 22:53:43 -0700 Subject: [PATCH 2/2] make static_view use asset resolver --- src/pyramid/static.py | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/src/pyramid/static.py b/src/pyramid/static.py index 403143aa8..adab41f14 100644 --- a/src/pyramid/static.py +++ b/src/pyramid/static.py @@ -2,8 +2,8 @@ import json import mimetypes import os -from os.path import exists, getmtime, getsize, isdir, join, normcase, normpath -from pkg_resources import resource_exists, resource_filename, resource_isdir +from os.path import exists, getmtime, getsize +import posixpath from pyramid.asset import resolve_asset_spec from pyramid.httpexceptions import HTTPMovedPermanently, HTTPNotFound @@ -94,8 +94,8 @@ def __init__( package_name, docroot = resolve_asset_spec(root_dir, package_name) self.use_subpath = use_subpath self.package_name = package_name + self.resolver = AssetResolver(self.package_name) self.docroot = docroot - self.norm_docroot = normcase(normpath(docroot)) self.index = index self.reload = reload self.content_encodings = _compile_content_encodings(content_encodings) @@ -137,24 +137,13 @@ def get_resource_name(self, request): raise HTTPNotFound('Out of bounds: %s' % request.url) # normalize asset spec or fs path into resource_path - if self.package_name: # package resource - resource_path = '{}/{}'.format(self.docroot.rstrip('/'), path) - if resource_isdir(self.package_name, resource_path): - if not request.path_url.endswith('/'): - raise self.add_slash_redirect(request) - resource_path = '{}/{}'.format( - resource_path.rstrip('/'), - self.index, - ) - - else: # filesystem file - # os.path.normpath converts / to \ on windows - resource_path = normcase(normpath(join(self.norm_docroot, path))) - if isdir(resource_path): - if not request.path_url.endswith('/'): - raise self.add_slash_redirect(request) - resource_path = join(resource_path, self.index) - + resource_path = posixpath.join(self.docroot, path) + asset = self.resolver.resolve(resource_path) + if asset.isdir(): + if not request.path_url.endswith('/'): + raise self.add_slash_redirect(request) + resource_path = posixpath.join(resource_path, self.index) + return resource_path return resource_path def find_resource_path(self, name): @@ -163,12 +152,9 @@ def find_resource_path(self, name): exist. """ - if self.package_name: - if resource_exists(self.package_name, name): - return resource_filename(self.package_name, name) - - elif exists(name): - return name + asset = self.resolver.resolve(name) + if asset.exists(): + return asset.abspath() def get_possible_files(self, resource_name): """Return a sorted list of ``(size, encoding, path)`` entries."""