我尝试使用此类Archive。但是,我从“ return Archive('r',strm.read())”中得到了“ AttributeError:'generator'对象没有属性'read'“。
class Archive(object):
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
info.mtime = time.time()
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.
"""
f = self.tar.extractfile(filename)
if f:
return f.read().decode(encoding)
return None
def close(self):
self.tar.close()
return self
def __iter__(self):
return iter(self.tar.getmembers())
@property
def buffer(self):
return self.file.getvalue()
主要问题:
def get_archive(self, path):
"""Get a file or directory from the container and make it into
an `Archive` object."""
if self.working_dir is not None and not posixpath.isabs(path):
path = posixpath.join(self.working_dir, path)
strm, stat = self.client.get_archive(
self.container_id, path)
return Archive('r', strm.read())
有人可以告诉我如何将原始tar数据流的strm读取或转换为适合类Archive的字节状对象吗?或其他任何想法?
答案 0 :(得分:0)
当我在新的Python虚拟环境中运行并重新安装所有必需的软件包时,此问题已解决。但是,我仍然不明白它是如何工作的。