feat: add environment variables and docs
ref: N25B-352
This commit is contained in:
32
src/robot_interface/utils/get_config.py
Normal file
32
src/robot_interface/utils/get_config.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user