API Documentation: gzip_static

Functions to compress a website’s static files.

class gzip_static.GzipStaticResult(created: int, updated: int, skipped: int, deleted: int)

A class containing the results for the gzip_static function.

property created

Alias for field number 0

property deleted

Alias for field number 3

property skipped

Alias for field number 2

property updated

Alias for field number 1

gzip_static.compress_idempotent(filepath: Union[str, os.PathLike], compresslevel=9, hash_algorithm=<built-in function openssl_sha1>, force: bool = False) int

Only compress the file if no companion .gz is present that contains the correct contents.

This function ensures the mode, atime and mtime of the gzip file are inherited from the file to be compressed.

Parameters
  • filepath – The path to the file.

  • compresslevel – The compression level. Use 11 for zopfli.

  • hash_algorithm – The hash_algorithm to check the contents with.

  • force – Always create a new ‘.gz’ file to overwrite the old one.

Returns

An integer that stands for the action taken. Matches with the COMPRESSED, RECOMPRESSED and SKIPPED constants in this module.

gzip_static.compress_path(filepath: Union[str, os.PathLike], compresslevel: int = 9, block_size: int = 32768) None

Compress a file’s contents and write them to a ‘.gz’ file.

Similar to gzip -k <filepath>

Parameters
  • filepath – The path to the file

  • compresslevel – The gzip compression level to use. Use 11 for zopfli compression.

  • block_size – The size of the chunks read from the file at once.

gzip_static.find_orphaned_files(dir: Union[str, os.PathLike], extensions: Container[str] = frozenset({'.css', '.htm', '.html', '.js', '.json', '.rss', '.svg', '.txt', '.xml', '.xsl'})) Generator[str, None, None]

Scan a directory recursively for ‘.gz’ files that do not have a parent file with an extension in extensions.

For example find_orphaned_files(my_dir, set(".html")) will find index.html.gz if index.html is not present. It will not find myhostedarchive.tar.gz as .tar is not in the set of extensions.

Parameters
  • dir – The directory to scan.

  • extensions – Extensions of parents file to include.

Returns

A generator of filepaths of orphaned ‘.gz’ files.

gzip_static.find_static_files(dir: Union[str, os.PathLike], extensions: Container[str] = frozenset({'.css', '.htm', '.html', '.js', '.json', '.rss', '.svg', '.txt', '.xml', '.xsl'})) Generator[str, None, None]

Scan a directory recursively for files that have an extension in the set of extensions.

Parameters
  • dir – The directory to scan.

  • extensions – A set of extensions to scan for.

Returns

A generator of filepaths that match the extensions.

gzip_static.get_extension(filename: str)

The filename’s extension, if any.

This includes the leading period. For example: ‘.txt’

gzip_static.gzip_static(dir: Union[str, os.PathLike], extensions: Container[str] = frozenset({'.css', '.htm', '.html', '.js', '.json', '.rss', '.svg', '.txt', '.xml', '.xsl'}), compresslevel: int = 9, hash_algorithm=<built-in function openssl_sha1>, force: bool = False, remove_orphans: bool = False) gzip_static.GzipStaticResult

Gzip all static files in a directory and its subdirectories in an idempotent manner.

Parameters
  • dir – The directory to recurse through.

  • extensions – Extensions which are static files.

  • compresslevel – The compression level that is used when compressing.

  • hash_algorithm – The hash algorithm is used when checking file contents.

  • force – Recompress all files regardless if content has changed or not.

  • remove_orphans – Remove ‘.gz’ files where the parent static file is no longer present.

Returns

A tuple with 4 entries. The number of compressed, recompressed, skipped and deleted gzip files.

gzip_static.hash_file_contents(filepath: Union[str, os.PathLike], hash_algorithm=<built-in function openssl_sha1>, block_size: int = 32768) bytes

Read contents from a file and return the hash.

Parameters
  • filepath – The path to the file. Paths ending in ‘.gz’ will be automatically decompressed.

  • hash_algorithm – The hash algorithm to use. Must be hashlib-compatible.

  • block_size – The size of the chunks read from the file at once.

Returns

A digest of the hash.

gzip_static.read_extensions_file(filepath: Union[str, os.PathLike]) Set[str]

Read a file where there is an extension on each line

Parameters

filepath – The extensions file

Returns

a set of extensions.