Skip to content

Commit d225254

Browse files
committed
RF: Move autoupdate check to configuration module
+ Adds function to handle envvar parsing
1 parent 7ede8f2 commit d225254

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

templateflow/__init__.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,8 @@
3737
del version
3838
del PackageNotFoundError
3939

40-
import os
41-
4240
from . import api
43-
from .conf import TF_USE_DATALAD, update
44-
45-
if not TF_USE_DATALAD and os.getenv('TEMPLATEFLOW_AUTOUPDATE', '1') not in (
46-
'false',
47-
'off',
48-
'0',
49-
'no',
50-
'n',
51-
):
52-
# trigger skeleton autoupdate
53-
update(local=True, overwrite=False, silent=True)
41+
from .conf import update
5442

5543
__all__ = [
5644
'__copyright__',

templateflow/conf/__init__.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,35 @@
1010

1111
load_data = Loader(__package__)
1212

13+
def _env_to_bool(envvar: str, default: bool) -> bool:
14+
"""Check for environment variable switches and convert to booleans."""
15+
switches = {
16+
'on': {'true', 'on', '1', 'yes', 'y'},
17+
'off': {'false', 'off', '0', 'no', 'n'},
18+
}
19+
20+
val = getenv(envvar, default)
21+
if isinstance(val, str):
22+
if val.lower() in switches['on']:
23+
return True
24+
elif val.lower() in switches['off']:
25+
return False
26+
else:
27+
# TODO: Create templateflow logger
28+
print(
29+
f'{envvar} is set to unknown value <{val}>. '
30+
f'Falling back to default value <{default}>'
31+
)
32+
return default
33+
return val
34+
35+
1336
TF_DEFAULT_HOME = Path.home() / '.cache' / 'templateflow'
1437
TF_HOME = Path(getenv('TEMPLATEFLOW_HOME', str(TF_DEFAULT_HOME)))
1538
TF_GITHUB_SOURCE = 'https://github.com/templateflow/templateflow.git'
1639
TF_S3_ROOT = 'https://templateflow.s3.amazonaws.com'
17-
TF_USE_DATALAD = getenv('TEMPLATEFLOW_USE_DATALAD', 'false').lower() in (
18-
'true',
19-
'on',
20-
'1',
21-
'yes',
22-
'y',
23-
)
40+
TF_USE_DATALAD = _env_to_bool('TEMPLATEFLOW_USE_DATALAD', False)
41+
TF_AUTOUPDATE = _env_to_bool('TEMPLATEFLOW_AUTOUPDATE', True)
2442
TF_CACHED = True
2543
TF_GET_TIMEOUT = 10
2644

@@ -50,7 +68,7 @@ def _init_cache():
5068
if not TF_USE_DATALAD:
5169
from ._s3 import update as _update_s3
5270

53-
_update_s3(TF_HOME, local=True, overwrite=True)
71+
_update_s3(TF_HOME, local=True, overwrite=TF_AUTOUPDATE, silent=True)
5472

5573

5674
_init_cache()

0 commit comments

Comments
 (0)