2018-03-24 15:26:35 +00:00
|
|
|
import pytoml as toml
|
2018-11-06 15:17:47 +00:00
|
|
|
import os
|
|
|
|
|
2018-11-06 15:23:47 +00:00
|
|
|
|
2018-11-06 15:17:47 +00:00
|
|
|
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:
|
2018-11-06 15:23:47 +00:00
|
|
|
configdict = toml.load(defaultconf)
|
2018-11-06 15:17:47 +00:00
|
|
|
|
|
|
|
try:
|
2018-11-06 17:08:51 +00:00
|
|
|
if os.environ['CONSUMER_KEY'] != "":
|
|
|
|
configdict['twitter']['consumer_key'] = os.environ['CONSUMER_KEY']
|
2018-11-06 15:17:47 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2018-11-06 17:08:51 +00:00
|
|
|
if os.environ['CONSUMER_SECRET'] != "":
|
|
|
|
configdict['twitter']['consumer_secret'] = os.environ['CONSUMER_SECRET']
|
2018-11-06 15:17:47 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2018-11-06 17:08:51 +00:00
|
|
|
if os.environ['HOST'] != "":
|
|
|
|
configdict['web']['host'] = os.environ['HOST']
|
2018-11-06 15:17:47 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2018-11-06 17:08:51 +00:00
|
|
|
if os.environ['PORT'] != "":
|
|
|
|
configdict['web']['port'] = os.environ['PORT']
|
2018-11-06 15:17:47 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2018-11-06 17:08:51 +00:00
|
|
|
if os.environ['CONTACT'] != "":
|
|
|
|
configdict['web']['contact'] = os.environ['CONTACT']
|
2018-11-06 15:17:47 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2018-11-06 17:08:51 +00:00
|
|
|
if os.environ['MBOX_USER'] != "":
|
|
|
|
configdict['mail']['mbox_user'] = os.environ['MBOX_USER']
|
2018-11-06 15:17:47 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2018-11-06 17:08:51 +00:00
|
|
|
if os.environ['DB_PATH'] != "":
|
|
|
|
configdict['database']['db_path'] = os.environ['DB_PATH']
|
2018-11-06 15:17:47 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2018-11-06 15:23:47 +00:00
|
|
|
return configdict
|
2018-11-06 15:17:47 +00:00
|
|
|
|
2018-03-24 15:26:35 +00:00
|
|
|
|
|
|
|
# read config in TOML format (https://github.com/toml-lang/toml#toml)
|
2018-11-06 15:17:47 +00:00
|
|
|
try:
|
|
|
|
with open('config.toml') as configfile:
|
|
|
|
config = toml.load(configfile)
|
|
|
|
except FileNotFoundError:
|
|
|
|
config = load_env()
|
2018-11-06 15:22:11 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
for category in config:
|
|
|
|
for key in config[category]:
|
2018-11-06 16:50:57 +00:00
|
|
|
print(key + "=" + str(config[category][key]))
|