From f4931e362642005f1148558e911f81c97450eba4 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Thu, 10 Feb 2022 00:13:09 -0800 Subject: [PATCH] minor syntax or performance improvements --- whitenoise/base.py | 9 ++++----- whitenoise/middleware.py | 8 +++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/whitenoise/base.py b/whitenoise/base.py index e7bcf2aa..993cc691 100644 --- a/whitenoise/base.py +++ b/whitenoise/base.py @@ -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 diff --git a/whitenoise/middleware.py b/whitenoise/middleware.py index 248339dc..55972d88 100644 --- a/whitenoise/middleware.py +++ b/whitenoise/middleware.py @@ -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 @@ -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): """