ThalamOS
a powerful Flask web application designed to enhance your storage management.
Loading...
Searching...
No Matches
config_manager Namespace Reference

Functions

Annotated[dict, "dictionary of environment variables"] get_env_variables_from_path (str env_path)
 
Annotated[dict, "dictionary of environment variables"] get_env ()
 
 test_get_env_variables_from_path (tmp_path)
 
 test_get_env_variables_from_path_empty_file (tmp_path)
 
 test_get_env_variables_from_path_invalid_format (tmp_path)
 

Detailed Description

This module provides functionality to read and load environment variables from a .env file.

Functions:
    get_env_variables(env_path: str) -> dict:
    get_env() -> dict:
        Loads environment variables from a .env file and returns them as a dictionary.

Function Documentation

◆ get_env()

Annotated[dict, "dictionary of environment variables"] config_manager.get_env ( )
Loads environment variables from a .env file and returns
them as a dictionary.
Returns:
    dict: A dictionary containing the environment variables.

Definition at line 45 of file config_manager.py.

45def get_env() -> Annotated[dict, "dictionary of environment variables"]:
46 """
47 Loads environment variables from a .env file and returns
48 them as a dictionary.
49 Returns:
50 dict: A dictionary containing the environment variables.
51 """
52 env_path = os.path.join(os.path.dirname(__file__), "data/.env")
53 load_dotenv(dotenv_path=env_path)
54 env_dict = get_env_variables_from_path(env_path)
55 return env_dict
56
57

◆ get_env_variables_from_path()

Annotated[dict, "dictionary of environment variables"] config_manager.get_env_variables_from_path ( str env_path)
Reads environment variables from a file and returns them as a dictionary.
Args:
    env_path (str): The path to the environment variables file.
Returns:
    dict: A dictionary containing the environment variables as key-value pairs.

Definition at line 17 of file config_manager.py.

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 env_dict = {}
28 try:
29 with open(env_path, encoding="utf-8") as f:
30 for line in f:
31 line = line.strip()
32 if not line or line.startswith("#"):
33 continue
34 if "=" in line:
35 key, value = line.split("=", 1) # Nur am ersten '=' splitten
36 env_dict[key.strip()] = value.strip().strip('"').strip("'")
37
38 logger.debug(f"Environment variables loaded: {env_dict}")
39 except FileNotFoundError:
40 logger.error(f"Config-File not found: {env_path}")
41
42 return env_dict
43
44

◆ test_get_env_variables_from_path()

config_manager.test_get_env_variables_from_path ( tmp_path)
Test the get_env_variables_from_path function.
This test creates a temporary .env file with predefined key-value pairs,
calls the get_env_variables_from_path function with the path to the temporary
.env file, and asserts that the returned dictionary contains the expected
key-value pairs.
Args:
    tmp_path (pathlib.Path): A temporary directory path provided by pytest.
Raises:
    AssertionError: If the returned dictionary does not match the expected key-value pairs.

Definition at line 65 of file config_manager.py.

65def test_get_env_variables_from_path(tmp_path):
66 """
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
71 key-value pairs.
72 Args:
73 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
74 Raises:
75 AssertionError: If the returned dictionary does not match the expected key-value pairs.
76 """
77
78 # Create a temporary .env file
79 env_file = tmp_path / ".env"
80 env_file.write_text('KEY1="value1"\nKEY2="value2"\n')
81
82 # Call the function with the path to the temporary .env file
83 env_vars = get_env_variables_from_path(str(env_file))
84
85 # Assert that the returned dictionary contains the expected key-value pairs
86 assert env_vars == {"KEY1": "value1", "KEY2": "value2"}
87
88

◆ test_get_env_variables_from_path_empty_file()

config_manager.test_get_env_variables_from_path_empty_file ( tmp_path)
Test case for get_env_variables_from_path function with an empty .env file.
This test creates an empty temporary .env file and calls the
get_env_variables_from_path function with the path to this file.
It then asserts that the returned dictionary is empty.
Args:
    tmp_path (pathlib.Path): A temporary directory path provided by pytest.

Definition at line 89 of file config_manager.py.

89def test_get_env_variables_from_path_empty_file(tmp_path):
90 """
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.
95 Args:
96 tmp_path (pathlib.Path): A temporary directory path provided by pytest.
97 """
98
99 # Create an empty temporary .env file
100 env_file = tmp_path / ".env"
101 env_file.write_text("")
102
103 # Call the function with the path to the empty .env file
104 env_vars = get_env_variables_from_path(str(env_file))
105
106 # Assert that the returned dictionary is empty
107 assert not env_vars
108
109

◆ test_get_env_variables_from_path_invalid_format()

config_manager.test_get_env_variables_from_path_invalid_format ( tmp_path)
Test case for get_env_variables_from_path function to handle invalid format in .env file.
This test creates a temporary .env file with an invalid format and verifies that the
get_env_variables_from_path function raises an IndexError when attempting to parse it.
Args:
    tmp_path (pathlib.Path): Temporary directory provided by pytest to create the .env file.
Raises:
    IndexError: Expected exception when the .env file contains an invalid format.

Definition at line 110 of file config_manager.py.

110def test_get_env_variables_from_path_invalid_format(tmp_path):
111 """
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.
115 Args:
116 tmp_path (pathlib.Path): Temporary directory provided by pytest to create the .env file.
117 Raises:
118 IndexError: Expected exception when the .env file contains an invalid format.
119 """
120
121 # Create a temporary .env file with invalid format
122 env_file = tmp_path / ".env"
123 env_file.write_text('KEY1="value1"\nINVALID_LINE\nKEY2="value2"\n')
124
125 # Call the function with the path to the temporary .env file
126 with pytest.raises(IndexError):
127 get_env_variables_from_path(str(env_file))