env_bool.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. # coding:utf-8
  2. #!/usr/bin/python
  3. #
  4. # Copyright (c) Contributors to the Open 3D Engine Project.
  5. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. #
  7. # SPDX-License-Identifier: Apache-2.0 OR MIT
  8. #
  9. #
  10. # -- This line is 75 characters -------------------------------------------
  11. import os
  12. # -- envar util ----------------------------------------------------------
  13. # not putting this in the env_util.py to reduce cyclical importing
  14. def env_bool(envar, default=False):
  15. """! cast a env bool to a python bool
  16. :@param envar: str
  17. the envar key
  18. :@param default: bool
  19. the default value if not set"""
  20. envar_test = os.getenv(envar, default)
  21. # check 'False', 'false', and '0' since all are non-empty
  22. # env comes back as string and normally coerced to True.
  23. if envar_test in ('True', 'true', '1'):
  24. return True
  25. elif envar_test in ('False', 'false', '0'):
  26. return False
  27. else:
  28. return envar_test
  29. # -------------------------------------------------------------------------