2This module provides functionality to read and load environment variables from a .env file.
5 get_env_variables(env_path: str) -> dict:
7 Loads environment variables from a .env file and returns them as a dictionary.
9from typing
import Annotated
11from dotenv
import load_dotenv
14from logger_config
import logger
19) -> Annotated[dict,
"dictionary of environment variables"]:
21 Reads environment variables from a file and returns them as a dictionary.
23 env_path (str): The path to the environment variables file.
25 dict: A dictionary containing the environment variables as key-value pairs.
29 with open(env_path, encoding=
"utf-8")
as f:
32 if not line
or line.startswith(
"#"):
35 key, value = line.split(
"=", 1)
36 env_dict[key.strip()] = value.strip().strip(
'"').strip(
"'")
38 logger.debug(f
"Environment variables loaded: {env_dict}")
39 except FileNotFoundError:
40 logger.error(f
"Config-File not found: {env_path}")
45def get_env() -> Annotated[dict, "dictionary of environment variables"]:
47 Loads environment variables from a .env file and returns
50 dict: A dictionary containing the environment variables.
52 env_path = os.path.join(os.path.dirname(__file__),
"data/.env")
53 load_dotenv(dotenv_path=env_path)
58if __name__ ==
"__main__":
67 Test the get_env_variables_from_path function.
68 This test creates a temporary .env file with predefined key-value pairs,
69 calls the get_env_variables_from_path function with the path to the temporary
70 .env file, and asserts that the returned dictionary contains the expected
73 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
75 AssertionError: If the returned dictionary does not match the expected key-value pairs.
79 env_file = tmp_path /
".env"
80 env_file.write_text(
'KEY1="value1"\nKEY2="value2"\n')
86 assert env_vars == {
"KEY1":
"value1",
"KEY2":
"value2"}
91 Test case for get_env_variables_from_path function with an empty .env file.
92 This test creates an empty temporary .env file and calls the
93 get_env_variables_from_path function with the path to this file.
94 It then asserts that the returned dictionary is empty.
96 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
100 env_file = tmp_path /
".env"
101 env_file.write_text(
"")
112 Test case for get_env_variables_from_path function to handle invalid format in .env file.
113 This test creates a temporary .env file with an invalid format and verifies that the
114 get_env_variables_from_path function raises an IndexError when attempting to parse it.
116 tmp_path (pathlib.Path): Temporary directory provided by pytest to create the .env file.
118 IndexError: Expected exception when the .env file contains an invalid format.
122 env_file = tmp_path /
".env"
123 env_file.write_text(
'KEY1="value1"\nINVALID_LINE\nKEY2="value2"\n')
126 with pytest.raises(IndexError):
test_get_env_variables_from_path_empty_file(tmp_path)
test_get_env_variables_from_path(tmp_path)
test_get_env_variables_from_path_invalid_format(tmp_path)
Annotated[dict, "dictionary of environment variables"] get_env_variables_from_path(str env_path)
Annotated[dict, "dictionary of environment variables"] get_env()