Skip to content

Commit

Permalink
minor syntax or performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Feb 10, 2022
1 parent 2f0f685 commit f4931e3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
9 changes: 4 additions & 5 deletions whitenoise/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,16 @@ def add_files(self, root, prefix=None):
# to store the list of directories in reverse order so later ones
# match first when they're checked in "autorefresh" mode
self.directories.insert(0, (root, prefix))
elif os.path.isdir(root):
self.update_files_dictionary(root, prefix)
else:
if os.path.isdir(root):
self.update_files_dictionary(root, prefix)
else:
warnings.warn(f"No directory at: {root}")
warnings.warn(f"No directory at: {root}")

def update_files_dictionary(self, root, prefix):
# Build a mapping from paths to the results of `os.stat` calls
# so we only have to touch the filesystem once
stat_cache = dict(scantree(root))
for path in stat_cache.keys():
for path in stat_cache:
relative_path = path[len(root) :]
relative_url = relative_path.replace("\\", "/")
url = prefix + relative_url
Expand Down
8 changes: 3 additions & 5 deletions whitenoise/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ def configure_from_settings(self, settings):
self.use_finders = settings.DEBUG
self.static_prefix = urlparse(settings.STATIC_URL or "").path
script_prefix = get_script_prefix().rstrip("/")
if script_prefix:
if self.static_prefix.startswith(script_prefix):
self.static_prefix = self.static_prefix[len(script_prefix) :]
if script_prefix and self.static_prefix.startswith(script_prefix):
self.static_prefix = self.static_prefix[len(script_prefix) :]
if settings.DEBUG:
self.max_age = 0
# Allow settings to override default attributes
Expand Down Expand Up @@ -128,8 +127,7 @@ def candidate_paths_for_url(self, url):
if path:
yield path
paths = super().candidate_paths_for_url(url)
for path in paths:
yield path
yield from paths

def immutable_file_test(self, path, url):
"""
Expand Down

0 comments on commit f4931e3

Please sign in to comment.