|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +from io import BytesIO |
| 5 | +from os import PathLike |
| 6 | +from typing import IO |
| 7 | +from typing import Literal |
| 8 | +from typing import Protocol |
| 9 | +from typing import cast |
| 10 | + |
| 11 | + |
| 12 | +class IncrementalCompressor(Protocol): |
| 13 | + """ |
| 14 | + A compressor that accepts uncompressed data in chunks. |
| 15 | + """ |
| 16 | + |
| 17 | + def compress(self, data: bytes) -> bytes: ... |
| 18 | + |
| 19 | + def flush(self) -> bytes: ... |
| 20 | + |
| 21 | + |
| 22 | +if sys.version_info >= (3, 14): |
| 23 | + from compression import zstd as _zstd |
| 24 | + |
| 25 | + ZstdError = _zstd.ZstdError |
| 26 | + |
| 27 | + def is_frame_header(header: bytes) -> bool: |
| 28 | + """ |
| 29 | + Determine if `header` is the start of a zstandard frame. |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + header : bytes |
| 34 | + The first bytes of the data to check, at least 18 bytes to cover |
| 35 | + the largest frame header. |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + bool |
| 40 | +
|
| 41 | + """ |
| 42 | + try: |
| 43 | + _zstd.get_frame_info(header) |
| 44 | + except _zstd.ZstdError: |
| 45 | + return False |
| 46 | + return True |
| 47 | + |
| 48 | + def stream_reader(reader: IO[bytes]) -> IO[bytes]: |
| 49 | + """ |
| 50 | + Return a reader that decompresses zstandard data from `reader`. |
| 51 | +
|
| 52 | + The returned reader decompresses across all frames in `reader`. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + reader : IO[bytes] |
| 57 | + The compressed data to read from. |
| 58 | +
|
| 59 | + Returns |
| 60 | + ------- |
| 61 | + IO[bytes] |
| 62 | +
|
| 63 | + """ |
| 64 | + return cast(IO[bytes], _zstd.ZstdFile(reader, mode="rb")) |
| 65 | + |
| 66 | + def stream_writer( |
| 67 | + path: PathLike[str] | str, |
| 68 | + mode: Literal["w", "x"], |
| 69 | + ) -> IO[bytes]: |
| 70 | + """ |
| 71 | + Open the file at `path` for writing zstandard compressed data. |
| 72 | +
|
| 73 | + Closing the returned writer finishes the frame and closes the file. |
| 74 | + Frames are written with a content checksum. |
| 75 | +
|
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + path : PathLike[str] or str |
| 79 | + The file path to write to. |
| 80 | + mode : str |
| 81 | + The file write mode to use, either "x" or "w". |
| 82 | +
|
| 83 | + Returns |
| 84 | + ------- |
| 85 | + IO[bytes] |
| 86 | +
|
| 87 | + """ |
| 88 | + return cast( |
| 89 | + IO[bytes], |
| 90 | + _zstd.ZstdFile( |
| 91 | + path, |
| 92 | + mode="wb" if mode == "w" else "xb", |
| 93 | + options={_zstd.CompressionParameter.checksum_flag: 1}, |
| 94 | + ), |
| 95 | + ) |
| 96 | + |
| 97 | + def compress(data: bytes) -> bytes: |
| 98 | + """ |
| 99 | + Compress `data` into a single zstandard frame. |
| 100 | +
|
| 101 | + Parameters |
| 102 | + ---------- |
| 103 | + data : bytes |
| 104 | + The data to compress. |
| 105 | +
|
| 106 | + Returns |
| 107 | + ------- |
| 108 | + bytes |
| 109 | +
|
| 110 | + """ |
| 111 | + return _zstd.compress(data) |
| 112 | + |
| 113 | + def decompress(data: bytes) -> bytes: |
| 114 | + """ |
| 115 | + Decompress all zstandard frames in `data`. |
| 116 | +
|
| 117 | + Parameters |
| 118 | + ---------- |
| 119 | + data : bytes |
| 120 | + The compressed data. |
| 121 | +
|
| 122 | + Returns |
| 123 | + ------- |
| 124 | + bytes |
| 125 | +
|
| 126 | + """ |
| 127 | + return _zstd.decompress(data) |
| 128 | + |
| 129 | + def incremental_compressor() -> IncrementalCompressor: |
| 130 | + """ |
| 131 | + Create a compressor that accepts uncompressed data in chunks. |
| 132 | +
|
| 133 | + Returns |
| 134 | + ------- |
| 135 | + IncrementalCompressor |
| 136 | +
|
| 137 | + """ |
| 138 | + return _zstd.ZstdCompressor() |
| 139 | + |
| 140 | +else: |
| 141 | + import zstandard |
| 142 | + |
| 143 | + ZstdError = zstandard.ZstdError |
| 144 | + |
| 145 | + def is_frame_header(header: bytes) -> bool: |
| 146 | + """ |
| 147 | + Determine if `header` is the start of a zstandard frame. |
| 148 | +
|
| 149 | + Parameters |
| 150 | + ---------- |
| 151 | + header : bytes |
| 152 | + The first bytes of the data to check, at least 18 bytes to cover |
| 153 | + the largest frame header. |
| 154 | +
|
| 155 | + Returns |
| 156 | + ------- |
| 157 | + bool |
| 158 | +
|
| 159 | + """ |
| 160 | + try: |
| 161 | + zstandard.get_frame_parameters(header) |
| 162 | + except zstandard.ZstdError: |
| 163 | + return False |
| 164 | + return True |
| 165 | + |
| 166 | + def stream_reader(reader: IO[bytes]) -> IO[bytes]: |
| 167 | + """ |
| 168 | + Return a reader that decompresses zstandard data from `reader`. |
| 169 | +
|
| 170 | + The returned reader decompresses across all frames in `reader`. |
| 171 | +
|
| 172 | + Parameters |
| 173 | + ---------- |
| 174 | + reader : IO[bytes] |
| 175 | + The compressed data to read from. |
| 176 | +
|
| 177 | + Returns |
| 178 | + ------- |
| 179 | + IO[bytes] |
| 180 | +
|
| 181 | + """ |
| 182 | + return zstandard.ZstdDecompressor().stream_reader( |
| 183 | + reader, |
| 184 | + read_across_frames=True, |
| 185 | + ) |
| 186 | + |
| 187 | + def stream_writer( |
| 188 | + path: PathLike[str] | str, |
| 189 | + mode: Literal["w", "x"], |
| 190 | + ) -> IO[bytes]: |
| 191 | + """ |
| 192 | + Open the file at `path` for writing zstandard compressed data. |
| 193 | +
|
| 194 | + Closing the returned writer finishes the frame and closes the file. |
| 195 | + Frames are written with a content checksum. |
| 196 | +
|
| 197 | + Parameters |
| 198 | + ---------- |
| 199 | + path : PathLike[str] or str |
| 200 | + The file path to write to. |
| 201 | + mode : str |
| 202 | + The file write mode to use, either "x" or "w". |
| 203 | +
|
| 204 | + Returns |
| 205 | + ------- |
| 206 | + IO[bytes] |
| 207 | +
|
| 208 | + """ |
| 209 | + return zstandard.ZstdCompressor(write_checksum=True).stream_writer( |
| 210 | + open(path, mode=f"{mode}b"), |
| 211 | + closefd=True, |
| 212 | + ) |
| 213 | + |
| 214 | + def compress(data: bytes) -> bytes: |
| 215 | + """ |
| 216 | + Compress `data` into a single zstandard frame. |
| 217 | +
|
| 218 | + Parameters |
| 219 | + ---------- |
| 220 | + data : bytes |
| 221 | + The data to compress. |
| 222 | +
|
| 223 | + Returns |
| 224 | + ------- |
| 225 | + bytes |
| 226 | +
|
| 227 | + """ |
| 228 | + return zstandard.compress(data) |
| 229 | + |
| 230 | + def decompress(data: bytes) -> bytes: |
| 231 | + """ |
| 232 | + Decompress all zstandard frames in `data`. |
| 233 | +
|
| 234 | + Parameters |
| 235 | + ---------- |
| 236 | + data : bytes |
| 237 | + The compressed data. |
| 238 | +
|
| 239 | + Returns |
| 240 | + ------- |
| 241 | + bytes |
| 242 | +
|
| 243 | + """ |
| 244 | + return stream_reader(BytesIO(data)).read() |
| 245 | + |
| 246 | + def incremental_compressor() -> IncrementalCompressor: |
| 247 | + """ |
| 248 | + Create a compressor that accepts uncompressed data in chunks. |
| 249 | +
|
| 250 | + Returns |
| 251 | + ------- |
| 252 | + IncrementalCompressor |
| 253 | +
|
| 254 | + """ |
| 255 | + return zstandard.ZstdCompressor().compressobj() |
0 commit comments