Skip to content

Commit 3d3e309

Browse files
authored
Merge pull request #2 from MyTooliT/🐎
Load Default Configuration from Package Data
2 parents 47b10d6 + 1351cce commit 3d3e309

6 files changed

Lines changed: 11 additions & 14 deletions

File tree

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ WORKDIR /app
1919

2020
# App code
2121
COPY . .
22-
COPY example.env .env
2322

2423
# Install deps (only main/runtime deps)
2524
RUN poetry install --no-interaction --no-ansi --only main

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
global-include *.yaml
2+
global-include *.env

README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ Then run the following command to get up and running:
5050
poetry lock && poetry install --all-extras
5151
```
5252

53-
Once you have that, get a baseline `.env` and run the API:
54-
55-
```shell
56-
cp example.env .env
57-
```
53+
Once you have that run the API:
5854

5955
```shell
6056
poetry run python3 icoapi/api.py
@@ -75,13 +71,7 @@ INSTALL_DIR="/etc/icoapi"
7571
SERVICE_PATH="/etc/systemd/system"
7672
```
7773

78-
Please note that the install script expects that the root folder of the repository contains an `.env` configuration file. You can use `example.env` as starting point:
79-
80-
```sh
81-
cp example.env .env
82-
```
83-
84-
After you created the configuration file, run the script to install normally:
74+
Run the script to install normally:
8575

8676
```sh
8777
./install.sh
File renamed without changes.

icoapi/scripts/file_handling.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from typing import Tuple
66
import shutil
77
import re
8+
from importlib.resources import files
9+
810

911
from dotenv import load_dotenv
1012
from platformdirs import user_data_dir
@@ -26,6 +28,11 @@ def load_env_file():
2628
bundle_dir = sys._MEIPASS
2729
logger.warning(f"Environment variables not found in local directory. Trying to load from app data: {bundle_dir}")
2830
env_loaded = load_dotenv(os.path.join(bundle_dir, "config", ".env"), verbose=True)
31+
if not env_loaded:
32+
# Fourth try: load default configuration from package data
33+
package_data = files('icoapi').joinpath("config")
34+
logger.warning(f"Environment variables not found in app data. Trying to load from package data: %s", package_data)
35+
env_loaded = load_dotenv(stream=(package_data.joinpath("default.env").open('r', encoding='utf-8')))
2936
if not env_loaded:
3037
logger.critical(f"Environment variables not found")
3138
raise EnvironmentError(".env not found")

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sudo mkdir -p $INSTALL_DIR
2727
sudo chown $USER:$USER $INSTALL_DIR
2828

2929
echo "Copying application files..."
30-
FILES_AND_DIRS=("config" "$MODULE_NAME" "pyproject.toml" "README.md")
30+
FILES_AND_DIRS=("$MODULE_NAME" "pyproject.toml" "README.md")
3131

3232
for ITEM in "${FILES_AND_DIRS[@]}"; do
3333
#cp -r "$ITEM" "$INSTALL_DIR"

0 commit comments

Comments
 (0)