Skip to content

Commit eec758b

Browse files
authored
Merge pull request #296 from python/refactor/compat-package
Refactor compatibility module
2 parents 3cf884a + 3736845 commit eec758b

File tree

9 files changed

+71
-113
lines changed

9 files changed

+71
-113
lines changed

importlib_resources/_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing import Union, Optional, cast
1313
from .abc import ResourceReader, Traversable
1414

15-
from ._compat import wrap_spec
15+
from .future.adapters import wrap_spec
1616

1717
Package = Union[types.ModuleType, str]
1818
Anchor = Package

importlib_resources/_compat.py

Lines changed: 0 additions & 110 deletions
This file was deleted.

importlib_resources/abc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import itertools
44
import pathlib
55
from typing import Any, BinaryIO, Iterable, Iterator, NoReturn, Text, Optional
6+
from typing import runtime_checkable, Protocol
67

7-
from ._compat import runtime_checkable, Protocol, StrPath
8+
from .compat.py38 import StrPath
89

910

1011
__all__ = ["ResourceReader", "Traversable", "TraversableResources"]

importlib_resources/compat/__init__.py

Whitespace-only changes.

importlib_resources/compat/py38.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import sys
3+
4+
from typing import Union
5+
6+
7+
if sys.version_info >= (3, 9):
8+
StrPath = Union[str, os.PathLike[str]]
9+
else:
10+
# PathLike is only subscriptable at runtime in 3.9+
11+
StrPath = Union[str, "os.PathLike[str]"]

importlib_resources/compat/py39.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
3+
4+
__all__ = ['ZipPath']
5+
6+
7+
if sys.version_info >= (3, 10):
8+
from zipfile import Path as ZipPath # type: ignore
9+
else:
10+
from zipp import Path as ZipPath # type: ignore

importlib_resources/future/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pathlib
2+
from contextlib import suppress
3+
from types import SimpleNamespace
4+
5+
from .. import readers, _adapters
6+
7+
8+
class TraversableResourcesLoader(_adapters.TraversableResourcesLoader):
9+
"""
10+
Adapt loaders to provide TraversableResources and other
11+
compatibility.
12+
13+
Ensures the readers from importlib_resources are preferred
14+
over stdlib readers.
15+
"""
16+
17+
def get_resource_reader(self, name):
18+
return self._standard_reader() or super().get_resource_reader(name)
19+
20+
def _standard_reader(self):
21+
return self._zip_reader() or self._namespace_reader() or self._file_reader()
22+
23+
def _zip_reader(self):
24+
with suppress(AttributeError):
25+
return readers.ZipReader(self.spec.loader, self.spec.name)
26+
27+
def _namespace_reader(self):
28+
with suppress(AttributeError, ValueError):
29+
return readers.NamespaceReader(self.spec.submodule_search_locations)
30+
31+
def _file_reader(self):
32+
try:
33+
path = pathlib.Path(self.spec.origin)
34+
except TypeError:
35+
return None
36+
if path.exists():
37+
return readers.FileReader(SimpleNamespace(path=path))
38+
39+
40+
def wrap_spec(package):
41+
"""
42+
Override _adapters.wrap_spec to use TraversableResourcesLoader
43+
from above. Ensures that future behavior is always available on older
44+
Pythons.
45+
"""
46+
return _adapters.SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)

importlib_resources/readers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from . import abc
1010

1111
from ._itertools import only
12-
from ._compat import ZipPath
12+
from .compat.py39 import ZipPath
1313

1414

1515
def remove_duplicates(items):

0 commit comments

Comments
 (0)