Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from io import BytesIO
- import pickle
- import tarfile
- def _is_tar(tar_candidate):
- return type(tar_candidate) == tarfile.TarFile
- def _write_tar(tar, name, get_data):
- data = get_data()
- info = tarfile.TarInfo(name=name)
- info.size=len(data)
- tar.addfile(tarinfo=info, fileobj=BytesIO(data))
- def _with_tar(stream, action, mode='w'):
- if _is_tar(stream):
- tar = stream
- else:
- tar = tarfile.TarFile(stream, mode=mode)
- action_response = action(tar)
- if not _is_tar(stream):
- tar.close()
- return action_response
- def _read_tar(input_, name):
- if _is_tar(input_):
- tar = input_
- else:
- tar = tarfile.open(input_, mode='r')
- data = pickle.load(tar.extractfile(name))
- if not _is_tar(input_):
- tar.close()
- return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement