|
| 1 | +# Copyright 2025 Google LLC |
| 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 | +"""Utilities for environment variable handling. |
| 16 | +
|
| 17 | +This module is for ADK internal use only. |
| 18 | +Please do not rely on the implementation details. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import os |
| 24 | + |
| 25 | + |
| 26 | +def is_env_enabled(env_var_name: str, default: str = '0') -> bool: |
| 27 | + """Check if an environment variable is enabled. |
| 28 | +
|
| 29 | + An environment variable is considered enabled if its value (case-insensitive) |
| 30 | + is 'true' or '1'. |
| 31 | +
|
| 32 | + Args: |
| 33 | + env_var_name: The name of the environment variable to check. |
| 34 | + default: The default value to use if the environment variable is not set. |
| 35 | + Defaults to '0'. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + True if the environment variable is enabled, False otherwise. |
| 39 | +
|
| 40 | + Examples: |
| 41 | + >>> os.environ['MY_FLAG'] = 'true' |
| 42 | + >>> is_env_enabled('MY_FLAG') |
| 43 | + True |
| 44 | +
|
| 45 | + >>> os.environ['MY_FLAG'] = '1' |
| 46 | + >>> is_env_enabled('MY_FLAG') |
| 47 | + True |
| 48 | +
|
| 49 | + >>> os.environ['MY_FLAG'] = 'false' |
| 50 | + >>> is_env_enabled('MY_FLAG') |
| 51 | + False |
| 52 | +
|
| 53 | + >>> is_env_enabled('NONEXISTENT_FLAG') |
| 54 | + False |
| 55 | +
|
| 56 | + >>> is_env_enabled('NONEXISTENT_FLAG', default='1') |
| 57 | + True |
| 58 | + """ |
| 59 | + return os.environ.get(env_var_name, default).lower() in ['true', '1'] |
0 commit comments