From 1188ddda17d11b9261acc89c03bc99a0a7113902 Mon Sep 17 00:00:00 2001 From: Turtle Date: Mon, 6 Jan 2020 01:08:23 -0500 Subject: [PATCH] Add load_config function --- pisa/pisad.py | 58 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/pisa/pisad.py b/pisa/pisad.py index be9d571..5e84a72 100644 --- a/pisa/pisad.py +++ b/pisa/pisad.py @@ -2,7 +2,6 @@ from getopt import getopt from sys import argv, exit from signal import signal, SIGINT, SIGQUIT, SIGTERM -from pisa.conf import DB_PATH from common.logger import Logger from pisa.api import API from pisa.watcher import Watcher @@ -24,6 +23,53 @@ def handle_signals(signal_received, frame): exit(0) +def load_config(config): + """ + Looks through all of the config options to make sure they contain the right type of data and builds a config + dictionary. + + Args: + config (:obj:`module`): It takes in a config module object. + + Returns: + :obj:`dict` A dictionary containing the config values. + """ + + conf_dict = {} + + conf_fields = { + "BTC_RPC_USER": {"value": config.BTC_RPC_USER, "type": str}, + "BTC_RPC_PASSWD": {"value": config.BTC_RPC_PASSWD, "type": str}, + "BTC_RPC_HOST": {"value": config.BTC_RPC_HOST, "type": str}, + "BTC_RPC_PORT": {"value": config.BTC_RPC_PORT, "type": int}, + "BTC_NETWORK": {"value": config.BTC_NETWORK, "type": str}, + "FEED_PROTOCOL": {"value": config.FEED_PROTOCOL, "type": str}, + "FEED_ADDR": {"value": config.FEED_ADDR, "type": str}, + "FEED_PORT": {"value": config.FEED_PORT, "type": int}, + "MAX_APPOINTMENTS": {"value": config.MAX_APPOINTMENTS, "type": int}, + "EXPIRY_DELTA": {"value": config.EXPIRY_DELTA, "type": int}, + "MIN_TO_SELF_DELAY": {"value": config.MIN_TO_SELF_DELAY, "type": int}, + "SERVER_LOG_FILE": {"value": config.SERVER_LOG_FILE, "type": str}, + "PISA_SECRET_KEY": {"value": config.PISA_SECRET_KEY, "type": str}, + "CLIENT_LOG_FILE": {"value": config.CLIENT_LOG_FILE, "type": str}, + "TEST_LOG_FILE": {"value": config.TEST_LOG_FILE, "type": str}, + "DB_PATH": {"value": config.DB_PATH, "type": str}, + } + + for field in conf_fields: + value = conf_fields[field]["value"] + correct_type = conf_fields[field]["type"] + + if (value is not None) and isinstance(value, correct_type): + conf_dict[field] = value + else: + err_msg = "{} variable in config is of the wrong type".format(field) + logger.error(err_msg) + raise ValueError(err_msg) + + return conf_dict + + if __name__ == "__main__": logger.info("Starting PISA") @@ -36,24 +82,24 @@ if __name__ == "__main__": # FIXME: Leaving this here for future option/arguments pass + pisa_config = load_config(conf) + if not can_connect_to_bitcoind(): logger.error("Can't connect to bitcoind. Shutting down") - elif not in_correct_network(BTC_NETWORK): + elif not in_correct_network(pisa_config.get("BTC_NETWORK")): logger.error("bitcoind is running on a different network, check conf.py and bitcoin.conf. Shutting down") else: try: - db_manager = DBManager(DB_PATH) + db_manager = DBManager(pisa_config.get("DB_PATH")) watcher_appointments_data = db_manager.load_watcher_appointments() responder_trackers_data = db_manager.load_responder_trackers() - with open(PISA_SECRET_KEY, "rb") as key_file: + with open(pisa_config.get("PISA_SECRET_KEY"), "rb") as key_file: secret_key_der = key_file.read() - pisa_config = load_config(conf) - watcher = Watcher(db_manager, secret_key_der, config=pisa_config) if len(watcher_appointments_data) == 0 and len(responder_trackers_data) == 0: