vvp -- VNF Validation Platform
[oom.git] / kubernetes / vvp / charts / vvp-em-uwsgi / resources / config / em / envbool.py
1 # Copyright © 2018 Amdocs, AT&T, Bell Canada
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 envbool.py
17
18 Return which environment is currently running on (to setting.py).
19
20 """
21 import os
22
23
24 def envbool(key, default=False, unknown=True):
25     """Return a boolean value based on that of an environment variable.
26
27     Environment variables have no native boolean type. They are always strings, and may be empty or
28     unset (which differs from empty.) Furthermore, notions of what is "truthy" in shell script
29     differ from that of python.
30
31     This function converts environment variables to python boolean True or False in
32     case-insensitive, expected ways to avoid pitfalls:
33
34         "True", "true", and "1" become True
35         "False", "false", and "0" become False
36         unset or empty becomes False by default (toggle with 'default' parameter.)
37         any other value becomes True by default (toggle with 'unknown' parameter.)
38
39     """
40     return {
41         'true': True, '1': True,  # 't': True,
42         'false': False, '0': False,  # 'f': False.
43         '': default,
44     }.get(os.getenv(key, '').lower(), unknown)