From 00e794292a1034cd0f317fd421e4fc0ca8948fab Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 1 Oct 2013 20:34:48 +1300 Subject: [PATCH] config loading, data directory, more logging --- cloudbot.py | 4 +--- core/bot.py | 18 ++++++++++++++++-- core/config.py | 20 ++++++++++++++++---- plugins/log.py | 4 ++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/cloudbot.py b/cloudbot.py index 533d56c..3c9b76b 100644 --- a/cloudbot.py +++ b/cloudbot.py @@ -23,9 +23,7 @@ eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(), reload(init=True) -bot.persist_dir = os.path.abspath('persist') -if not os.path.exists(bot.persist_dir): - os.mkdir(bot.persist_dir) + print 'Connection(s) made, starting main loop.' diff --git a/core/bot.py b/core/bot.py index 96cab3f..879a0e2 100644 --- a/core/bot.py +++ b/core/bot.py @@ -1,6 +1,8 @@ import time import logging import re +import os +import sys import config import irc @@ -19,6 +21,7 @@ class Bot(object): # set up config and logging self.setup() + self.logger.debug("Bot setup completed.") # start IRC connections self.connections = {} @@ -46,14 +49,25 @@ class Bot(object): self.logger = self.get_logger() self.logger.debug("Logging engine started.") - # logging + # data folder + self.data_dir = os.path.abspath('data/{}'.format(self.name)) + if not os.path.exists(self.data_dir): + self.logger.debug("Data folder not found, creating.") + os.mkdir(os.path.abspath('data')) + os.mkdir(self.data_dir) + self.logger.debug("Created data folder.") + + # config self.config = self.get_config() + self.logger.debug("Config object created.") self.config.load_config() self.logger.debug("Config loaded.") + def get_config(self): """create and return the config object""" - return config.Config(self.name) + return config.Config(self.name, self.logger) + def get_logger(self): """create and return the logger object""" diff --git a/core/config.py b/core/config.py index da32ddf..5ec75f1 100755 --- a/core/config.py +++ b/core/config.py @@ -1,16 +1,28 @@ import json import os +import time +import sys class Config(dict): - def __init__(self, name, *args, **kwargs): - self.path = os.path.abspath("{}.json".format(name)) + def __init__(self, name, logger, *args, **kwargs): + self.path = os.path.abspath("{}.config.json".format(name)) + self.logger = logger self.update(*args, **kwargs) def load_config(self): + if not os.path.exists(self.path): + # if there is no config, show an error and die + self.logger.critical("No config file found, bot shutting down!") + print "No config file found! Bot shutting down in five seconds." + print "Copy 'cloudbot.default.json' to 'cloudbot.config.json' for defaults." + print "For help, see http://git.io/cloudbotirc. Thank you for using CloudBot!" + time.sleep(5) + sys.exit() + with open(self.path) as f: self.update(json.load(f)) - + self.logger.info("Config reloaded.") def save_config(self): - pass \ No newline at end of file + json.dump(self, open(self.path, 'w'), sort_keys=True, indent=2) diff --git a/plugins/log.py b/plugins/log.py index d72dc1a..487d92f 100755 --- a/plugins/log.py +++ b/plugins/log.py @@ -93,7 +93,7 @@ def get_log_fd(dir, server, chan): def log(paraml, input=None, bot=None): timestamp = gmtime(timestamp_format) - fd = get_log_fd(bot.persist_dir, input.server, 'raw') + fd = get_log_fd(bot.data_dir, input.server, 'raw') fd.write(timestamp + ' ' + input.raw + '\n') if input.command == 'QUIT': # these are temporary fixes until proper @@ -107,7 +107,7 @@ def log(paraml, input=None, bot=None): return if input.chan: - fd = get_log_fd(bot.persist_dir, input.server, input.chan) + fd = get_log_fd(bot.data_dir, input.server, input.chan) fd.write(timestamp + ' ' + beau + '\n') print timestamp, input.chan, beau.encode('utf8', 'ignore')