summaryrefslogtreecommitdiff
path: root/linuxnamespaces/tarutils.py
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2025-06-26 18:43:02 +0200
committerHelmut Grohne <helmut@subdivi.de>2025-06-26 18:43:02 +0200
commit7dbe52bb5cf303f4a51b58c9e2865d3ece3394bf (patch)
tree4b9f81cb3214382f37ec05ca3cedc594187e0142 /linuxnamespaces/tarutils.py
parent8d8431aa57d3710b0e13bd99bbf786e830153862 (diff)
downloadpython-linuxnamespaces-7dbe52bb5cf303f4a51b58c9e2865d3ece3394bf.tar.gz
Allow customizing ZstdTarFile compression concurrency
Also turn the compressor-specific arguments into keyword-only arguments.
Diffstat (limited to 'linuxnamespaces/tarutils.py')
-rw-r--r--linuxnamespaces/tarutils.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/linuxnamespaces/tarutils.py b/linuxnamespaces/tarutils.py
index 3a152d6..5ad60cd 100644
--- a/linuxnamespaces/tarutils.py
+++ b/linuxnamespaces/tarutils.py
@@ -31,11 +31,15 @@ class ZstdTarFile(tarfile.TarFile):
name: str,
mode: typing.Literal["r", "w", "x"] = "r",
fileobj: typing.BinaryIO | None = None,
+ *,
compresslevel: int | None = None,
+ threads: int | None = None,
**kwargs: typing.Any,
) -> tarfile.TarFile:
"""Open a zstd compressed tar archive with the given name for readin or
- writing. Appending is not supported.
+ writing. Appending is not supported. The class allows customizing the
+ compression level and the compression concurrency (default parallel)
+ while decompression ignores those arguments.
"""
if mode not in ("r", "w", "x"):
raise ValueError("mode must be 'r', 'w' or 'x'")
@@ -49,17 +53,19 @@ class ZstdTarFile(tarfile.TarFile):
if mode == "r":
zfobj = zstandard.open(openobj, "rb")
else:
+ if threads is None:
+ threads = -1
if compresslevel is not None:
if compresslevel > 22:
raise ValueError(
"invalid compression level {compresslevel}"
)
cctx = zstandard.ZstdCompressor(
- write_checksum=True, threads=-1, level=compresslevel
+ write_checksum=True, threads=threads, level=compresslevel
)
else:
cctx = zstandard.ZstdCompressor(
- write_checksum=True, threads=-1
+ write_checksum=True, threads=threads
)
zfobj = zstandard.open(openobj, mode + "b", cctx=cctx)
try: