aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2023-12-08 11:59:09 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2023-12-29 07:29:15 +0100
commit024337df24f6f897bdbb3a3bb4ddf24ed562c997 (patch)
tree3349efe72f9a8617a5bfa9691bf7e40bb2b43e83
parent5973b7ca26e63880e9d74a52fa9e2ec61c811401 (diff)
Load configuration from a yaml file
-rw-r--r--.gitignore3
-rw-r--r--kerbana/config.py59
-rw-r--r--kerbana/settings.py8
-rw-r--r--kerbana/tests/__init__.py0
-rw-r--r--kerbana/tests/data/invalid.yaml1
-rw-r--r--kerbana/tests/data/valid.yaml1
-rw-r--r--kerbana/tests/test_config.py25
7 files changed, 94 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index 1dec1e4..4feffad 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,7 +3,6 @@
db.sqlite3
-kerbana.toml
-kerbana_tests.toml
+kerbana.yaml
.*.sw?
diff --git a/kerbana/config.py b/kerbana/config.py
new file mode 100644
index 0000000..03664c0
--- /dev/null
+++ b/kerbana/config.py
@@ -0,0 +1,59 @@
+import logging
+import os
+import sys
+
+import strictyaml
+
+
+log = logging.getLogger(__name__)
+
+
+def read_from_yaml(fp):
+ data = strictyaml.load(fp.read())
+ for k, v in data.data.items():
+ setattr(sys.modules[__name__], k, v)
+
+
+conf_file = "/etc/kerbana/kerbana.yaml"
+try:
+ with open(conf_file) as fp:
+ read_from_yaml(fp)
+ log.info("Configuration read from %s", conf_file)
+except FileNotFoundError:
+ log.info("File %s not found.", conf_file)
+except strictyaml.YAMLError as error:
+ log.error("Syntax error in %s", conf_file)
+ log.error(error)
+ sys.exit(1)
+
+
+conf_file = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)),
+ "..",
+ "kerbana.yaml"
+)
+try:
+ with open(conf_file) as fp:
+ read_from_yaml(fp)
+ log.info("Configuration read from %s", conf_file)
+except FileNotFoundError:
+ log.info("File %s not found.", conf_file)
+except strictyaml.YAMLError as error:
+ log.error("Syntax error in %s", conf_file)
+ log.error(error)
+ sys.exit(1)
+
+conf_file = os.environ.get("KERBANA_CONFIG")
+if conf_file:
+ try:
+ with open(conf_file) as fp:
+ read_from_yaml(fp)
+ log.info("Configuration read from %s", conf_file)
+ except FileNotFoundError:
+ log.info("File %s (as in $KERBANA_CONFIG) not found.", conf_file)
+ except strictyaml.YAMLError as error:
+ log.error("Syntax error in %s (as in $KERBANA_CONFIG)", conf_file)
+ log.error(error)
+ sys.exit(1)
+else:
+ log.info("Variable KERBANA_CONFIG not available")
diff --git a/kerbana/settings.py b/kerbana/settings.py
index f0ba3f2..9cccae0 100644
--- a/kerbana/settings.py
+++ b/kerbana/settings.py
@@ -125,8 +125,14 @@ STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
-MQTT_SERVER = "test.mosquitto.org"
+# MQTT settings
+# There is a MQTT server at test.mosquitto.org which can be used to try
+# out kerbana, but please read the terms of use before using it in
+# production.
+MQTT_SERVER = "mqtt.invalid.org"
MQTT_PORT = 1883
MQTT_USER = None
MQTT_PASSWORD = None
MQTT_TOPIC = "kerbana/#"
+
+from .config import *
diff --git a/kerbana/tests/__init__.py b/kerbana/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/kerbana/tests/__init__.py
diff --git a/kerbana/tests/data/invalid.yaml b/kerbana/tests/data/invalid.yaml
new file mode 100644
index 0000000..e29a5d7
--- /dev/null
+++ b/kerbana/tests/data/invalid.yaml
@@ -0,0 +1 @@
+SETTING: "value": "invalid"
diff --git a/kerbana/tests/data/valid.yaml b/kerbana/tests/data/valid.yaml
new file mode 100644
index 0000000..186d59b
--- /dev/null
+++ b/kerbana/tests/data/valid.yaml
@@ -0,0 +1 @@
+SETTING: "value"
diff --git a/kerbana/tests/test_config.py b/kerbana/tests/test_config.py
new file mode 100644
index 0000000..43b611d
--- /dev/null
+++ b/kerbana/tests/test_config.py
@@ -0,0 +1,25 @@
+import importlib
+import os
+
+from django.test import TestCase
+
+import kerbana.config
+
+class TestConfig(TestCase):
+ def test_config_from_env(self):
+ os.environ["KERBANA_CONFIG"] = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)),
+ "data",
+ "valid.yaml"
+ )
+ importlib.reload(kerbana.config)
+ self.assertEqual(kerbana.config.SETTING, "value")
+
+ def test_config_invalid(self):
+ os.environ["KERBANA_CONFIG"] = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)),
+ "data",
+ "invalid.yaml"
+ )
+ with self.assertRaises(SystemExit):
+ importlib.reload(kerbana.config)