1
1
"""Configuration and settings."""
2
+
2
3
import re
3
4
from contextlib import suppress
4
5
from functools import wraps
@@ -43,9 +44,19 @@ def _env_to_bool(envvar: str, default: bool) -> bool:
43
44
TF_CACHED = True
44
45
TF_GET_TIMEOUT = 10
45
46
47
+ if TF_USE_DATALAD :
48
+ try :
49
+ from datalad .api import install
50
+ except ImportError :
51
+ warn ('DataLad is not installed ➔ disabled.' )
52
+ TF_USE_DATALAD = False
53
+
54
+ if not TF_USE_DATALAD :
55
+ from templateflow .conf ._s3 import update as _update_s3
56
+
46
57
47
58
def _init_cache ():
48
- global TF_HOME , TF_CACHED , TF_USE_DATALAD
59
+ global TF_CACHED
49
60
50
61
if not TF_HOME .exists () or not list (TF_HOME .iterdir ()):
51
62
TF_CACHED = False
@@ -58,17 +69,9 @@ def _init_cache():
58
69
stacklevel = 2 ,
59
70
)
60
71
if TF_USE_DATALAD :
61
- try :
62
- from datalad .api import install
63
- except ImportError :
64
- TF_USE_DATALAD = False
65
- else :
66
- TF_HOME .parent .mkdir (exist_ok = True , parents = True )
67
- install (path = str (TF_HOME ), source = TF_GITHUB_SOURCE , recursive = True )
68
-
69
- if not TF_USE_DATALAD :
70
- from ._s3 import update as _update_s3
71
-
72
+ TF_HOME .parent .mkdir (exist_ok = True , parents = True )
73
+ install (path = str (TF_HOME ), source = TF_GITHUB_SOURCE , recursive = True )
74
+ else :
72
75
_update_s3 (TF_HOME , local = True , overwrite = TF_AUTOUPDATE , silent = True )
73
76
74
77
@@ -85,18 +88,16 @@ def wrapper(*args, **kwargs):
85
88
if TF_LAYOUT is None :
86
89
from bids import __version__
87
90
88
- raise RuntimeError (
89
- f'A layout with PyBIDS <{ __version__ } > could not be initiated'
90
- )
91
+ raise RuntimeError (f'A layout with PyBIDS <{ __version__ } > could not be initiated' )
91
92
return func (* args , ** kwargs )
92
93
93
94
return wrapper
94
95
95
96
96
97
def update (local = False , overwrite = True , silent = False ):
97
98
"""Update an existing DataLad or S3 home."""
98
- if TF_USE_DATALAD and _update_datalad () :
99
- success = True
99
+ if TF_USE_DATALAD :
100
+ success = _update_datalad ()
100
101
else :
101
102
from ._s3 import update as _update_s3
102
103
@@ -116,7 +117,6 @@ def update(local=False, overwrite=True, silent=False):
116
117
117
118
def wipe ():
118
119
"""Clear the cache if functioning in S3 mode."""
119
- global TF_USE_DATALAD , TF_HOME
120
120
121
121
if TF_USE_DATALAD :
122
122
print ('TemplateFlow is configured in DataLad mode, wipe() has no effect' )
@@ -131,9 +131,7 @@ def _onerror(func, path, excinfo):
131
131
from pathlib import Path
132
132
133
133
if Path (path ).exists ():
134
- print (
135
- f'Warning: could not delete <{ path } >, please clear the cache manually.'
136
- )
134
+ print (f'Warning: could not delete <{ path } >, please clear the cache manually.' )
137
135
138
136
rmtree (TF_HOME , onerror = _onerror )
139
137
_init_cache ()
@@ -191,12 +189,6 @@ def init_layout():
191
189
),
192
190
)
193
191
194
- def _preload ():
195
- global TF_HOME , TF_USE_DATALAD , TF_AUTOUPDATE
196
- TF_HOME = Path (getenv ('TEMPLATEFLOW_HOME' , str (TF_DEFAULT_HOME ))).absolute ()
197
- TF_USE_DATALAD = _env_to_bool ('TEMPLATEFLOW_USE_DATALAD' , False )
198
- TF_AUTOUPDATE = _env_to_bool ('TEMPLATEFLOW_AUTOUPDATE' , True )
199
-
200
192
201
193
with suppress (ImportError ):
202
194
init_layout ()
0 commit comments