-
Notifications
You must be signed in to change notification settings - Fork 0
/
newenvreader.py
164 lines (129 loc) · 4.73 KB
/
newenvreader.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
This module provides utility functions for reading environment variables.
"""
import os
from configparser import ConfigParser, MissingSectionHeaderError
from typing import TypeVar, Optional
T = TypeVar("T")
def clean_env_var(value: str) -> str:
"""Clean an environment variable value.
:param value: The value to clean.
:type value: str
:return: The cleaned value.
:rtype: str
"""
if value.startswith(("'")) and value.endswith(("'")) and len(value) > 2:
return value[1:-1]
elif value.startswith(('"')) and value.endswith(('"')) and len(value) > 2:
return value[1:-1]
return value
def parse_env_file(path: str) -> dict[str, str]:
"""Parse a .env file.
:param path: Path to the .env file.
:type path: str
:return: A dictionary of environment variables.
:rtype: dict
"""
env_file_val = {}
with open(path, "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
if not line:
continue
key, value = line.split("=", maxsplit=1)
key = key.strip()
env_file_val[key] = clean_env_var(value.strip())
return env_file_val
def parse_ini_file(path: str) -> dict[str, str]:
"""Parse a .ini file.
:param path: Path to the .ini file.
:type path: str
:return: A dictionary of environment variables.
:rtype: dict
"""
parser = ConfigParser()
parser.optionxform = lambda option: option
with open(path, "r", encoding="utf-8") as file:
try:
parser.read_file(file)
except MissingSectionHeaderError as err:
raise ValueError(
"Invalid .ini file: File contains no settings section"
) from err
# get alls key values from section settings
env_file_val = dict(parser.items("settings"))
return env_file_val
def search_env_file(start_path: str) -> str:
"""Search for a .env file in the current directory and its parent directories.
:param start_path: Start directory to search for the .env file.
:type start_path: str
:raises FileNotFoundError: If no .env file is found.
:return: The path to the .env file.
:rtype: str
"""
current_dir = os.path.abspath(start_path)
for root, _, files in os.walk(current_dir):
for file in files:
# Check if it's file ending with .env or is settings.ini
if file.endswith(".env") or file == "settings.ini":
return os.path.join(root, file)
parent_dir = os.path.dirname(current_dir)
if parent_dir == current_dir:
# Reached the root directory, file not found
raise FileNotFoundError("No .env file found")
return search_env_file(parent_dir)
def cast_bool(value: str) -> bool:
"""Cast a string to a boolean.
:param value: The string to cast.
:type value: str
:return: The boolean value.
:rtype: bool
"""
if not isinstance(value, str):
return bool(value)
if value.lower() in ("yes", "true", "t", "1", "on", "y"):
return True
if value.lower() in ("no", "false", "f", "0", "off", "n", ""):
return False
raise ValueError("Invalid boolean value")
def load_env() -> dict[str, str]:
"""Load environment variables from the .env file or the system environment.
:raises KeyError: If a required environment variable is not found.
:return: A dictionary of environment variables.
:rtype: dict[str, str]
"""
env = {}
for key, value in os.environ.items():
env[key] = value
try:
found_env_path = search_env_file(os.getcwd())
if found_env_path.endswith(".ini"):
env.update(parse_ini_file(found_env_path))
else:
env.update(parse_env_file(found_env_path))
except FileNotFoundError:
pass
return env
loaded_env = load_env()
def get_env(key: str, cast: type[T] = str, default: Optional[T] = None) -> T:
"""Load environment variable from the .env file or the system environment.
:param key: The environment variable key.
:type key: str
:param cast: The type to cast the environment variable to, defaults to str
:type cast: type, optional
:param default: The default value to return if the environment variable is not found, defaults to None
:type default: Optional[T], optional
:raises KeyError: If the environment variable is not found and is required.
:return: The environment variable value.
:rtype: str
"""
try:
val = loaded_env[key]
except KeyError as err:
if default is not None:
val = default
else:
raise KeyError(f"Environment variable {key} is not found") from err
if cast is bool:
return cast_bool(val)
return cast(val)