-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from contextlib import contextmanager | ||
from tarfile import open as tarfile_open | ||
|
||
try: | ||
import lz4.frame as lz4 | ||
except ImportError: | ||
lz4 = None | ||
|
||
|
||
class Lz4Tarfile: | ||
def __init__(self, **kwargs): | ||
self.lz4_kwargs = kwargs | ||
|
||
@contextmanager | ||
def read(self, path: str, mode: str): | ||
with lz4.LZ4FrameFile(path) as lz4d: | ||
archive = tarfile_open(mode=mode, fileobj=lz4d, **self.lz4_kwargs) | ||
try: | ||
yield archive | ||
finally: | ||
archive.close() | ||
|
||
@contextmanager | ||
def write(self, path: str, mode: str): | ||
with lz4.LZ4FrameFile(path, mode=mode[0]) as lz4c: | ||
archive = tarfile_open(mode=mode, fileobj=lz4c, **self.lz4_kwargs) | ||
try: | ||
yield archive | ||
finally: | ||
archive.close() | ||
|
||
|
||
if lz4 is None: | ||
Lz4Tarfile = None # noqa F811 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters