2020-05-08 20:20:26 -03:00
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
import tempfile
|
|
|
|
import functools
|
|
|
|
import contextlib
|
2020-06-07 22:00:51 -03:00
|
|
|
import types
|
|
|
|
import importlib
|
2023-01-01 12:07:32 -04:00
|
|
|
import inspect
|
|
|
|
import warnings
|
|
|
|
import itertools
|
2020-05-08 20:20:26 -03:00
|
|
|
|
2023-01-01 12:07:32 -04:00
|
|
|
from typing import Union, Optional, cast
|
2021-05-21 14:00:40 -03:00
|
|
|
from .abc import ResourceReader, Traversable
|
2020-05-08 20:20:26 -03:00
|
|
|
|
2020-06-07 22:00:51 -03:00
|
|
|
Package = Union[types.ModuleType, str]
|
2023-01-01 12:07:32 -04:00
|
|
|
Anchor = Package
|
2020-06-07 22:00:51 -03:00
|
|
|
|
|
|
|
|
2023-01-01 12:07:32 -04:00
|
|
|
def package_to_anchor(func):
|
2020-05-08 20:20:26 -03:00
|
|
|
"""
|
2023-01-01 12:07:32 -04:00
|
|
|
Replace 'package' parameter as 'anchor' and warn about the change.
|
|
|
|
|
|
|
|
Other errors should fall through.
|
|
|
|
|
|
|
|
>>> files('a', 'b')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
TypeError: files() takes from 0 to 1 positional arguments but 2 were given
|
2024-06-04 03:36:28 -03:00
|
|
|
|
|
|
|
Remove this compatibility in Python 3.14.
|
2023-01-01 12:07:32 -04:00
|
|
|
"""
|
|
|
|
undefined = object()
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def wrapper(anchor=undefined, package=undefined):
|
|
|
|
if package is not undefined:
|
|
|
|
if anchor is not undefined:
|
|
|
|
return func(anchor, package)
|
|
|
|
warnings.warn(
|
|
|
|
"First parameter to files is renamed to 'anchor'",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
return func(package)
|
|
|
|
elif anchor is undefined:
|
|
|
|
return func()
|
|
|
|
return func(anchor)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
@package_to_anchor
|
|
|
|
def files(anchor: Optional[Anchor] = None) -> Traversable:
|
2020-06-07 22:00:51 -03:00
|
|
|
"""
|
2023-01-01 12:07:32 -04:00
|
|
|
Get a Traversable resource for an anchor.
|
|
|
|
"""
|
|
|
|
return from_package(resolve(anchor))
|
2020-06-07 22:00:51 -03:00
|
|
|
|
2020-05-08 20:20:26 -03:00
|
|
|
|
2023-01-01 12:07:32 -04:00
|
|
|
def get_resource_reader(package: types.ModuleType) -> Optional[ResourceReader]:
|
2020-05-08 20:20:26 -03:00
|
|
|
"""
|
2020-06-07 22:00:51 -03:00
|
|
|
Return the package's loader if it's a ResourceReader.
|
2020-05-08 20:20:26 -03:00
|
|
|
"""
|
2020-06-07 22:00:51 -03:00
|
|
|
# We can't use
|
|
|
|
# a issubclass() check here because apparently abc.'s __subclasscheck__()
|
|
|
|
# hook wants to create a weak reference to the object, but
|
|
|
|
# zipimport.zipimporter does not support weak references, resulting in a
|
|
|
|
# TypeError. That seems terrible.
|
|
|
|
spec = package.__spec__
|
2021-03-04 14:43:00 -04:00
|
|
|
reader = getattr(spec.loader, 'get_resource_reader', None) # type: ignore
|
2020-06-07 22:00:51 -03:00
|
|
|
if reader is None:
|
|
|
|
return None
|
2021-03-04 14:43:00 -04:00
|
|
|
return reader(spec.name) # type: ignore
|
2020-05-08 20:20:26 -03:00
|
|
|
|
|
|
|
|
2023-01-01 12:07:32 -04:00
|
|
|
@functools.singledispatch
|
|
|
|
def resolve(cand: Optional[Anchor]) -> types.ModuleType:
|
|
|
|
return cast(types.ModuleType, cand)
|
|
|
|
|
|
|
|
|
|
|
|
@resolve.register
|
|
|
|
def _(cand: str) -> types.ModuleType:
|
|
|
|
return importlib.import_module(cand)
|
2020-06-07 22:00:51 -03:00
|
|
|
|
|
|
|
|
2023-01-01 12:07:32 -04:00
|
|
|
@resolve.register
|
|
|
|
def _(cand: None) -> types.ModuleType:
|
|
|
|
return resolve(_infer_caller().f_globals['__name__'])
|
2020-06-07 22:00:51 -03:00
|
|
|
|
2023-01-01 12:07:32 -04:00
|
|
|
|
|
|
|
def _infer_caller():
|
|
|
|
"""
|
|
|
|
Walk the stack and find the frame of the first caller not in this module.
|
2020-06-07 22:00:51 -03:00
|
|
|
"""
|
2023-01-01 12:07:32 -04:00
|
|
|
|
|
|
|
def is_this_file(frame_info):
|
2024-09-12 16:08:06 -03:00
|
|
|
return frame_info.filename == stack[0].filename
|
2023-01-01 12:07:32 -04:00
|
|
|
|
|
|
|
def is_wrapper(frame_info):
|
|
|
|
return frame_info.function == 'wrapper'
|
|
|
|
|
2024-09-12 16:08:06 -03:00
|
|
|
stack = inspect.stack()
|
|
|
|
not_this_file = itertools.filterfalse(is_this_file, stack)
|
2023-01-01 12:07:32 -04:00
|
|
|
# also exclude 'wrapper' due to singledispatch in the call stack
|
|
|
|
callers = itertools.filterfalse(is_wrapper, not_this_file)
|
|
|
|
return next(callers).frame
|
2020-06-07 22:00:51 -03:00
|
|
|
|
|
|
|
|
2023-01-01 12:07:32 -04:00
|
|
|
def from_package(package: types.ModuleType):
|
2020-06-07 22:00:51 -03:00
|
|
|
"""
|
|
|
|
Return a Traversable object for the given package.
|
|
|
|
|
|
|
|
"""
|
2024-03-21 00:49:10 -03:00
|
|
|
# deferred for performance (python/cpython#109829)
|
|
|
|
from ._adapters import wrap_spec
|
|
|
|
|
2021-03-04 14:43:00 -04:00
|
|
|
spec = wrap_spec(package)
|
2020-06-07 22:00:51 -03:00
|
|
|
reader = spec.loader.get_resource_reader(spec.name)
|
|
|
|
return reader.files()
|
2020-05-08 20:20:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2022-10-16 16:00:39 -03:00
|
|
|
def _tempfile(
|
|
|
|
reader,
|
|
|
|
suffix='',
|
|
|
|
# gh-93353: Keep a reference to call os.remove() in late Python
|
|
|
|
# finalization.
|
|
|
|
*,
|
|
|
|
_os_remove=os.remove,
|
|
|
|
):
|
2020-05-08 20:20:26 -03:00
|
|
|
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
|
|
|
|
# blocks due to the need to close the temporary file to work on Windows
|
|
|
|
# properly.
|
|
|
|
fd, raw_path = tempfile.mkstemp(suffix=suffix)
|
|
|
|
try:
|
2021-07-30 21:37:09 -03:00
|
|
|
try:
|
|
|
|
os.write(fd, reader())
|
|
|
|
finally:
|
|
|
|
os.close(fd)
|
2020-10-25 15:21:46 -03:00
|
|
|
del reader
|
2020-05-08 20:20:26 -03:00
|
|
|
yield pathlib.Path(raw_path)
|
|
|
|
finally:
|
|
|
|
try:
|
2022-06-13 14:24:00 -03:00
|
|
|
_os_remove(raw_path)
|
2021-07-30 21:37:09 -03:00
|
|
|
except FileNotFoundError:
|
2020-05-08 20:20:26 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-10-16 16:00:39 -03:00
|
|
|
def _temp_file(path):
|
|
|
|
return _tempfile(path.read_bytes, suffix=path.name)
|
|
|
|
|
|
|
|
|
|
|
|
def _is_present_dir(path: Traversable) -> bool:
|
|
|
|
"""
|
|
|
|
Some Traversables implement ``is_dir()`` to raise an
|
|
|
|
exception (i.e. ``FileNotFoundError``) when the
|
|
|
|
directory doesn't exist. This function wraps that call
|
|
|
|
to always return a boolean and only return True
|
|
|
|
if there's a dir and it exists.
|
|
|
|
"""
|
|
|
|
with contextlib.suppress(FileNotFoundError):
|
|
|
|
return path.is_dir()
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-05-08 20:20:26 -03:00
|
|
|
@functools.singledispatch
|
|
|
|
def as_file(path):
|
|
|
|
"""
|
|
|
|
Given a Traversable object, return that object as a
|
|
|
|
path on the local file system in a context manager.
|
|
|
|
"""
|
2022-10-16 16:00:39 -03:00
|
|
|
return _temp_dir(path) if _is_present_dir(path) else _temp_file(path)
|
2020-05-08 20:20:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
@as_file.register(pathlib.Path)
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def _(path):
|
|
|
|
"""
|
|
|
|
Degenerate behavior for pathlib.Path objects.
|
|
|
|
"""
|
|
|
|
yield path
|
2022-10-16 16:00:39 -03:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def _temp_path(dir: tempfile.TemporaryDirectory):
|
|
|
|
"""
|
2024-09-02 21:20:40 -03:00
|
|
|
Wrap tempfile.TemporaryDirectory to return a pathlib object.
|
2022-10-16 16:00:39 -03:00
|
|
|
"""
|
|
|
|
with dir as result:
|
|
|
|
yield pathlib.Path(result)
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def _temp_dir(path):
|
|
|
|
"""
|
|
|
|
Given a traversable dir, recursively replicate the whole tree
|
|
|
|
to the file system in a context manager.
|
|
|
|
"""
|
|
|
|
assert path.is_dir()
|
|
|
|
with _temp_path(tempfile.TemporaryDirectory()) as temp_dir:
|
|
|
|
yield _write_contents(temp_dir, path)
|
|
|
|
|
|
|
|
|
|
|
|
def _write_contents(target, source):
|
|
|
|
child = target.joinpath(source.name)
|
|
|
|
if source.is_dir():
|
|
|
|
child.mkdir()
|
|
|
|
for item in source.iterdir():
|
|
|
|
_write_contents(child, item)
|
|
|
|
else:
|
2022-12-28 17:31:43 -04:00
|
|
|
child.write_bytes(source.read_bytes())
|
2022-10-16 16:00:39 -03:00
|
|
|
return child
|