Skip to content

Commit

Permalink
Match storage.py to upstream
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Chainz <[email protected]>
  • Loading branch information
Archmonger and adamchainz committed Oct 28, 2024
1 parent 11dfd43 commit 4f30ab6
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/servestatic/storage.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

import concurrent.futures
import contextlib
import errno
import json
import os
import re
import textwrap
from collections.abc import Iterator
from collections.abc import Generator, Iterator
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any, Union

from django.conf import settings
Expand Down Expand Up @@ -37,20 +37,17 @@ def post_process(self, paths: dict[str, Any], dry_run: bool = False, **options:
extensions = getattr(settings, "SERVESTATIC_SKIP_COMPRESS_EXTENSIONS", None)
self.compressor = self.create_compressor(extensions=extensions, quiet=True)

to_compress = (path for path in paths if self.compressor.should_compress(path))
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = (executor.submit(self._compress_one, path) for path in to_compress)
for compressed_paths in concurrent.futures.as_completed(futures):
yield from compressed_paths.result()

def _compress_one(self, path: str) -> list[tuple[str, str, bool]]:
compressed: list[tuple[str, str, bool]] = []
full_path = self.path(path)
prefix_len = len(full_path) - len(path)
compressed.extend(
(path, compressed_path[prefix_len:], True) for compressed_path in self.compressor.compress(full_path)
)
return compressed
def _compress_path(path: str) -> Generator[tuple[str, str, bool]]:
full_path = self.path(path)
prefix_len = len(full_path) - len(path)
for compressed_path in self.compressor.compress(full_path):
compressed_name = compressed_path[prefix_len:]
yield (path, compressed_name, True)

with ThreadPoolExecutor() as executor:
futures = (executor.submit(_compress_path, path) for path in paths if self.compressor.should_compress(path))
for future in as_completed(futures):
yield from future.result()

def create_compressor(self, **kwargs: Any) -> Compressor: # noqa: PLR6301
return Compressor(**kwargs)
Expand Down Expand Up @@ -184,15 +181,21 @@ def delete_files(self, files_to_delete):
def create_compressor(self, **kwargs): # noqa: PLR6301
return Compressor(**kwargs)

def compress_files(self, names):
def compress_files(self, paths):
extensions = getattr(settings, "SERVESTATIC_SKIP_COMPRESS_EXTENSIONS", None)
self.compressor = self.create_compressor(extensions=extensions, quiet=True)

to_compress = (name for name in names if self.compressor.should_compress(name))
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = (executor.submit(self._compress_one, name) for name in to_compress)
for compressed_paths in concurrent.futures.as_completed(futures):
yield from compressed_paths.result()
def _compress_path(path: str) -> Generator[tuple[str, str]]:
full_path = self.path(path)
prefix_len = len(full_path) - len(path)
for compressed_path in self.compressor.compress(full_path):
compressed_name = compressed_path[prefix_len:]
yield (path, compressed_name)

with ThreadPoolExecutor() as executor:
futures = (executor.submit(_compress_path, path) for path in paths if self.compressor.should_compress(path))
for future in as_completed(futures):
yield from future.result()

def _compress_one(self, name: str) -> list[tuple[str, str]]:
compressed: list[tuple[str, str]] = []
Expand Down

0 comments on commit 4f30ab6

Please sign in to comment.