summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/readers.py
blob: fb49ebe2b16425c38d7178ebf997bdd8360d65cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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()