Skip to content

Commit 6872aea

Browse files
committed
ENH: Allow setting default behaviors with environment
Allow using environmental variables to overide default behaviors. These environmental variables provide a convenient mechanism for testing various default behaviors of the python infrastructure by only changing the environmental variables. To test both with and without LazyLoading: export ITK_PYTHON_LAZYLOADING=ON ; ctest -j 8 -R Python export ITK_PYTHON_LAZYLOADING=OFF; ctest -j 8 -R Python
1 parent 14873a2 commit 6872aea

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

Wrapping/Generators/Python/itkConfig.py.in

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,35 @@ ERROR: int = 2
4242
DebugLevel: int = WARN
4343
ImportCallback = None
4444
ProgressCallback = None
45-
LazyLoading: bool = True
46-
NotInPlace: bool = False
45+
46+
47+
def _get_environment_boolean(environment_var: str, default_string: str) -> bool:
48+
# Use defaults if not available as environmental overrides
49+
# True values are y, yes, t, true, on and 1;
50+
# False values are n, no, f, false, off and 0.
51+
# Raises ValueError if val is anything else.
52+
from os import environ as _environ
53+
from distutils.util import strtobool as _strtobool
54+
55+
try:
56+
_StringDefault: str = _environ.get(environment_var, default_string)
57+
return bool(_strtobool(_StringDefault))
58+
except ValueError:
59+
print(
60+
f"{environment_var} environment variable has invalid value {_StringDefault}"
61+
)
62+
print(
63+
" Valid True values are (case insensitive): 'y', 'yes', 't', 'true', 'on', and '1'"
64+
)
65+
print(
66+
" Valid False values are (case insensitive): 'n', 'no', 'f', 'false', 'off', and '0'"
67+
)
68+
return bool(_strtobool(default_string))
69+
70+
71+
LazyLoading: bool = _get_environment_boolean("ITK_PYTHON_LAZYLOADING", "True")
72+
NotInPlace: bool = _get_environment_boolean("ITK_PYTHON_NOTINPLACE", "False")
73+
del _get_environment_boolean
4774

4875
# Internal settings
4976

@@ -96,6 +123,4 @@ ITK_GLOBAL_VERSION_STRING: str = "@ITK_VERSION_MAJOR@.@ITK_VERSION_MINOR@.@ITK_V
96123

97124
(swig_lib, swig_py, config_py, doxygen_root, path) = _initialize()
98125
del _initialize
99-
del _normalized_path
100-
del os
101126
del warnings

0 commit comments

Comments
 (0)