|
3 | 3 | import os
|
4 | 4 | import tempfile
|
5 | 5 | import contextlib
|
| 6 | +import types |
| 7 | +import importlib |
6 | 8 |
|
7 | 9 | from ._compat import (
|
8 |
| - Path, package_spec, FileNotFoundError, ZipPath, |
9 |
| - singledispatch, suppress, |
| 10 | + Path, FileNotFoundError, |
| 11 | + singledispatch, package_spec, |
10 | 12 | )
|
11 | 13 |
|
| 14 | +if False: # TYPE_CHECKING |
| 15 | + from typing import Union, Any, Optional |
| 16 | + from .abc import ResourceReader |
| 17 | + Package = Union[types.ModuleType, str] |
12 | 18 |
|
13 |
| -def from_package(package): |
| 19 | + |
| 20 | +def files(package): |
14 | 21 | """
|
15 |
| - Return a Traversable object for the given package. |
| 22 | + Get a Traversable resource from a package |
| 23 | + """ |
| 24 | + return from_package(get_package(package)) |
| 25 | + |
16 | 26 |
|
| 27 | +def normalize_path(path): |
| 28 | + # type: (Any) -> str |
| 29 | + """Normalize a path by ensuring it is a string. |
| 30 | +
|
| 31 | + If the resulting string contains path separators, an exception is raised. |
17 | 32 | """
|
18 |
| - spec = package_spec(package) |
19 |
| - return from_traversable_resources(spec) or fallback_resources(spec) |
| 33 | + str_path = str(path) |
| 34 | + parent, file_name = os.path.split(str_path) |
| 35 | + if parent: |
| 36 | + raise ValueError('{!r} must be only a file name'.format(path)) |
| 37 | + return file_name |
20 | 38 |
|
21 | 39 |
|
22 |
| -def from_traversable_resources(spec): |
| 40 | +def get_resource_reader(package): |
| 41 | + # type: (types.ModuleType) -> Optional[ResourceReader] |
23 | 42 | """
|
24 |
| - If the spec.loader implements TraversableResources, |
25 |
| - directly or implicitly, it will have a ``files()`` method. |
| 43 | + Return the package's loader if it's a ResourceReader. |
26 | 44 | """
|
27 |
| - with suppress(AttributeError): |
28 |
| - return spec.loader.files() |
| 45 | + # We can't use |
| 46 | + # a issubclass() check here because apparently abc.'s __subclasscheck__() |
| 47 | + # hook wants to create a weak reference to the object, but |
| 48 | + # zipimport.zipimporter does not support weak references, resulting in a |
| 49 | + # TypeError. That seems terrible. |
| 50 | + spec = package.__spec__ |
| 51 | + reader = getattr(spec.loader, 'get_resource_reader', None) |
| 52 | + if reader is None: |
| 53 | + return None |
| 54 | + return reader(spec.name) |
29 | 55 |
|
30 | 56 |
|
31 |
| -def fallback_resources(spec): |
32 |
| - package_directory = Path(spec.origin).parent |
33 |
| - try: |
34 |
| - archive_path = spec.loader.archive |
35 |
| - rel_path = package_directory.relative_to(archive_path) |
36 |
| - return ZipPath(archive_path, str(rel_path) + '/') |
37 |
| - except Exception: |
38 |
| - pass |
39 |
| - return package_directory |
| 57 | +def resolve(cand): |
| 58 | + # type: (Package) -> types.ModuleType |
| 59 | + return ( |
| 60 | + cand if isinstance(cand, types.ModuleType) |
| 61 | + else importlib.import_module(cand) |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def get_package(package): |
| 66 | + # type: (Package) -> types.ModuleType |
| 67 | + """Take a package name or module object and return the module. |
| 68 | +
|
| 69 | + Raise an exception if the resolved module is not a package. |
| 70 | + """ |
| 71 | + resolved = resolve(package) |
| 72 | + if package_spec(resolved).submodule_search_locations is None: |
| 73 | + raise TypeError('{!r} is not a package'.format(package)) |
| 74 | + return resolved |
| 75 | + |
| 76 | + |
| 77 | +def from_package(package): |
| 78 | + """ |
| 79 | + Return a Traversable object for the given package. |
| 80 | +
|
| 81 | + """ |
| 82 | + spec = package_spec(package) |
| 83 | + reader = spec.loader.get_resource_reader(spec.name) |
| 84 | + return reader.files() |
40 | 85 |
|
41 | 86 |
|
42 | 87 | @contextlib.contextmanager
|
|
0 commit comments