11"""Configuration and settings."""
2+
23import re
34from contextlib import suppress
45from functools import wraps
@@ -43,9 +44,19 @@ def _env_to_bool(envvar: str, default: bool) -> bool:
4344TF_CACHED = True
4445TF_GET_TIMEOUT = 10
4546
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+
4657
4758def _init_cache ():
48- global TF_HOME , TF_CACHED , TF_USE_DATALAD
59+ global TF_CACHED
4960
5061 if not TF_HOME .exists () or not list (TF_HOME .iterdir ()):
5162 TF_CACHED = False
@@ -58,17 +69,9 @@ def _init_cache():
5869 stacklevel = 2 ,
5970 )
6071 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 :
7275 _update_s3 (TF_HOME , local = True , overwrite = TF_AUTOUPDATE , silent = True )
7376
7477
@@ -85,18 +88,16 @@ def wrapper(*args, **kwargs):
8588 if TF_LAYOUT is None :
8689 from bids import __version__
8790
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' )
9192 return func (* args , ** kwargs )
9293
9394 return wrapper
9495
9596
9697def update (local = False , overwrite = True , silent = False ):
9798 """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 ()
100101 else :
101102 from ._s3 import update as _update_s3
102103
@@ -116,7 +117,6 @@ def update(local=False, overwrite=True, silent=False):
116117
117118def wipe ():
118119 """Clear the cache if functioning in S3 mode."""
119- global TF_USE_DATALAD , TF_HOME
120120
121121 if TF_USE_DATALAD :
122122 print ('TemplateFlow is configured in DataLad mode, wipe() has no effect' )
@@ -131,9 +131,7 @@ def _onerror(func, path, excinfo):
131131 from pathlib import Path
132132
133133 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.' )
137135
138136 rmtree (TF_HOME , onerror = _onerror )
139137 _init_cache ()
@@ -191,12 +189,6 @@ def init_layout():
191189 ),
192190 )
193191
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-
200192
201193with suppress (ImportError ):
202194 init_layout ()
0 commit comments