Skip to content

Commit 48ab265

Browse files
committed
fix: robuster module import
1 parent 2584aa6 commit 48ab265

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

templateflow/conf/__init__.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Configuration and settings."""
2+
23
import re
34
from contextlib import suppress
45
from functools import wraps
@@ -43,9 +44,19 @@ def _env_to_bool(envvar: str, default: bool) -> bool:
4344
TF_CACHED = True
4445
TF_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

4758
def _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

9697
def 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

117118
def 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

201193
with suppress(ImportError):
202194
init_layout()

templateflow/tests/test_conf.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
# https://www.nipreps.org/community/licensing/
2222
#
2323
"""Tests the config module."""
24-
from pathlib import Path
24+
2525
from importlib import reload
2626
from shutil import rmtree
2727

2828
import pytest
2929

30-
from templateflow import api
3130
from templateflow import conf as tfc
3231

3332

@@ -40,6 +39,7 @@ def _find_message(lines, msg, reverse=True):
4039
return True
4140
return False
4241

42+
4343
@pytest.mark.parametrize('use_datalad', ['off', 'on'])
4444
def test_conf_init(monkeypatch, tmp_path, use_datalad):
4545
"""Check the correct functioning of config set-up."""
@@ -61,7 +61,9 @@ def test_conf_init(monkeypatch, tmp_path, use_datalad):
6161
def test_setup_home(monkeypatch, tmp_path, capsys, use_datalad):
6262
"""Check the correct functioning of the installation hook."""
6363

64-
use_pre = tfc._env_to_bool('TEMPLATEFLOW_USE_DATALAD', False)
64+
if use_datalad == 'on':
65+
# ImportError if not installed
66+
pass
6567

6668
home = (tmp_path / f'setup-home-{use_datalad}').absolute()
6769
monkeypatch.setenv('TEMPLATEFLOW_USE_DATALAD', use_datalad)
@@ -70,7 +72,6 @@ def test_setup_home(monkeypatch, tmp_path, capsys, use_datalad):
7072
use_post = tfc._env_to_bool('TEMPLATEFLOW_USE_DATALAD', False)
7173
assert use_post is (use_datalad == 'on')
7274

73-
tfc._preload()
7475
with capsys.disabled():
7576
reload(tfc)
7677

0 commit comments

Comments
 (0)