httk.core.datastream.compression¶
Classes¶
A decompression codec for a single container format. |
Functions¶
|
Register (or replace) a codec under its |
Return the registered codec names, in registration order. |
|
|
Return the codec whose extension matches the trailing suffix of |
|
Split a trailing compression extension off |
|
Detect a codec from the leading magic bytes of |
|
Return a decompressed view of |
|
Raise |
|
Validate a |
Module Contents¶
- class httk.core.datastream.compression.CompressionCodec[source]¶
A decompression codec for a single container format.
A codec is an orthogonal layer below the datastream backends: it turns a compressed binary stream into an uncompressed binary stream, independently of where the compressed bytes come from (a filename, an open file, raw bytes, or a remote response).
- Parameters:
name – Canonical name used to select the codec explicitly.
extensions – Filename suffixes that identify the codec.
magics – Leading byte signatures used to detect the codec.
open_stream – Function that wraps compressed bytes for reading.
- name: str[source]¶
Canonical, lower-case codec name (e.g.
"gzip"); also how an explicit hint selects it.
- extensions: tuple[str, Ellipsis][source]¶
Recognized filename suffixes including the leading dot (e.g.
(".gz",)).
- magics: tuple[bytes, Ellipsis][source]¶
Leading magic-byte signatures; an empty tuple means the format cannot be sniffed.
- open_stream: collections.abc.Callable[[io.IOBase], io.IOBase][source]¶
Wrap a compressed binary stream and return a readable, decompressed binary stream.
- httk.core.datastream.compression.register_compression(codec)[source]¶
Register (or replace) a codec under its
name(case-insensitive).- Parameters:
codec (CompressionCodec) – Codec to add to the registry.
- httk.core.datastream.compression.known_compressions()[source]¶
Return the registered codec names, in registration order.
- httk.core.datastream.compression.codec_for_name(name)[source]¶
Return the codec whose extension matches the trailing suffix of
name, elseNone.- Parameters:
name (str) – Filename or URL path to inspect.
- Returns:
The matching codec, or
Nonewhen no suffix is recognized.- Return type:
CompressionCodec | None
- httk.core.datastream.compression.split_compression_suffix(name)[source]¶
Split a trailing compression extension off
name."data.json.gz"becomes("data.json", <gzip codec>); a name with no recognized compression extension is returned unchanged withNone.- Parameters:
name (str) – Filename or URL path to split.
- Returns:
The name without its recognized suffix and the matching codec, if any.
- Return type:
tuple[str, CompressionCodec | None]
- httk.core.datastream.compression.sniff_codec(stream)[source]¶
Detect a codec from the leading magic bytes of
streamwithout consuming data.A seekable stream is read and rewound; an unseekable stream is peeked (directly when it supports
peek, otherwise via a wrappingio.BufferedReader). The returned stream must be used in place of the input, since it may be the wrapper.- Parameters:
stream (io.IOBase) – Binary stream whose leading bytes should be inspected.
- Returns:
The stream to continue reading and the detected codec, if any.
- Return type:
tuple[io.IOBase, CompressionCodec | None]
- httk.core.datastream.compression.open_compressed(stream, *, compression='auto', name=None)[source]¶
Return a decompressed view of
streamaccording to thecompressionhint.Values are
"none"(passthrough),"extension"(decide fromnameonly),"detect"(always sniff magic bytes),"auto"(extension if recognized, else sniff), or a registered codec name (force that codec; an unknown name raisesValueError). When no codec applies the stream is returned unchanged.- Parameters:
- Returns:
A decompressed stream, or the original stream when no codec applies.
- Raises:
ValueError – If
compressionnames an unknown codec.- Return type:
- httk.core.datastream.compression.validate_compression(compression)[source]¶
Raise
ValueErrorunlesscompressionis a known mode or registered codec name.- Parameters:
compression (str) – Mode or codec name to validate.
- Raises:
ValueError – If
compressionis unknown.
- httk.core.datastream.compression.reject_text_native_compression(compression)[source]¶
Validate a
compressionhint for a text-native source (an open text stream or a string).Such sources carry no compressed bytes to decode, so only the no-op modes
"auto","extension", and"none"are accepted; a codec name or"detect"raisesValueError.None(no hint given) is accepted.- Parameters:
compression (str | None) – Optional compression hint supplied for a text-native source.
- Raises:
ValueError – If the hint requests native decompression or magic detection.