diff --git a/core/config.py b/core/config.py index e69de29..f72a4a4 100644 --- a/core/config.py +++ b/core/config.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +## EarwigBot's Config File Parser + +from collections import defaultdict +import ConfigParser as configparser +import os + +main_cfg_path = os.path.join("config", "main.cfg") +secure_cfg_path = os.path.join("config", "secure.cfg") + +config = dict() + +def load_config_file(filename): + parser = configparser.SafeConfigParser() + parser.optionxform = str # don't lowercase option names automatically + parser.read(filename) + return parser + +def make_new_config(): + print "You haven't configured the bot yet!" + choice = raw_input("Would you like to do this now? [y/n] ") + if choice.lower().startswith("y"): + pass + else: + exit() + +def dump_config_to_dict(parsers): + global config + for parser in parsers: + for section in parser.sections(): + for option in parser.options(section): + try: + config[section][option] = parser.get(section, option) + except KeyError: + config[section] = defaultdict(lambda: None) + config[section][option] = parser.get(section, option) + +def load(): + if not os.path.exists(main_cfg_path): + make_new_config() + + main_cfg = load_config_file(main_cfg_path) + secure_cfg = load_config_file(secure_cfg_path) + + dump_config_to_dict([main_cfg, secure_cfg]) diff --git a/core/main.py b/core/main.py index 3000def..a5d7f2f 100644 --- a/core/main.py +++ b/core/main.py @@ -28,7 +28,7 @@ import os parent_dir = os.path.split(sys.path[0])[0] sys.path.append(parent_dir) # make sure we look in the parent directory for modules -from config.main import * +from core import config from irc import frontend, watcher from wiki import task_manager @@ -96,10 +96,13 @@ def irc_frontend(): f_conn.close() def run(): - if enable_irc_frontend: # make the frontend run on our primary thread if enabled, and enable additional components through that function + config.load() + components = config.config["main"] + + if components["enable_irc_frontend"]: # make the frontend run on our primary thread if enabled, and enable additional components through that function irc_frontend() - elif enable_wiki_schedule: # the scheduler is enabled - run it on the main thread, but also run the IRC watcher on another thread if it is enabled + elif components["enable_wiki_schedule"]: # the scheduler is enabled - run it on the main thread, but also run the IRC watcher on another thread if it is enabled print "\nStarting wiki scheduler..." task_manager.load_tasks() if enable_irc_watcher: @@ -109,7 +112,7 @@ def run(): t_watcher.start() wiki_scheduler() - elif enable_irc_watcher: # the IRC watcher is our only enabled component, so run its function only and don't worry about anything else + elif components["enable_irc_watcher"]: # the IRC watcher is our only enabled component, so run its function only and don't worry about anything else irc_watcher() else: # nothing is enabled!