Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: 0b1a1c8c4c556169799f027a39950de78d726b47 authored by Ɓukasz Langa on 08 June 2020, 18:41:34 UTC
Python 3.9.0b2
Tip revision: 0b1a1c8
readers.py
import zipfile
import pathlib
from . import abc


class FileReader(abc.TraversableResources):
    def __init__(self, loader):
        self.path = pathlib.Path(loader.path).parent

    def files(self):
        return self.path


class ZipReader(FileReader):
    def __init__(self, loader, module):
        _, _, name = module.rpartition('.')
        prefix = loader.prefix.replace('\\', '/') + name + '/'
        self.path = zipfile.Path(loader.archive, prefix)

    def open_resource(self, resource):
        try:
            return super().open_resource(resource)
        except KeyError as exc:
            raise FileNotFoundError(exc.args[0])

    def is_resource(self, path):
        # workaround for `zipfile.Path.is_file` returning true
        # for non-existent paths.
        target = self.files().joinpath(path)
        return target.is_file() and target.exists()
back to top