From 9f2446b5c99f822acce8853bfa359b1910004e9f Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Wed, 29 May 2019 09:39:59 -0400 Subject: [PATCH] Add a way to read the bot's config without the bot --- sopel_modules/SpiceBot/Config.py | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 sopel_modules/SpiceBot/Config.py diff --git a/sopel_modules/SpiceBot/Config.py b/sopel_modules/SpiceBot/Config.py new file mode 100644 index 0000000..9c35d9d --- /dev/null +++ b/sopel_modules/SpiceBot/Config.py @@ -0,0 +1,41 @@ +# coding=utf8 +from __future__ import unicode_literals, absolute_import, division, print_function +"""A way to read the bot's config without bot""" + +from sopel.cli.run import build_parser, get_configuration + +import sys +import configparser + + +class BotConfig(): + + def __init__(self): + self.dict = {} + self.config = self.load_config() + self.config_dict = self.config_file_to_dict() + + def load_config(self): + if len(sys.argv[1:]): + argv = [] + else: + argv = sys.argv[1:] + parser = build_parser() + opts = parser.parse_args(argv) + config_module = get_configuration(opts) + return config_module + + def config_file_to_dict(self): + configdict = dict() + config = configparser.ConfigParser() + config.read(self.config.filename) + for each_section in config.sections(): + if each_section not in configdict.keys(): + configdict[each_section] = dict() + for (each_key, each_val) in config.items(each_section): + if each_key not in configdict[each_section].keys(): + configdict[each_section][each_key] = each_val + return configdict + + +db = BotConfig()