From eaa89c20e61f5537eb3fbb0635e029ae0b40f0c3 Mon Sep 17 00:00:00 2001 From: mathiasg Date: Mon, 6 Jul 2020 16:48:29 -0400 Subject: [PATCH] ENH: Add option to raise exception on empty ``get`` query --- templateflow/api.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/templateflow/api.py b/templateflow/api.py index 5a65940a..db426554 100644 --- a/templateflow/api.py +++ b/templateflow/api.py @@ -7,7 +7,7 @@ from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD -def get(template, **kwargs): +def get(template, raise_empty=False, **kwargs): """ Fetch one file from one particular template. @@ -15,6 +15,8 @@ def get(template, **kwargs): ---------- template : str A template identifier (e.g., ``MNI152NLin2009cAsym``). + raise_empty : bool, optional + Raise exception if no files were matched Keyword Arguments ----------------- @@ -50,6 +52,14 @@ def get(template, **kwargs): ... density='32k', suffix='sphere')) # doctest: +ELLIPSIS '.../tpl-fsLR_hemi-L_den-32k_sphere.surf.gii' + >>> get('fsLR', space='madeup') + [] + + >>> get('fsLR', raise_empty=True, space='madeup') # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + Exception: + ... + """ out_file = [ Path(p) for p in TF_LAYOUT.get(template=template, return_type="file", **kwargs) @@ -93,6 +103,9 @@ def get(template, **kwargs): raise RuntimeError(msg) + if not out_file and raise_empty: + raise Exception("No results found") + if len(out_file) == 1: return out_file[0] return out_file