Skip to content

Commit

Permalink
make possible to set compressor class from settings (or command line …
Browse files Browse the repository at this point in the history
…argument)
  • Loading branch information
PetrDlouhy committed Dec 20, 2021
1 parent d8a58bd commit 299abb0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
39 changes: 33 additions & 6 deletions whitenoise/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import os
import re

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

from io import BytesIO

try:
Expand Down Expand Up @@ -39,13 +43,15 @@ class Compressor(object):
)

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 @@ -59,9 +65,12 @@ def get_extension_re(extensions):
)

def should_compress(self, filename):
exclude_regexp = getattr(settings, "WHITENOISE_SKIP_REGEXP", [])
if self.skip_regexp is False:
skip_regexp = getattr(settings, "WHITENOISE_SKIP_REGEXP", [])
else:
skip_regexp = self.skip_regexp
skip = False
for r in exclude_regexp:
for r in skip_regexp:
if re.match(r, filename):
skip = True
break
Expand Down Expand Up @@ -131,8 +140,18 @@ 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 @@ -170,8 +189,16 @@ 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="*",
Expand Down
6 changes: 3 additions & 3 deletions whitenoise/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
StaticFilesStorage,
)

from .compress import Compressor
from .compress import compressor_class


class CompressedStaticFilesMixin(object):
Expand All @@ -36,7 +36,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 @@ -189,7 +189,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

0 comments on commit 299abb0

Please sign in to comment.