From 28cb06c84ba295acefd6d03fce87d050b242737b Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 1 Oct 2013 19:12:41 +1300 Subject: [PATCH] Started project refresh --- cloudbot.py | 21 ++++++--------------- core/__init__.py | 0 core/bot.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ core/config.py | 30 ++++++++++-------------------- 4 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 core/__init__.py create mode 100644 core/bot.py diff --git a/cloudbot.py b/cloudbot.py index 91515db..66ba61d 100644 --- a/cloudbot.py +++ b/cloudbot.py @@ -1,41 +1,32 @@ #!/usr/bin/env python +# we import bot as _bot for now, for legacy reasons +from core import bot as _bot import os import Queue import sys -import time import re -sys.path += ['plugins', 'lib'] # add stuff to the sys.path for easy imports +sys.path += ['plugins', 'lib', 'core'] # add stuff to the sys.path for easy imports os.chdir(sys.path[0] or '.') # do stuff relative to the install directory - -class Bot(object): - pass - print 'CloudBot DEV ' # create new bot object -bot = Bot() -bot.vars = {} - -# record start time for the uptime command -bot.start_time = time.time() +bot = _bot.Bot("cloudbot") print 'Begin Plugin Loading.' +print bot.config # bootstrap the reloader eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(), os.path.join('core', 'reload.py'), 'exec')) reload(init=True) -config() -if not hasattr(bot, 'config'): - exit() - print 'Connecting to IRC...' bot.conns = {} +print bot.config try: for name, conf in bot.config['connections'].iteritems(): diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/bot.py b/core/bot.py new file mode 100644 index 0000000..ae987d1 --- /dev/null +++ b/core/bot.py @@ -0,0 +1,46 @@ +import time +import logging + +import config + +class Bot(object): + def __init__(self, name): + # basic variables + self.name = name + self.start_time = time.time() + + # set up config and logging + self.setup() + print self.config + + + def setup(self): + # logging + self.logger = self.get_logger() + self.logger.debug("Logging engine started.") + + # logging + self.config = self.get_config() + self.config.reload() + self.logger.debug("Config loaded.") + + def get_config(self): + return config.Config(self.name) + + def get_logger(self): + # create logger + logger = logging.getLogger(self.name) + logger.setLevel(logging.DEBUG) + + # add a file handler + log_name = "{}.log".format(self.name) + fh = logging.FileHandler(log_name) + fh.setLevel(logging.DEBUG) + + # create a formatter and set the formatter for the handler. + frmt = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s') + fh.setFormatter(frmt) + + # add the Handler to the logger + logger.addHandler(fh) + return logger diff --git a/core/config.py b/core/config.py index b81fb4d..d5be7e8 100755 --- a/core/config.py +++ b/core/config.py @@ -1,27 +1,17 @@ -import inspect import json import os -def save(conf): - json.dump(conf, open('config', 'w'), sort_keys=True, indent=2) +class Config(dict): + def __init__(self, name, *args, **kwargs): + self.path = os.path.abspath(name) + self.update(*args, **kwargs) -if not os.path.exists('config'): - print "Please rename 'config.default' to 'config' to set up your bot!" - print "For help, see http://git.io/cloudbotirc" - print "Thank you for using CloudBot!" - sys.exit() + def reload(self): + with open(self.path) as f: + self = json.load(f) + print self -def config(): - # reload config from file if file has changed - config_mtime = os.stat('config').st_mtime - if bot._config_mtime != config_mtime: - try: - bot.config = json.load(open('config')) - bot._config_mtime = config_mtime - except ValueError, e: - print 'error: malformed config', e - - -bot._config_mtime = 0 \ No newline at end of file + def save(self): + pass \ No newline at end of file