|
1 | 1 | """Module containing data files, including the schema source files |
2 | 2 |
|
3 | | -.. autofunction:: load_resource |
4 | | -""" |
5 | | - |
6 | | -import atexit |
7 | | -import os |
8 | | -from contextlib import ExitStack |
9 | | -from functools import cache, cached_property |
10 | | -from importlib.resources import as_file, files |
11 | | -from pathlib import Path |
12 | | -from types import ModuleType |
13 | | -from typing import Union |
14 | | - |
15 | | -__all__ = ["load_resource"] |
16 | | - |
17 | | - |
18 | | -class Loader: |
19 | | - """A loader for package files relative to a module |
20 | | -
|
21 | | - This class wraps :mod:`importlib.resources` to provide a getter |
22 | | - function with an interpreter-lifetime scope. For typical packages |
23 | | - it simply passes through filesystem paths as :class:`~pathlib.Path` |
24 | | - objects. For zipped distributions, it will unpack the files into |
25 | | - a temporary directory that is cleaned up on interpreter exit. |
26 | | -
|
27 | | - This loader accepts a fully-qualified module name or a module |
28 | | - object. |
29 | | -
|
30 | | - Expected usage:: |
31 | | -
|
32 | | - '''Data package |
| 3 | +.. autofunction:: load |
33 | 4 |
|
34 | | - .. autofunction:: load_data |
35 | | - ''' |
| 5 | +.. automethod:: load.readable |
36 | 6 |
|
37 | | - from bidsschematools.data import Loader |
| 7 | +.. automethod:: load.as_path |
38 | 8 |
|
39 | | - load_data = Loader(__package__) |
40 | | -
|
41 | | - :class:`~Loader` objects implement the :func:`callable` interface |
42 | | - and generate a docstring, and are intended to be treated and documented |
43 | | - as functions. |
44 | | - """ |
45 | | - |
46 | | - def __init__(self, anchor: Union[str, ModuleType]): |
47 | | - self._anchor = anchor |
48 | | - self.files = files(anchor) |
49 | | - self.exit_stack = ExitStack() |
50 | | - atexit.register(self.exit_stack.close) |
51 | | - # Allow class to have a different docstring from instances |
52 | | - self.__doc__ = self._doc |
53 | | - |
54 | | - @cached_property |
55 | | - def _doc(self): |
56 | | - """Construct docstring for instances |
57 | | -
|
58 | | - Lists the public top-level paths inside the location, where |
59 | | - non-public means has a `.` or `_` prefix or is a 'tests' |
60 | | - directory. |
61 | | - """ |
62 | | - top_level = sorted( |
63 | | - os.path.relpath(p, self.files) + "/"[: p.is_dir()] |
64 | | - for p in self.files.iterdir() |
65 | | - if p.name[0] not in (".", "_") and p.name != "tests" |
66 | | - ) |
67 | | - doclines = [ |
68 | | - f"Load package files relative to ``{self._anchor}``.", |
69 | | - "", |
70 | | - "This package contains the following (top-level) files/directories:", |
71 | | - "", |
72 | | - *(f"* ``{path}``" for path in top_level), |
73 | | - ] |
74 | | - |
75 | | - return "\n".join(doclines) |
76 | | - |
77 | | - @cache |
78 | | - def __call__(self, *segments) -> Path: |
79 | | - """Ensure data is available as a :class:`~pathlib.Path`. |
80 | | -
|
81 | | - Any temporary files that are created remain available throughout |
82 | | - the duration of the program, and are deleted when Python exits. |
83 | | -
|
84 | | - Results are cached so that multiple calls do not unpack the same |
85 | | - data multiple times, but the cache is sensitive to the specific |
86 | | - argument(s) passed. |
87 | | - """ |
88 | | - return self.exit_stack.enter_context(as_file(self.files.joinpath(*segments))) |
| 9 | +.. automethod:: load.cached |
| 10 | +""" |
89 | 11 |
|
| 12 | +from acres import Loader |
90 | 13 |
|
91 | | -load_resource = Loader(__package__) |
| 14 | +load = Loader(__spec__.name) |
0 commit comments