-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from python/refactor/compat-package
Refactor compatibility module
- Loading branch information
Showing
9 changed files
with
71 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters