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 4, 2022
1 parent bc3dbe2 commit 1224d1b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
31 changes: 28 additions & 3 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 @@ -122,8 +126,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 +179,15 @@ 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",
)
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

0 comments on commit 1224d1b

Please sign in to comment.