1

我尝试使用此类存档。但是,我从“return Archive('r',strm.read())”中得到了“AttributeError: 'generator' object has no attribute '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 转换为适合归档类的字节类对象吗?或任何其他想法?

4

1 回答 1

0

当我在新的 Python 虚拟环境中运行并重新安装所有必需的软件包时,该问题已得到解决。但是,我仍然不明白它是如何工作的。

于 2019-12-02T13:15:41.383 回答