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).
- 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: CompressionCodec) None[source]¶
Register (or replace) a codec under its
name(case-insensitive).
- httk.core.datastream.compression.known_compressions() list[str][source]¶
Return the registered codec names, in registration order.
- httk.core.datastream.compression.codec_for_name(name: str) CompressionCodec | None[source]¶
Return the codec whose extension matches the trailing suffix of
name, elseNone.
- httk.core.datastream.compression.split_compression_suffix(name: str) tuple[str, CompressionCodec | None][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.
- httk.core.datastream.compression.sniff_codec(stream: io.IOBase) tuple[io.IOBase, CompressionCodec | None][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.
- httk.core.datastream.compression.open_compressed(stream: io.IOBase, *, compression: str = 'auto', name: str | None = None) io.IOBase[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.
- httk.core.datastream.compression.validate_compression(compression: str) None[source]¶
Raise
ValueErrorunlesscompressionis a known mode or registered codec name.
- httk.core.datastream.compression.reject_text_native_compression(compression: str | None) None[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.