|
19 | 19 |
|
20 | 20 | """Constants for the web API.""" |
21 | 21 |
|
| 22 | +import atexit |
22 | 23 | import shutil |
| 24 | +from contextlib import ExitStack |
| 25 | +from importlib.resources import as_file, files |
23 | 26 |
|
24 | 27 | import gramps.gen.lib as lib |
25 | 28 | from gramps.gen.plug import CATEGORY_DRAW, CATEGORY_GRAPHVIZ, CATEGORY_TEXT |
26 | | -from pkg_resources import resource_filename # type: ignore[import-untyped] |
27 | 29 |
|
28 | 30 | from ._version import __version__ as VERSION |
29 | 31 |
|
30 | 32 | # the value of the TREE config option that enables multi-tree support |
31 | 33 | TREE_MULTI = "*" |
32 | 34 |
|
33 | | -# files |
34 | | -TEST_CONFIG = resource_filename("gramps_webapi", "data/test.cfg") |
35 | | -TEST_AUTH_CONFIG = resource_filename("gramps_webapi", "data/test_auth.cfg") |
36 | | -TEST_EXAMPLE_GRAMPS_CONFIG = resource_filename( |
| 35 | +# Helper function to get resource file paths with managed cleanup |
| 36 | +_file_manager = ExitStack() |
| 37 | +atexit.register(_file_manager.close) |
| 38 | + |
| 39 | + |
| 40 | +def _get_resource_path(package: str, resource_path: str) -> str: |
| 41 | + """Get the file system path for a package resource with managed cleanup.""" |
| 42 | + ref = files(package) / resource_path |
| 43 | + return str(_file_manager.enter_context(as_file(ref))) |
| 44 | + |
| 45 | + |
| 46 | +# Configuration files |
| 47 | +TEST_CONFIG = _get_resource_path("gramps_webapi", "data/test.cfg") |
| 48 | +TEST_AUTH_CONFIG = _get_resource_path("gramps_webapi", "data/test_auth.cfg") |
| 49 | +TEST_EXAMPLE_GRAMPS_CONFIG = _get_resource_path( |
37 | 50 | "gramps_webapi", "data/example_gramps.cfg" |
38 | 51 | ) |
39 | | -TEST_EXAMPLE_GRAMPS_AUTH_CONFIG = resource_filename( |
| 52 | +TEST_EXAMPLE_GRAMPS_AUTH_CONFIG = _get_resource_path( |
40 | 53 | "gramps_webapi", "data/example_gramps_auth.cfg" |
41 | 54 | ) |
42 | | -TEST_EMPTY_GRAMPS_AUTH_CONFIG = resource_filename( |
| 55 | +TEST_EMPTY_GRAMPS_AUTH_CONFIG = _get_resource_path( |
43 | 56 | "gramps_webapi", "data/empty_gramps_auth.cfg" |
44 | 57 | ) |
45 | 58 |
|
|
0 commit comments