Skip to content

Commit 96dfd1c

Browse files
committed
Support .env.override file and remove env.example
1 parent a6686dd commit 96dfd1c

File tree

4 files changed

+20
-32
lines changed

4 files changed

+20
-32
lines changed

.env.example

-24
This file was deleted.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.idea
33
**/__pycache__
44
.env
5+
.env.override
56
logs/

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ When the current time is a price minimum, the program wakes up and does the foll
117117
```
118118
4. Create an `.env` and fill in your values
119119
```bash
120-
cp .env.example .env
121120
vi .env
122121
```
123122

@@ -143,6 +142,7 @@ When the current time is a price minimum, the program wakes up and does the foll
143142

144143
All the environment variables are read in every time they are used.
145144
As a consequence, the program does **not** have to be restarted when they are altered.
145+
If a `.env.override` exists values of the `.env` are overwritten.
146146

147147
### Running
148148
#### Manually
@@ -165,7 +165,6 @@ cd app/
165165
python -m venv .venv
166166
source .venv/bin/activate
167167
poetry install
168-
cp .env.example .env
169168
vi .env
170169
chown -R <username>: app/
171170
```

source/environment_variable_getter.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
import os
22
from typing import Any
33

4-
from dotenv import load_dotenv
4+
from dotenv import find_dotenv, load_dotenv
55

66

77
class EnvironmentVariableGetter:
88
@staticmethod
9-
def get(name_of_variable: str, default_value: Any = None) -> str:
9+
def get(name_of_variable: str, default_value: Any = None) -> bool | str:
1010
"""
11+
Gets the value of an environment variable.
12+
13+
The method attempts to retrieve the value of the provided environment variable. If the variable exists, it's
14+
retrieved and, if applicable, converted to a boolean. If the variable doesn't exist, an optional default value
15+
is returned. If neither the variable is set nor a default value is provided, it raises a RuntimeError.
16+
The method also processes and gives precedence to variables defined in an `.env.override` file.
17+
1118
Args:
12-
name_of_variable: The name of the environment variable to be retrieved.
13-
default_value: Optional; The default value to return if the environment variable is not set.
19+
name_of_variable (str): The name of the environment variable to query.
20+
default_value (Any, optional): The fallback value to return if the environment variable is not set.
1421
1522
Returns:
16-
The value of the environment variable cast to a boolean if possible, otherwise returns its string value or the default value.
23+
bool | str: The retrieved environment variable value, either as a boolean (if applicable) or string.
1724
1825
Raises:
19-
RuntimeError: If the environment variable is not set and no default value is provided.
26+
RuntimeError: If the specified environment variable is not found and no default value is provided.
2027
"""
2128
load_dotenv(override=True)
2229

30+
# Load variables from .env.override with higher priority
31+
load_dotenv(dotenv_path=find_dotenv(".env.override"), override=True)
32+
2333
try:
2434
value = os.environ[name_of_variable]
35+
if value == "":
36+
raise KeyError()
2537
return EnvironmentVariableGetter._cast_string_to_bool(value)
2638
except KeyError:
2739
if default_value is not None:

0 commit comments

Comments
 (0)