-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (42 loc) · 1.57 KB
/
utils.py
File metadata and controls
50 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Utitlies Module."""
import os
import logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def get_configs(config_name, strict=False, default_value=None):
"""
Retrieves the value of a configuration from the environment variables.
Args:
config_name (str): The name of the configuration to retrieve.
strict (bool): If True, raises an error if the configuration
is not found. Default is False.
default_value (str): The default value to return if the configuration
is not found and strict is False. Default is None.
Returns:
str: The value of the configuration, or default_value if not found and s
trict is False.
Raises:
KeyError: If the configuration is not found and strict is True.
ValueError: If the configuration value is empty and strict is True.
"""
try:
value = (
os.environ[config_name]
if strict
else os.environ.get(config_name) or default_value
)
if strict and (value is None or value.strip() == ""):
raise ValueError(f"Configuration '{config_name}' is missing or empty.")
return value
except KeyError as error:
logger.error(
"Configuration '%s' not found in environment variables: %s",
config_name,
error,
)
raise
except ValueError as error:
logger.error("Configuration '%s' is empty: %s", config_name, error)
raise