aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2023-07-16 09:27:00 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2023-07-16 10:06:27 +0200
commitc6786b2ba652f555798725e2e7d692f5c7658a11 (patch)
tree3cfd9232f7d773cb7d5c3b5c787678a4d25cac62
parent7a3aa01f9e78e2322d1f11c7fab5e8d3003303aa (diff)
Fix loading of missing configuration
-rw-r--r--.gitignore2
-rw-r--r--kerbana/__init__.py40
-rw-r--r--tests/test_app.py13
3 files changed, 45 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 8534f9c..8a37618 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
*.pyc
.coverage
+kerbana.toml
+
.*.sw?
diff --git a/kerbana/__init__.py b/kerbana/__init__.py
index 920e730..ea7f5cf 100644
--- a/kerbana/__init__.py
+++ b/kerbana/__init__.py
@@ -1,7 +1,8 @@
+import os
from typing import Optional
import flask
-import tomllib
+import toml
from . import config
@@ -12,15 +13,34 @@ def create_app(test_config: Optional[config.Config] = None):
app.config.from_object(config.DefaultConfig)
if test_config is None:
- app.config.from_file(
- "/etc/kerbana/kerbana.toml",
- load=tomllib.load,
- )
- app.config.from_file(
- "kerbana.toml",
- load=tomllib.load,
- )
- app.config.from_envvar("KERBANA_CONFIG")
+ try:
+ app.config.from_file(
+ "/etc/kerbana/kerbana.toml",
+ load=toml.load,
+ )
+ except FileNotFoundError:
+ app.logger.debug("File /etc/kerbana/kerbana.toml not found.")
+ try:
+ app.config.from_file(
+ os.path.join(
+ os.path.dirname(os.path.abspath(__file__)),
+ "..",
+ "kerbana.toml",
+ ),
+ load=toml.load,
+ )
+ except FileNotFoundError:
+ app.logger.debug("File kerbana.toml not found.")
+ try:
+ app.config.from_envvar("KERBANA_CONFIG")
+ except RuntimeError as e:
+ app.logger.debug(e)
+ except FileNotFoundError:
+ app.logger.debug(
+ "File {} (as found in $KERBANA_CONFIG) not found.".format(
+ os.environ["KERBANA_CONFIG"]
+ )
+ )
app.config.from_prefixed_env(prefix="KERBANA_")
else:
app.config.from_object(test_config)
diff --git a/tests/test_app.py b/tests/test_app.py
index 6d96fbb..6cf29c5 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -1,4 +1,6 @@
+import os
import unittest
+import unittest.mock
from kerbana import config, create_app
@@ -12,3 +14,14 @@ class TestBase(unittest.TestCase):
def test_root(self):
res = self.client.get("/")
self.assertEqual("Hello World!", res.data.decode())
+
+
+class TestConfig(unittest.TestCase):
+ def test_default_config(self):
+ app = create_app()
+ self.assertEqual(app.config["SECRET_KEY"], "dev")
+
+ @unittest.mock.patch.dict(os.environ, {"KERBANA_CONFIG": "no_such_file"})
+ def test_kerbana_config_env_non_existing(self):
+ app = create_app()
+ self.assertEqual(app.config["SECRET_KEY"], "dev")