33 lines
968 B
Python
33 lines
968 B
Python
import os
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
|
|
def get_config(value, env, default, cast=None):
|
|
"""
|
|
Small utility to get a configuration value, returns `value` if it is not None, else it will try to get the
|
|
environment variable cast with `cast`. If the environment variable is not set, it will return `default`.
|
|
|
|
:param value: The value to check.
|
|
:type value: Any
|
|
:param env: The environment variable to check.
|
|
:type env: string
|
|
:param default: The default value to return if the environment variable is not set.
|
|
:type default: Any
|
|
:param cast: A function to use to cast the environment variable. Must support string input.
|
|
:type cast: Callable[[Any], Any], optional
|
|
|
|
:return: The value, the environment variable value, or the default.
|
|
:rtype: Any
|
|
"""
|
|
if value is not None:
|
|
return value
|
|
|
|
env = os.environ.get(env, default)
|
|
|
|
if cast is None:
|
|
return env
|
|
|
|
return cast(env)
|