ticketfrei/config.py

64 lines
1.4 KiB
Python
Raw Normal View History

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