2
2
# vi: set ft=python sts=4 ts=4 sw=4 et:
3
3
"""Miscellaneous utilities."""
4
4
5
+ from pathlib import Path
6
+ from typing import Union
7
+
5
8
from .. import __version__
6
9
7
10
@@ -14,7 +17,6 @@ def fix_multi_source_name(in_files):
14
17
'/path/to/sub-045_T1w.nii.gz'
15
18
"""
16
19
import re
17
- from pathlib import Path
18
20
19
21
from nipype .utils .filemanip import filename_to_list
20
22
@@ -113,3 +115,23 @@ def combine_meepi_source(in_files):
113
115
entities = [ent for ent in in_file .split ("_" ) if not ent .startswith ("echo-" )]
114
116
basename = "_" .join (entities )
115
117
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