Skip to content

Commit d068bff

Browse files
authored
Merge pull request #286 from nipreps/rf/anatomical-processing
RF: Anatomical processing
2 parents 9d070eb + 57f3ca6 commit d068bff

File tree

7 files changed

+570
-550
lines changed

7 files changed

+570
-550
lines changed

nibabies/utils/misc.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
"""Miscellaneous utilities."""
44

5+
from pathlib import Path
6+
from typing import Union
7+
58
from .. import __version__
69

710

@@ -14,7 +17,6 @@ def fix_multi_source_name(in_files):
1417
'/path/to/sub-045_T1w.nii.gz'
1518
"""
1619
import re
17-
from pathlib import Path
1820

1921
from nipype.utils.filemanip import filename_to_list
2022

@@ -113,3 +115,23 @@ def combine_meepi_source(in_files):
113115
entities = [ent for ent in in_file.split("_") if not ent.startswith("echo-")]
114116
basename = "_".join(entities)
115117
return os.path.join(base, basename)
118+
119+
120+
def get_file(pkg: str, src_path: Union[str, Path]) -> str:
121+
"""
122+
Get or extract a source file.
123+
Assures the file will be available until the lifetime of the current Python process.
124+
"""
125+
import atexit
126+
from contextlib import ExitStack
127+
128+
try:
129+
from importlib.resources import as_file, files
130+
except ImportError:
131+
from importlib_resources import as_file, files
132+
133+
file_manager = ExitStack()
134+
atexit.register(file_manager.close)
135+
ref = files(pkg) / str(src_path)
136+
fl = file_manager.enter_context(as_file(ref))
137+
return str(fl)

0 commit comments

Comments
 (0)