From 238dd20d207c9d98b9167bf202e6d9aa9cc34772 Mon Sep 17 00:00:00 2001
From: b3yond <b3yond@riseup.net>
Date: Tue, 6 Nov 2018 16:17:47 +0100
Subject: [PATCH] if no config.toml, set config through environment #64

---
 config.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)

diff --git a/config.py b/config.py
index fa01302..abbae62 100755
--- a/config.py
+++ b/config.py
@@ -1,5 +1,57 @@
 import pytoml as toml
+import os
+
+def load_env():
+    """
+    load environment variables from the environment. If empty, use default
+    values from config.toml.example.
+
+    :return: config dictionary of dictionaries.
+    """
+    with open('config.toml.example') as defaultconf:
+        config = toml.load(configfile)
+
+    try:
+        config['twitter']['consumer_key'] = os.environ['CONSUMER_KEY']
+    except KeyError:
+        pass
+
+    try:
+        config['twitter']['consumer_secret'] = os.environ['CONSUMER_SECRET']
+    except KeyError:
+        pass
+
+    try:
+        config['web']['host'] = os.environ['HOST']
+    except KeyError:
+        pass
+
+    try:
+        config['web']['port'] = os.environ['PORT']
+    except KeyError:
+        pass
+
+    try:
+        config['web']['contact'] = os.environ['CONTACT']
+    except KeyError:
+        pass
+
+    try:
+        config['mail']['mbox_user'] = os.environ['MBOX_USER']
+    except KeyError:
+        pass
+
+    try:
+        config['database']['db_path'] = os.environ['DB_PATH']
+    except KeyError:
+        pass
+
+    return config
+
 
 # read config in TOML format (https://github.com/toml-lang/toml#toml)
-with open('config.toml') as configfile:
-    config = toml.load(configfile)
+try:
+    with open('config.toml') as configfile:
+        config = toml.load(configfile)
+except FileNotFoundError:
+    config = load_env()