diff --git a/active_bots/mailbot.py b/active_bots/mailbot.py index 7c8fcb5..fbcd02b 100644 --- a/active_bots/mailbot.py +++ b/active_bots/mailbot.py @@ -19,7 +19,11 @@ class Mailbot(Bot): def crawl(self, user): reports = [] # todo: adjust to actual mailbox - mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user']) + try: + mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user']) + except FileNotFoundError: + logger.error("No mbox file found.") + return reports for msg in mails: if get_date_from_header(msg['Date']) > user.get_seen_mail(): if user.get_city().lower() in msg['To'].lower(): diff --git a/config.py b/config.py index fa01302..bc7baaf 100755 --- a/config.py +++ b/config.py @@ -1,5 +1,70 @@ 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: + configdict = toml.load(defaultconf) + + try: + if os.environ['CONSUMER_KEY'] != "": + configdict['twitter']['consumer_key'] = os.environ['CONSUMER_KEY'] + except KeyError: + pass + + try: + if os.environ['CONSUMER_SECRET'] != "": + configdict['twitter']['consumer_secret'] = os.environ['CONSUMER_SECRET'] + except KeyError: + pass + + try: + if os.environ['HOST'] != "": + configdict['web']['host'] = os.environ['HOST'] + except KeyError: + pass + + try: + if os.environ['PORT'] != "": + configdict['web']['port'] = os.environ['PORT'] + except KeyError: + pass + + try: + if os.environ['CONTACT'] != "": + configdict['web']['contact'] = os.environ['CONTACT'] + except KeyError: + pass + + try: + if os.environ['MBOX_USER'] != "": + configdict['mail']['mbox_user'] = os.environ['MBOX_USER'] + except KeyError: + pass + + try: + if os.environ['DB_PATH'] != "": + configdict['database']['db_path'] = os.environ['DB_PATH'] + except KeyError: + pass + + return configdict + # 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() + +if __name__ == "__main__": + for category in config: + for key in config[category]: + print(key + "=" + str(config[category][key])) diff --git a/frontend.py b/frontend.py index 63143d5..b2e0eb2 100755 --- a/frontend.py +++ b/frontend.py @@ -263,7 +263,6 @@ application = bottle.default_app() bottle.install(SessionPlugin('/')) if __name__ == '__main__': - # testing only - bottle.run(host=config["web"]["host"], port=config["web"]["port"]) + bottle.run(host="0.0.0.0", port=config["web"]["port"]) else: application.catchall = False