Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use AssetResolver internally #3750

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
8 changes: 2 additions & 6 deletions src/pyramid/asset.py
Original file line number Diff line number Diff line change
@@ -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__'):
Expand Down Expand Up @@ -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()
48 changes: 17 additions & 31 deletions src/pyramid/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
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 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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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."""
Expand Down Expand Up @@ -381,8 +367,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

Expand Down
Loading