|
7 | 7 | .. automethod:: load.as_path
|
8 | 8 |
|
9 | 9 | .. automethod:: load.cached
|
10 |
| -
|
11 |
| -.. autoclass:: Loader |
12 | 10 | """
|
13 | 11 |
|
14 |
| -from __future__ import annotations |
15 |
| - |
16 |
| -import atexit |
17 |
| -import os |
18 |
| -from contextlib import AbstractContextManager, ExitStack |
19 |
| -from functools import cached_property |
20 |
| -from pathlib import Path |
21 |
| -from types import ModuleType |
22 |
| - |
23 |
| -try: |
24 |
| - from functools import cache |
25 |
| -except ImportError: # PY38 |
26 |
| - from functools import lru_cache as cache |
27 |
| - |
28 |
| -try: # Prefer backport to leave consistency to dependency spec |
29 |
| - from importlib_resources import as_file, files |
30 |
| -except ImportError: |
31 |
| - from importlib.resources import as_file, files # type: ignore |
32 |
| - |
33 |
| -try: # Prefer stdlib so Sphinx can link to authoritative documentation |
34 |
| - from importlib.resources.abc import Traversable |
35 |
| -except ImportError: |
36 |
| - from importlib_resources.abc import Traversable |
37 |
| - |
38 |
| -__all__ = ['load'] |
39 |
| - |
40 |
| - |
41 |
| -class Loader: |
42 |
| - """A loader for package files relative to a module |
43 |
| -
|
44 |
| - This class wraps :mod:`importlib.resources` to provide a getter |
45 |
| - function with an interpreter-lifetime scope. For typical packages |
46 |
| - it simply passes through filesystem paths as :class:`~pathlib.Path` |
47 |
| - objects. For zipped distributions, it will unpack the files into |
48 |
| - a temporary directory that is cleaned up on interpreter exit. |
49 |
| -
|
50 |
| - This loader accepts a fully-qualified module name or a module |
51 |
| - object. |
52 |
| -
|
53 |
| - Expected usage:: |
54 |
| -
|
55 |
| - '''Data package |
56 |
| -
|
57 |
| - .. autofunction:: load_data |
58 |
| -
|
59 |
| - .. automethod:: load_data.readable |
60 |
| -
|
61 |
| - .. automethod:: load_data.as_path |
62 |
| -
|
63 |
| - .. automethod:: load_data.cached |
64 |
| - ''' |
65 |
| -
|
66 |
| - from fmriprep.data import Loader |
67 |
| -
|
68 |
| - load_data = Loader(__package__) |
69 |
| -
|
70 |
| - :class:`~Loader` objects implement the :func:`callable` interface |
71 |
| - and generate a docstring, and are intended to be treated and documented |
72 |
| - as functions. |
73 |
| -
|
74 |
| - For greater flexibility and improved readability over the ``importlib.resources`` |
75 |
| - interface, explicit methods are provided to access resources. |
76 |
| -
|
77 |
| - +---------------+----------------+------------------+ |
78 |
| - | On-filesystem | Lifetime | Method | |
79 |
| - +---------------+----------------+------------------+ |
80 |
| - | `True` | Interpreter | :meth:`cached` | |
81 |
| - +---------------+----------------+------------------+ |
82 |
| - | `True` | `with` context | :meth:`as_path` | |
83 |
| - +---------------+----------------+------------------+ |
84 |
| - | `False` | n/a | :meth:`readable` | |
85 |
| - +---------------+----------------+------------------+ |
86 |
| -
|
87 |
| - It is also possible to use ``Loader`` directly:: |
88 |
| -
|
89 |
| - from fmriprep.data import Loader |
90 |
| -
|
91 |
| - Loader(other_package).readable('data/resource.ext').read_text() |
92 |
| -
|
93 |
| - with Loader(other_package).as_path('data') as pkgdata: |
94 |
| - # Call function that requires full Path implementation |
95 |
| - func(pkgdata) |
96 |
| -
|
97 |
| - # contrast to |
98 |
| -
|
99 |
| - from importlib_resources import files, as_file |
100 |
| -
|
101 |
| - files(other_package).joinpath('data/resource.ext').read_text() |
102 |
| -
|
103 |
| - with as_file(files(other_package) / 'data') as pkgdata: |
104 |
| - func(pkgdata) |
105 |
| -
|
106 |
| - .. automethod:: readable |
107 |
| -
|
108 |
| - .. automethod:: as_path |
109 |
| -
|
110 |
| - .. automethod:: cached |
111 |
| - """ |
112 |
| - |
113 |
| - def __init__(self, anchor: str | ModuleType): |
114 |
| - self._anchor = anchor |
115 |
| - self.files = files(anchor) |
116 |
| - self.exit_stack = ExitStack() |
117 |
| - atexit.register(self.exit_stack.close) |
118 |
| - # Allow class to have a different docstring from instances |
119 |
| - self.__doc__ = self._doc |
120 |
| - |
121 |
| - @cached_property |
122 |
| - def _doc(self): |
123 |
| - """Construct docstring for instances |
124 |
| -
|
125 |
| - Lists the public top-level paths inside the location, where |
126 |
| - non-public means has a `.` or `_` prefix or is a 'tests' |
127 |
| - directory. |
128 |
| - """ |
129 |
| - top_level = sorted( |
130 |
| - os.path.relpath(p, self.files) + '/'[: p.is_dir()] |
131 |
| - for p in self.files.iterdir() |
132 |
| - if p.name[0] not in ('.', '_') and p.name != 'tests' |
133 |
| - ) |
134 |
| - doclines = [ |
135 |
| - f'Load package files relative to ``{self._anchor}``.', |
136 |
| - '', |
137 |
| - 'This package contains the following (top-level) files/directories:', |
138 |
| - '', |
139 |
| - *(f'* ``{path}``' for path in top_level), |
140 |
| - ] |
141 |
| - |
142 |
| - return '\n'.join(doclines) |
143 |
| - |
144 |
| - def readable(self, *segments) -> Traversable: |
145 |
| - """Provide read access to a resource through a Path-like interface. |
146 |
| -
|
147 |
| - This file may or may not exist on the filesystem, and may be |
148 |
| - efficiently used for read operations, including directory traversal. |
149 |
| -
|
150 |
| - This result is not cached or copied to the filesystem in cases where |
151 |
| - that would be necessary. |
152 |
| - """ |
153 |
| - return self.files.joinpath(*segments) |
154 |
| - |
155 |
| - def as_path(self, *segments) -> AbstractContextManager[Path]: |
156 |
| - """Ensure data is available as a :class:`~pathlib.Path`. |
157 |
| -
|
158 |
| - This method generates a context manager that yields a Path when |
159 |
| - entered. |
160 |
| -
|
161 |
| - This result is not cached, and any temporary files that are created |
162 |
| - are deleted when the context is exited. |
163 |
| - """ |
164 |
| - return as_file(self.files.joinpath(*segments)) |
165 |
| - |
166 |
| - @cache # noqa: B019 |
167 |
| - def cached(self, *segments) -> Path: |
168 |
| - """Ensure data is available as a :class:`~pathlib.Path`. |
169 |
| -
|
170 |
| - Any temporary files that are created remain available throughout |
171 |
| - the duration of the program, and are deleted when Python exits. |
172 |
| -
|
173 |
| - Results are cached so that multiple calls do not unpack the same |
174 |
| - data multiple times, but the cache is sensitive to the specific |
175 |
| - argument(s) passed. |
176 |
| - """ |
177 |
| - return self.exit_stack.enter_context(as_file(self.files.joinpath(*segments))) |
178 |
| - |
179 |
| - __call__ = cached |
180 |
| - |
| 12 | +from acres import Loader |
181 | 13 |
|
182 | 14 | load = Loader(__package__)
|
0 commit comments