aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/importlib-resources/importlib_resources/__init__.py
blob: bdbac100a92292a58e57e695c452c5eb4ead1630 (plain) (blame)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import io
import pkgutil
from contextlib import contextmanager

__all__ = 'read_binary read_text open_binary open_text is_resource contents path'.split()

try:
    FileNotFoundError
except NameError:
    FileNotFoundError = OSError


def read_binary(package, resource):
    data = pkgutil.get_data(package, resource)
    if data is None:
        raise FileNotFoundError('{} does not contain {!r}'.format(package, resource))
    return data


def read_text(package, resource, encoding='utf-8', errors='strict'):
    return read_binary(package, resource).decode(encoding, errors)


def open_binary(package, resource):
    return io.BytesIO(read_binary(package, resource))


def open_text(package, resource, encoding='utf-8', errors='strict'):
    return io.StringIO(read_text(package, resource, encoding, errors))


def is_resource(package, name):
    try:
        read_binary(package, name)
        return True
    except (FileNotFoundError, OSError, IOError):
        return False


def contents(package):
    raise NotImplementedError('importlib_resources.contents is not implemented')


@contextmanager
def path(package, resource):
    raise NotImplementedError('importlib_resources.path is not implemented')
    yield None