Skip to content

Commit 8fa8849

Browse files
author
Martin Ruefenacht
committed
Dealt with deprecation warning and cleaned up load internal db
1 parent 9cf7f01 commit 8fa8849

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/pympistandard/__init__.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
from typing import Union, Tuple, Optional
1717
import logging
1818
import os
19+
import sys
20+
21+
22+
MPI_DATABASE_FILE: str = "apis.json"
1923

2024

2125
from .storage import KINDS, PROCEDURES, CALLBACKS, PREDEFINED_FUNCTIONS, clear_storage
@@ -129,38 +133,42 @@ def _register_kinds_v1() -> None:
129133
KINDS[key.lower()] = item
130134

131135

136+
def _load_bundled_db():
137+
if sys.version_info.major == 3 and sys.version_info.minor < 9:
138+
with importlib.resources.path(
139+
"pympistandard.data", MPI_DATABASE_FILE
140+
) as datapath:
141+
return datapath
142+
143+
else:
144+
return importlib.resources.files("pympistandard.data").joinpath(
145+
MPI_DATABASE_FILE
146+
)
147+
148+
132149
def _resolve_path(
133150
given_path: Optional[Union[str, Path]] = None, force_bundled: bool = False
134151
) -> Path:
135152
"""Find correct path to load apis.json from."""
136153

137154
if force_bundled:
138-
with importlib.resources.path("pympistandard.data", "apis.json") as datapath:
139-
path = datapath
155+
path = _load_bundled_db()
140156

141157
# convert str path to Path
142158
elif isinstance(given_path, str):
143159
given_path = Path(given_path)
144160

145161
# use given path
146162
elif isinstance(given_path, Path):
147-
path = given_path / "apis.json"
163+
path = given_path / MPI_DATABASE_FILE
148164

149165
# use environment variable paths
150166
elif "MPISTANDARD" in os.environ:
151-
path = Path(os.environ["MPISTANDARD"] + "/apis.json")
152-
153-
# else:
154-
# raise RuntimeError(
155-
# "Could not find apis.json, either use MPISTANDARD environment variable"
156-
# "or execute pympistandard from root of MPI Standard direction."
157-
# )
167+
path = Path(os.environ["MPISTANDARD"] + "/" + MPI_DATABASE_FILE)
158168

159169
else:
160170
# fallback to packaged data
161-
# AFTER 3.9 path = importlib.resources.files("pympistandard.data").joinpath("apis.json")
162-
with importlib.resources.path("pympistandard.data", "apis.json") as datapath:
163-
path = datapath
171+
path = _load_bundled_db()
164172

165173
# require resolved path to exist
166174
path.resolve(True)

0 commit comments

Comments
 (0)