Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 1 | import os |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 2 | import io |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 3 | |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 4 | from . import _common |
Jason R. Coombs | 843c277 | 2020-06-07 21:00:51 -0400 | [diff] [blame] | 5 | from ._common import as_file, files |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 6 | from .abc import ResourceReader |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 7 | from contextlib import suppress |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 8 | from importlib.abc import ResourceLoader |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 9 | from importlib.machinery import ModuleSpec |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 10 | from io import BytesIO, TextIOWrapper |
| 11 | from pathlib import Path |
| 12 | from types import ModuleType |
Jason R. Coombs | 843c277 | 2020-06-07 21:00:51 -0400 | [diff] [blame] | 13 | from typing import ContextManager, Iterable, Union |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 14 | from typing import cast |
| 15 | from typing.io import BinaryIO, TextIO |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 16 | from collections.abc import Sequence |
| 17 | from functools import singledispatch |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 18 | |
| 19 | |
Barry Warsaw | 0ed66df | 2018-05-17 11:41:53 -0400 | [diff] [blame] | 20 | __all__ = [ |
| 21 | 'Package', |
| 22 | 'Resource', |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 23 | 'ResourceReader', |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 24 | 'as_file', |
Barry Warsaw | 0ed66df | 2018-05-17 11:41:53 -0400 | [diff] [blame] | 25 | 'contents', |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 26 | 'files', |
Barry Warsaw | 0ed66df | 2018-05-17 11:41:53 -0400 | [diff] [blame] | 27 | 'is_resource', |
| 28 | 'open_binary', |
| 29 | 'open_text', |
| 30 | 'path', |
| 31 | 'read_binary', |
| 32 | 'read_text', |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 33 | ] |
Barry Warsaw | 0ed66df | 2018-05-17 11:41:53 -0400 | [diff] [blame] | 34 | |
| 35 | |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 36 | Package = Union[str, ModuleType] |
| 37 | Resource = Union[str, os.PathLike] |
| 38 | |
| 39 | |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 40 | def open_binary(package: Package, resource: Resource) -> BinaryIO: |
| 41 | """Return a file-like object opened for binary reading of the resource.""" |
Jason R. Coombs | 843c277 | 2020-06-07 21:00:51 -0400 | [diff] [blame] | 42 | resource = _common.normalize_path(resource) |
| 43 | package = _common.get_package(package) |
| 44 | reader = _common.get_resource_reader(package) |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 45 | if reader is not None: |
| 46 | return reader.open_resource(resource) |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 47 | spec = cast(ModuleSpec, package.__spec__) |
| 48 | # Using pathlib doesn't work well here due to the lack of 'strict' |
| 49 | # argument for pathlib.Path.resolve() prior to Python 3.6. |
| 50 | if spec.submodule_search_locations is not None: |
| 51 | paths = spec.submodule_search_locations |
| 52 | elif spec.origin is not None: |
| 53 | paths = [os.path.dirname(os.path.abspath(spec.origin))] |
| 54 | |
| 55 | for package_path in paths: |
| 56 | full_path = os.path.join(package_path, resource) |
| 57 | try: |
| 58 | return open(full_path, mode='rb') |
| 59 | except OSError: |
| 60 | # Just assume the loader is a resource loader; all the relevant |
| 61 | # importlib.machinery loaders are and an AttributeError for |
| 62 | # get_data() will make it clear what is needed from the loader. |
| 63 | loader = cast(ResourceLoader, spec.loader) |
| 64 | data = None |
| 65 | if hasattr(spec.loader, 'get_data'): |
| 66 | with suppress(OSError): |
| 67 | data = loader.get_data(full_path) |
| 68 | if data is not None: |
| 69 | return BytesIO(data) |
| 70 | |
| 71 | raise FileNotFoundError( |
| 72 | '{!r} resource not found in {!r}'.format(resource, spec.name) |
| 73 | ) |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 74 | |
| 75 | |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 76 | def open_text( |
| 77 | package: Package, |
| 78 | resource: Resource, |
| 79 | encoding: str = 'utf-8', |
| 80 | errors: str = 'strict', |
| 81 | ) -> TextIO: |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 82 | """Return a file-like object opened for text reading of the resource.""" |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 83 | return TextIOWrapper( |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 84 | open_binary(package, resource), encoding=encoding, errors=errors |
| 85 | ) |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def read_binary(package: Package, resource: Resource) -> bytes: |
| 89 | """Return the binary contents of the resource.""" |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 90 | with open_binary(package, resource) as fp: |
| 91 | return fp.read() |
| 92 | |
| 93 | |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 94 | def read_text( |
| 95 | package: Package, |
| 96 | resource: Resource, |
| 97 | encoding: str = 'utf-8', |
| 98 | errors: str = 'strict', |
| 99 | ) -> str: |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 100 | """Return the decoded string of the resource. |
| 101 | |
| 102 | The decoding-related arguments have the same semantics as those of |
| 103 | bytes.decode(). |
| 104 | """ |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 105 | with open_text(package, resource, encoding, errors) as fp: |
| 106 | return fp.read() |
| 107 | |
| 108 | |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 109 | def path( |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 110 | package: Package, |
| 111 | resource: Resource, |
| 112 | ) -> 'ContextManager[Path]': |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 113 | """A context manager providing a file path object to the resource. |
| 114 | |
| 115 | If the resource does not already exist on its own on the file system, |
| 116 | a temporary file will be created. If the file was created, the file |
| 117 | will be deleted upon exiting the context manager (no exception is |
| 118 | raised if the file was deleted prior to the context manager |
| 119 | exiting). |
| 120 | """ |
Jason R. Coombs | 843c277 | 2020-06-07 21:00:51 -0400 | [diff] [blame] | 121 | reader = _common.get_resource_reader(_common.get_package(package)) |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 122 | return ( |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 123 | _path_from_reader(reader, _common.normalize_path(resource)) |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 124 | if reader |
| 125 | else _common.as_file( |
| 126 | _common.files(package).joinpath(_common.normalize_path(resource)) |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 127 | ) |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 128 | ) |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 129 | |
| 130 | |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 131 | def _path_from_reader(reader, resource): |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 132 | return _path_from_resource_path(reader, resource) or _path_from_open_resource( |
| 133 | reader, resource |
| 134 | ) |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def _path_from_resource_path(reader, resource): |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 138 | with suppress(FileNotFoundError): |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 139 | return Path(reader.resource_path(resource)) |
| 140 | |
| 141 | |
| 142 | def _path_from_open_resource(reader, resource): |
| 143 | saved = io.BytesIO(reader.open_resource(resource).read()) |
| 144 | return _common._tempfile(saved.read, suffix=resource) |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 145 | |
| 146 | |
| 147 | def is_resource(package: Package, name: str) -> bool: |
| 148 | """True if 'name' is a resource inside 'package'. |
| 149 | |
| 150 | Directories are *not* resources. |
| 151 | """ |
Jason R. Coombs | 843c277 | 2020-06-07 21:00:51 -0400 | [diff] [blame] | 152 | package = _common.get_package(package) |
| 153 | _common.normalize_path(name) |
| 154 | reader = _common.get_resource_reader(package) |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 155 | if reader is not None: |
| 156 | return reader.is_resource(name) |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 157 | package_contents = set(contents(package)) |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 158 | if name not in package_contents: |
| 159 | return False |
Jason R. Coombs | 7f7e706 | 2020-05-08 19:20:26 -0400 | [diff] [blame] | 160 | return (_common.from_package(package) / name).is_file() |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 161 | |
| 162 | |
Brett Cannon | 3ab9365 | 2018-04-30 11:31:45 -0700 | [diff] [blame] | 163 | def contents(package: Package) -> Iterable[str]: |
| 164 | """Return an iterable of entries in 'package'. |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 165 | |
| 166 | Note that not all entries are resources. Specifically, directories are |
| 167 | not considered resources. Use `is_resource()` on each entry returned here |
| 168 | to check if it is a resource or not. |
| 169 | """ |
Jason R. Coombs | 843c277 | 2020-06-07 21:00:51 -0400 | [diff] [blame] | 170 | package = _common.get_package(package) |
| 171 | reader = _common.get_resource_reader(package) |
Barry Warsaw | deae6b4 | 2017-12-30 15:18:06 -0500 | [diff] [blame] | 172 | if reader is not None: |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 173 | return _ensure_sequence(reader.contents()) |
Jason R. Coombs | 6714825 | 2021-03-04 13:43:00 -0500 | [diff] [blame] | 174 | transversable = _common.from_package(package) |
| 175 | if transversable.is_dir(): |
| 176 | return list(item.name for item in transversable.iterdir()) |
| 177 | return [] |
Jason R. Coombs | df8d4c8 | 2020-10-25 14:21:46 -0400 | [diff] [blame] | 178 | |
| 179 | |
| 180 | @singledispatch |
| 181 | def _ensure_sequence(iterable): |
| 182 | return list(iterable) |
| 183 | |
| 184 | |
| 185 | @_ensure_sequence.register(Sequence) |
| 186 | def _(iterable): |
| 187 | return iterable |