Skip to content

Commit 70681f7

Browse files
oestebaneffigies
andcommitted
enh: use function decorator
Co-authored-by: Chris Markiewicz <[email protected]>
1 parent a5e3faa commit 70681f7

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

templateflow/api.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import re
55
import sys
66

7-
from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD
7+
from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD, requires_layout
88

99

10+
@requires_layout
1011
def get(template, raise_empty=False, **kwargs):
1112
"""
1213
Fetch one file from one particular template.
@@ -61,10 +62,6 @@ def get(template, raise_empty=False, **kwargs):
6162
...
6263
6364
"""
64-
if TF_LAYOUT is None:
65-
from bids import __version__
66-
raise RuntimeError(f"A layout with PyBIDS <{__version__}> could not be initiated")
67-
6865
out_file = [
6966
Path(p) for p in TF_LAYOUT.get(template=template, return_type="file", **kwargs)
7067
]
@@ -115,9 +112,10 @@ def get(template, raise_empty=False, **kwargs):
115112
return out_file
116113

117114

115+
@requires_layout
118116
def templates(**kwargs):
119117
"""
120-
Returns a list of available templates.
118+
Return a list of available templates.
121119
122120
Keyword Arguments
123121
-----------------
@@ -141,13 +139,10 @@ def templates(**kwargs):
141139
['MNI152Lin', 'MNI152NLin2009cAsym', 'MNI152NLin2009cSym', 'MNIInfant', 'MNIPediatricAsym']
142140
143141
"""
144-
if TF_LAYOUT is None:
145-
from bids import __version__
146-
raise RuntimeError(f"A layout with PyBIDS <{__version__}> could not be initiated")
147-
148142
return sorted(TF_LAYOUT.get_templates(**kwargs))
149143

150144

145+
@requires_layout
151146
def get_metadata(template):
152147
"""
153148
Fetch one file from one template.
@@ -163,10 +158,6 @@ def get_metadata(template):
163158
'Linear ICBM Average Brain (ICBM152) Stereotaxic Registration Model'
164159
165160
"""
166-
if TF_LAYOUT is None:
167-
from bids import __version__
168-
raise RuntimeError(f"A layout with PyBIDS <{__version__}> could not be initiated")
169-
170161
tf_home = Path(TF_LAYOUT.root)
171162
filepath = tf_home / ("tpl-%s" % template) / "template_description.json"
172163

templateflow/conf/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from warnings import warn
44
from pathlib import Path
55
from contextlib import suppress
6+
from functools import wraps
67

78
TF_DEFAULT_HOME = Path.home() / ".cache" / "templateflow"
89
TF_HOME = Path(getenv("TEMPLATEFLOW_HOME", str(TF_DEFAULT_HOME)))
@@ -17,6 +18,20 @@
1718
)
1819
TF_CACHED = True
1920

21+
22+
def requires_layout(func):
23+
"""Decorate function to ensure ``TF_LAYOUT`` is correctly initiated."""
24+
@wraps(func)
25+
def wrapper(*args, **kwargs):
26+
from templateflow.conf import TF_LAYOUT
27+
28+
if TF_LAYOUT is None:
29+
from bids import __version__
30+
raise RuntimeError(f"A layout with PyBIDS <{__version__}> could not be initiated")
31+
return func(*args, **kwargs)
32+
return wrapper
33+
34+
2035
if not TF_HOME.exists() or not list(TF_HOME.iterdir()):
2136
TF_CACHED = False
2237
warn(

0 commit comments

Comments
 (0)