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 Feb 2, 2022
1 parent af2a5a2 commit 720d4ef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
40 changes: 34 additions & 6 deletions whitenoise/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import gzip
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 @@ -40,13 +45,15 @@ 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,9 +67,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 @@ -128,8 +138,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 @@ -167,8 +187,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 @@ -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

0 comments on commit 720d4ef

Please sign in to comment.