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 22, 2024
1 parent fccb2ca commit 0fec8c0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ arguments upper-cased with a 'WHITENOISE\_' prefix.
just skip over them.


.. attribute:: WHITENOISE_COMPRESSOR_CLASS

:default: ``'whitenoise.compress.Compressor'``

String with custom Compressor class dotted path.


.. attribute:: WHITENOISE_ADD_HEADERS_FUNCTION

:default: ``None``
Expand Down
32 changes: 29 additions & 3 deletions src/whitenoise/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -134,6 +138,20 @@ def write_data(self, path, data, suffix, stat_result):
return filename


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


def main(argv=None):
parser = argparse.ArgumentParser(
description="Search for all files inside <root> *not* matching "
Expand All @@ -150,26 +168,34 @@ def main(argv=None):
action="store_false",
dest="use_gzip",
)
parser.add_argument(
"--compressor-class",
nargs="*",
help="Path to compressor class",
dest="compressor_class",
default="whitenoise.compress.Compressor",
)
parser.add_argument(
"--no-brotli",
help="Don't produce brotli '.br' files",
action="store_false",
dest="use_brotli",
)
parser.add_argument("root", help="Path root from which to search for files")
default_exclude = ", ".join(Compressor.SKIP_COMPRESS_EXTENSIONS)
compressor_class = import_string(parser.parse_args(argv).compressor_class)
default_exclude = ", ".join(compressor_class.SKIP_COMPRESS_EXTENSIONS)
parser.add_argument(
"extensions",
nargs="*",
help=(
"File extensions to exclude from compression "
+ f"(default: {default_exclude})"
),
default=Compressor.SKIP_COMPRESS_EXTENSIONS,
default=compressor_class.SKIP_COMPRESS_EXTENSIONS,
)
args = parser.parse_args(argv)

compressor = Compressor(
compressor = compressor_class(
extensions=args.extensions,
use_gzip=args.use_gzip,
use_brotli=args.use_brotli,
Expand Down
6 changes: 3 additions & 3 deletions src/whitenoise/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
from django.contrib.staticfiles.storage import StaticFilesStorage

from whitenoise.compress import Compressor
from .compress import get_compressor_class, Compressor

_PostProcessT = Iterator[Union[Tuple[str, str, bool], Tuple[str, None, RuntimeError]]]

Expand Down Expand Up @@ -41,7 +41,7 @@ def post_process(
yield path, compressed_name, True

def create_compressor(self, **kwargs: Any) -> Compressor:
return Compressor(**kwargs)
return get_compressor_class()(**kwargs)


class MissingFileError(ValueError):
Expand Down Expand Up @@ -126,7 +126,7 @@ def delete_files(self, files_to_delete):
raise

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

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

0 comments on commit 0fec8c0

Please sign in to comment.