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

Add compress skip patterns from settings #297

Open
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,25 @@ arguments upper-cased with a 'WHITENOISE\_' prefix.
confident won't benefit from compression, it speeds up the process if we
just skip over them.

.. attribute:: WHITENOISE_SKIP_REGEX

:default: ``()``

List/tuple of regular expressions filename patterns to be excluded from compression.

For example: ::

WHITENOISE_SKIP_REGEXP = (
r".*/src/",
r".*\.scss$",
r".*\.less$",
)

.. attribute:: WHITENOISE_COMPRESSOR_CLASS
:default: ``"whitenoise.compress.Compressor"``

String with custom Compressor class dotted path.


.. attribute:: WHITENOISE_ADD_HEADERS_FUNCTION

Expand Down
58 changes: 53 additions & 5 deletions whitenoise/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import re
from io import BytesIO

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string

try:
import brotli

Expand Down Expand Up @@ -40,13 +44,20 @@ class Compressor:
)

def __init__(
self, extensions=None, use_gzip=True, use_brotli=True, log=print, quiet=False
self,
extensions=None,
use_gzip=True,
use_brotli=True,
skip_regexp=False,
log=print,
quiet=False,
):
if extensions is None:
extensions = self.SKIP_COMPRESS_EXTENSIONS
self.extension_re = self.get_extension_re(extensions)
self.use_gzip = use_gzip
self.use_brotli = use_brotli and brotli_installed
self.skip_regexp = skip_regexp
if not quiet:
self.log = log

Expand All @@ -60,7 +71,16 @@ def get_extension_re(extensions):
)

def should_compress(self, filename):
return not self.extension_re.search(filename)
if self.skip_regexp is False:
skip_regexp = getattr(settings, "WHITENOISE_SKIP_REGEXP", [])
else:
skip_regexp = self.skip_regexp
skip = False
for r in skip_regexp:
if re.match(r, filename):
skip = True
break
return not self.extension_re.search(filename) and not skip

def log(self, message):
pass
Expand Down Expand Up @@ -122,8 +142,22 @@ def write_data(self, path, data, suffix, stat_result):
return filename


try:
compressor_class = import_string(
getattr(
settings, "WHITENOISE_COMPRESSOR_CLASS", "whitenoise.compress.Compressor"
),
)
except ImproperlyConfigured:
compressor_class = Compressor


def main(root, **kwargs):
compressor = Compressor(**kwargs)
compressor_class_str = kwargs.pop(
"compressor_class", "whitenoise.compress.Compressor"
)
compressor_class = import_string(compressor_class_str)
compressor = compressor_class(**kwargs)
for dirpath, _dirs, files in os.walk(root):
for filename in files:
if compressor.should_compress(filename):
Expand Down Expand Up @@ -161,8 +195,22 @@ def main(root, **kwargs):
"extensions",
nargs="*",
help="File extensions to exclude from compression "
"(default: {})".format(", ".join(Compressor.SKIP_COMPRESS_EXTENSIONS)),
default=Compressor.SKIP_COMPRESS_EXTENSIONS,
"(default: {})".format(", ".join(compressor_class.SKIP_COMPRESS_EXTENSIONS)),
default=compressor_class.SKIP_COMPRESS_EXTENSIONS,
)
parser.add_argument(
"--compressor-class",
nargs="*",
help="Path to compressor class",
dest="compressor_class",
default="whitenoise.compress.Compressor",
)
parser.add_argument(
"--skip-regexp",
nargs="*",
help="File regexp patterns to exclude from compression",
dest="skip_regexp",
default="",
)
args = parser.parse_args()
main(**vars(args))
6 changes: 3 additions & 3 deletions whitenoise/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
StaticFilesStorage,
)

from .compress import Compressor
from .compress import compressor_class


class CompressedStaticFilesMixin:
Expand All @@ -38,7 +38,7 @@ def fallback_post_process(self, paths, dry_run=False, **options):
yield path, None, False

def create_compressor(self, **kwargs):
return Compressor(**kwargs)
return compressor_class(**kwargs)

def post_process_with_compression(self, files):
extensions = getattr(settings, "WHITENOISE_SKIP_COMPRESS_EXTENSIONS", None)
Expand Down Expand Up @@ -187,7 +187,7 @@ def delete_files(self, files_to_delete):
raise

def create_compressor(self, **kwargs):
return Compressor(**kwargs)
return compressor_class(**kwargs)

def compress_files(self, names):
extensions = getattr(settings, "WHITENOISE_SKIP_COMPRESS_EXTENSIONS", None)
Expand Down