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.
27 with open(env_path, encoding=
"utf-8")
as f:
28 env_keys = f.read().splitlines()
29 env_dict = {item.split(
"=")[0]: item.split(
"=")[1].strip(
'"')
for item
in env_keys}
30 logger.debug(f
"Environment variables loaded: {env_dict}")
34def get_env() -> Annotated[dict, "dictionary of environment variables"]:
36 Loads environment variables from a .env file and returns
39 dict: A dictionary containing the environment variables.
41 env_path = os.path.join(os.path.dirname(__file__),
"data/.env")
42 load_dotenv(dotenv_path=env_path)
47if __name__ ==
"__main__":
56 Test the get_env_variables_from_path function.
57 This test creates a temporary .env file with predefined key-value pairs,
58 calls the get_env_variables_from_path function with the path to the temporary
59 .env file, and asserts that the returned dictionary contains the expected
62 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
64 AssertionError: If the returned dictionary does not match the expected key-value pairs.
68 env_file = tmp_path /
".env"
69 env_file.write_text(
'KEY1="value1"\nKEY2="value2"\n')
75 assert env_vars == {
"KEY1":
"value1",
"KEY2":
"value2"}
80 Test case for get_env_variables_from_path function with an empty .env file.
81 This test creates an empty temporary .env file and calls the
82 get_env_variables_from_path function with the path to this file.
83 It then asserts that the returned dictionary is empty.
85 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
89 env_file = tmp_path /
".env"
90 env_file.write_text(
"")
101 Test case for get_env_variables_from_path function to handle invalid format in .env file.
102 This test creates a temporary .env file with an invalid format and verifies that the
103 get_env_variables_from_path function raises an IndexError when attempting to parse it.
105 tmp_path (pathlib.Path): Temporary directory provided by pytest to create the .env file.
107 IndexError: Expected exception when the .env file contains an invalid format.
111 env_file = tmp_path /
".env"
112 env_file.write_text(
'KEY1="value1"\nINVALID_LINE\nKEY2="value2"\n')
115 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()