Skip to content

Commit f097d72

Browse files
committed
rf: Warn on all failed attempts to configure use_datalad
1 parent c9e2767 commit f097d72

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

templateflow/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from click.decorators import FC, Option, _param_memo
3333

3434
from templateflow.client import TemplateFlowClient
35+
from templateflow.conf import _cache
3536

3637
load_data = _Loader(__spec__.parent)
3738

@@ -48,7 +49,7 @@
4849
}
4950
ENTITY_EXCLUDE = {'template', 'description'}
5051

51-
CLIENT = TemplateFlowClient()
52+
CLIENT = TemplateFlowClient(cache=_cache)
5253
CACHE = CLIENT.cache
5354
CONFIG = CACHE.config
5455
CACHE.ensure()

templateflow/conf/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from acres import Loader
77

88
from .cache import CacheConfig, TemplateFlowCache
9-
from .env import _env_to_bool
109

1110
load_data = Loader(__spec__.name)
1211

@@ -33,10 +32,6 @@ def __getattr__(name: str):
3332
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
3433

3534

36-
if _env_to_bool('TEMPLATEFLOW_USE_DATALAD', False) and not _cache.config.use_datalad:
37-
warn('DataLad is not installed ➔ disabled.', stacklevel=2)
38-
39-
4035
if not _cache.precached:
4136
warn(
4237
f"""\

templateflow/conf/cache.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass, field
4-
from functools import cached_property
4+
from functools import cache, cached_property
55
from pathlib import Path
66
from warnings import warn
77

@@ -12,6 +12,19 @@
1212
from bids.layout import BIDSLayout
1313

1414

15+
# The first CacheConfig is initialized during import, so we need a higher
16+
# level of indirection for warnings to point to the user code.
17+
# After that, we will set the stack level to point to the CacheConfig() caller.
18+
STACKLEVEL = 6
19+
20+
21+
@cache
22+
def _have_datalad():
23+
import importlib.util
24+
25+
return importlib.util.find_spec('datalad') is not None
26+
27+
1528
@dataclass
1629
class CacheConfig:
1730
root: Path = field(default_factory=get_templateflow_home)
@@ -22,10 +35,11 @@ class CacheConfig:
2235
timeout: int = field(default=10)
2336

2437
def __post_init__(self):
25-
if self.use_datalad:
26-
from importlib.util import find_spec
27-
28-
self.use_datalad = find_spec('datalad') is not None
38+
global STACKLEVEL
39+
if self.use_datalad and not _have_datalad():
40+
self.use_datalad = False
41+
warn('DataLad is not installed ➔ disabled.', stacklevel=STACKLEVEL)
42+
STACKLEVEL = 3
2943

3044

3145
@dataclass

templateflow/tests/test_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_setup_home(monkeypatch, tmp_path, capsys, use_datalad):
6868
monkeypatch.setenv('TEMPLATEFLOW_USE_DATALAD', use_datalad)
6969
monkeypatch.setenv('TEMPLATEFLOW_HOME', str(home))
7070

71-
use_post = tfc._env_to_bool('TEMPLATEFLOW_USE_DATALAD', False)
71+
use_post = tfc.env._env_to_bool('TEMPLATEFLOW_USE_DATALAD', False)
7272
assert use_post is (use_datalad == 'on')
7373

7474
with capsys.disabled():

0 commit comments

Comments
 (0)