ThalamOS
a powerful Flask web application designed to enhance your storage management.
Loading...
Searching...
No Matches
config_manager.py
Go to the documentation of this file.
1"""
2This module provides functionality to read and load environment variables from a .env file.
3
4Functions:
5 get_env_variables(env_path: str) -> dict:
6 get_env() -> dict:
7 Loads environment variables from a .env file and returns them as a dictionary.
8"""
9from typing import Annotated
10import os
11from dotenv import load_dotenv # pylint: disable=import-error
12import pytest # pylint: disable=import-error
13
14from logger_config import logger
15
16
18 env_path: str,
19) -> Annotated[dict, "dictionary of environment variables"]:
20 """
21 Reads environment variables from a file and returns them as a dictionary.
22 Args:
23 env_path (str): The path to the environment variables file.
24 Returns:
25 dict: A dictionary containing the environment variables as key-value pairs.
26 """
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}")
31 return env_dict
32
33
34def get_env() -> Annotated[dict, "dictionary of environment variables"]:
35 """
36 Loads environment variables from a .env file and returns
37 them as a dictionary.
38 Returns:
39 dict: A dictionary containing the environment variables.
40 """
41 env_path = os.path.join(os.path.dirname(__file__), "data/.env")
42 load_dotenv(dotenv_path=env_path)
43 env_dict = get_env_variables_from_path(env_path)
44 return env_dict
45
46
47if __name__ == "__main__":
48 print(get_env())
49
50
51# Tests
52
53
55 """
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
60 key-value pairs.
61 Args:
62 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
63 Raises:
64 AssertionError: If the returned dictionary does not match the expected key-value pairs.
65 """
66
67 # Create a temporary .env file
68 env_file = tmp_path / ".env"
69 env_file.write_text('KEY1="value1"\nKEY2="value2"\n')
70
71 # Call the function with the path to the temporary .env file
72 env_vars = get_env_variables_from_path(str(env_file))
73
74 # Assert that the returned dictionary contains the expected key-value pairs
75 assert env_vars == {"KEY1": "value1", "KEY2": "value2"}
76
77
79 """
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.
84 Args:
85 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
86 """
87
88 # Create an empty temporary .env file
89 env_file = tmp_path / ".env"
90 env_file.write_text("")
91
92 # Call the function with the path to the empty .env file
93 env_vars = get_env_variables_from_path(str(env_file))
94
95 # Assert that the returned dictionary is empty
96 assert env_vars == {}
97
98
100 """
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.
104 Args:
105 tmp_path (pathlib.Path): Temporary directory provided by pytest to create the .env file.
106 Raises:
107 IndexError: Expected exception when the .env file contains an invalid format.
108 """
109
110 # Create a temporary .env file with invalid format
111 env_file = tmp_path / ".env"
112 env_file.write_text('KEY1="value1"\nINVALID_LINE\nKEY2="value2"\n')
113
114 # Call the function with the path to the temporary .env file
115 with pytest.raises(IndexError):
116 get_env_variables_from_path(str(env_file))
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()