Skip to content

Instantly share code, notes, and snippets.

@jhidding
Created February 3, 2017 10:01
Show Gist options
  • Save jhidding/770e555fdb82e09ea347b42058805f42 to your computer and use it in GitHub Desktop.
Save jhidding/770e555fdb82e09ea347b42058805f42 to your computer and use it in GitHub Desktop.
An archive class, allowing easy access to files in a TAR
class Archive(object):
"""Easy interface to `tarfile`.
We use buffered `tar` files to communicate with Docker
containers. This class provides an easy way to create
`tar` buffers on the fly, or read from them.
Methods in this class can be chained JS style."""
def __init__(self, mode, data=None):
self.file = io.BytesIO(data)
self.tar = tarfile.open(mode=mode, fileobj=self.file)
def add_text_file(self, filename: str, text: str, encoding='utf-8'):
"""Add the contents of `text` to a new entry in the `tar`
file.
:return:
self
"""
b = text.encode(encoding)
f = io.BytesIO(b)
info = tarfile.TarInfo(filename)
info.size = len(b)
info.type = tarfile.REGTYPE
self.tar.addfile(info, fileobj=f)
return self
def get_text_file(self, filename: str, encoding='utf-8') -> str:
"""Read the contents of a file in the archive.
:return:
contents of file in string.
"""
return self.tar.extractfile(filename).read().decode(encoding)
def close(self):
self.tar.close()
return self
@property
def buffer(self):
return self.file.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment