Skip to content

Commit

Permalink
Merge pull request #296 from python/refactor/compat-package
Browse files Browse the repository at this point in the history
Refactor compatibility module
  • Loading branch information
jaraco authored Feb 25, 2024
2 parents 3cf884a + 3736845 commit eec758b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 113 deletions.
2 changes: 1 addition & 1 deletion importlib_resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Union, Optional, cast
from .abc import ResourceReader, Traversable

from ._compat import wrap_spec
from .future.adapters import wrap_spec

Package = Union[types.ModuleType, str]
Anchor = Package
Expand Down
110 changes: 0 additions & 110 deletions importlib_resources/_compat.py

This file was deleted.

3 changes: 2 additions & 1 deletion importlib_resources/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import itertools
import pathlib
from typing import Any, BinaryIO, Iterable, Iterator, NoReturn, Text, Optional
from typing import runtime_checkable, Protocol

from ._compat import runtime_checkable, Protocol, StrPath
from .compat.py38 import StrPath


__all__ = ["ResourceReader", "Traversable", "TraversableResources"]
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions importlib_resources/compat/py38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
import sys

from typing import Union


if sys.version_info >= (3, 9):
StrPath = Union[str, os.PathLike[str]]
else:
# PathLike is only subscriptable at runtime in 3.9+
StrPath = Union[str, "os.PathLike[str]"]
10 changes: 10 additions & 0 deletions importlib_resources/compat/py39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys


__all__ = ['ZipPath']


if sys.version_info >= (3, 10):
from zipfile import Path as ZipPath # type: ignore
else:
from zipp import Path as ZipPath # type: ignore
Empty file.
46 changes: 46 additions & 0 deletions importlib_resources/future/adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pathlib
from contextlib import suppress
from types import SimpleNamespace

from .. import readers, _adapters


class TraversableResourcesLoader(_adapters.TraversableResourcesLoader):
"""
Adapt loaders to provide TraversableResources and other
compatibility.
Ensures the readers from importlib_resources are preferred
over stdlib readers.
"""

def get_resource_reader(self, name):
return self._standard_reader() or super().get_resource_reader(name)

def _standard_reader(self):
return self._zip_reader() or self._namespace_reader() or self._file_reader()

def _zip_reader(self):
with suppress(AttributeError):
return readers.ZipReader(self.spec.loader, self.spec.name)

def _namespace_reader(self):
with suppress(AttributeError, ValueError):
return readers.NamespaceReader(self.spec.submodule_search_locations)

def _file_reader(self):
try:
path = pathlib.Path(self.spec.origin)
except TypeError:
return None
if path.exists():
return readers.FileReader(SimpleNamespace(path=path))


def wrap_spec(package):
"""
Override _adapters.wrap_spec to use TraversableResourcesLoader
from above. Ensures that future behavior is always available on older
Pythons.
"""
return _adapters.SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)
2 changes: 1 addition & 1 deletion importlib_resources/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from . import abc

from ._itertools import only
from ._compat import ZipPath
from .compat.py39 import ZipPath


def remove_duplicates(items):
Expand Down

0 comments on commit eec758b

Please sign in to comment.