Skip to content

Commit

Permalink
RF: Move autoupdate check to configuration module
Browse files Browse the repository at this point in the history
+ Adds function to handle envvar parsing
  • Loading branch information
mgxd committed Mar 19, 2024
1 parent 7ede8f2 commit d225254
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
14 changes: 1 addition & 13 deletions templateflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,8 @@
del version
del PackageNotFoundError

import os

from . import api
from .conf import TF_USE_DATALAD, update

if not TF_USE_DATALAD and os.getenv('TEMPLATEFLOW_AUTOUPDATE', '1') not in (
'false',
'off',
'0',
'no',
'n',
):
# trigger skeleton autoupdate
update(local=True, overwrite=False, silent=True)
from .conf import update

__all__ = [
'__copyright__',
Expand Down
34 changes: 26 additions & 8 deletions templateflow/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,35 @@

load_data = Loader(__package__)

def _env_to_bool(envvar: str, default: bool) -> bool:
"""Check for environment variable switches and convert to booleans."""
switches = {
'on': {'true', 'on', '1', 'yes', 'y'},
'off': {'false', 'off', '0', 'no', 'n'},
}

val = getenv(envvar, default)
if isinstance(val, str):
if val.lower() in switches['on']:
return True
elif val.lower() in switches['off']:
return False
else:
# TODO: Create templateflow logger
print(
f'{envvar} is set to unknown value <{val}>. '
f'Falling back to default value <{default}>'
)
return default
return val


TF_DEFAULT_HOME = Path.home() / '.cache' / 'templateflow'
TF_HOME = Path(getenv('TEMPLATEFLOW_HOME', str(TF_DEFAULT_HOME)))
TF_GITHUB_SOURCE = 'https://github.com/templateflow/templateflow.git'
TF_S3_ROOT = 'https://templateflow.s3.amazonaws.com'
TF_USE_DATALAD = getenv('TEMPLATEFLOW_USE_DATALAD', 'false').lower() in (
'true',
'on',
'1',
'yes',
'y',
)
TF_USE_DATALAD = _env_to_bool('TEMPLATEFLOW_USE_DATALAD', False)
TF_AUTOUPDATE = _env_to_bool('TEMPLATEFLOW_AUTOUPDATE', True)
TF_CACHED = True
TF_GET_TIMEOUT = 10

Expand Down Expand Up @@ -50,7 +68,7 @@ def _init_cache():
if not TF_USE_DATALAD:
from ._s3 import update as _update_s3

_update_s3(TF_HOME, local=True, overwrite=True)
_update_s3(TF_HOME, local=True, overwrite=TF_AUTOUPDATE, silent=True)


_init_cache()
Expand Down

0 comments on commit d225254

Please sign in to comment.