Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion nibabies/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Miscellaneous utilities."""

from pathlib import Path
from typing import Union

from .. import __version__


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

from nipype.utils.filemanip import filename_to_list

Expand Down Expand Up @@ -113,3 +115,23 @@ def combine_meepi_source(in_files):
entities = [ent for ent in in_file.split("_") if not ent.startswith("echo-")]
basename = "_".join(entities)
return os.path.join(base, basename)


def get_file(pkg: str, src_path: Union[str, Path]) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. But I would consider using a module-local ExitStack and cached responses to avoid unnecessary duplicates.

Example:

import atexit
from contextlib import ExitStack
from functools import cache
try:
    from importlib.resources import as_file, files
except ImportError:
    from importlib_resources import as_file, files

# Module-scoped
exit_stack = ExitStack()
atexit.register(exit_stack.close)

@cache
def get_file(pkg: str, src_path: Union[str, Path]) -> str:
    file_ref = files(pkg) / src_path
    pathlike = exit_stack.enter_context(as_file(file_ref))
    return str(pathlike)

"""
Get or extract a source file.
Assures the file will be available until the lifetime of the current Python process.
"""
import atexit
from contextlib import ExitStack

try:
from importlib.resources import as_file, files
except ImportError:
from importlib_resources import as_file, files

file_manager = ExitStack()
atexit.register(file_manager.close)
ref = files(pkg) / str(src_path)
fl = file_manager.enter_context(as_file(ref))
return str(fl)
Loading