Source code for restapi.env

"""
Utilities to work with environment variables
"""

import os
from functools import lru_cache
from typing import Union


[docs] class Env:
[docs] @staticmethod @lru_cache def get(var: str, default: str) -> str: return os.getenv(var, default)
[docs] @staticmethod @lru_cache def get_bool(var: str, default: bool = False) -> bool: return Env.to_bool(Env.get(var, ""), default)
[docs] @staticmethod @lru_cache def get_int(var: str, default: int = 0) -> int: return Env.to_int(Env.get(var, ""), default)
[docs] @staticmethod @lru_cache def to_bool(var: Union[None, str, bool], default: bool = False) -> bool: if var is None or var == "": return default if isinstance(var, bool): return var # if not directly a bool, try an interpretation # INTEGERS try: tmp = int(var) return bool(tmp) except (TypeError, ValueError): pass # STRINGS if isinstance(var, str): # false / False / FALSE if var.lower() == "false": return False # any non empty string has to be considered True if len(var) > 0: return True return default
[docs] @staticmethod @lru_cache def to_int(var: Union[None, str, int], default: int = 0) -> int: if var is None or var == "": return default if isinstance(var, int): return var try: return int(var) except ValueError: pass return default
[docs] @staticmethod def load_variables_group(prefix: str) -> dict[str, str]: prefix += "_" variables: dict[str, str] = {} for var, value in os.environ.items(): var = var.lower() if not var.startswith(prefix): continue # Fix key and value before saving key = var.removeprefix(prefix) # One thing that we must avoid is any quote around our value value = value.strip('"').strip("'") # save variables[key] = value return variables