Skip to content

Commit 850632c

Browse files
committed
rf: Warn on all failed attempts to configure use_datalad
1 parent 799d386 commit 850632c

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

templateflow/cli.py

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

34+
from templateflow.conf import _cache
3435
from templateflow.client import TemplateFlowClient
3536

3637
load_data = _Loader(__spec__.parent)
@@ -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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ def __getattr__(name: str):
3333
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
3434

3535

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-
4036
if not _cache.precached:
4137
warn(
4238
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

0 commit comments

Comments
 (0)