config loading, data directory, more logging

This commit is contained in:
Luke Rogers 2013-10-01 20:34:48 +13:00
parent 74cd20c55d
commit 00e794292a
4 changed files with 35 additions and 11 deletions

View file

@ -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.'

View file

@ -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"""

View file

@ -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
json.dump(self, open(self.path, 'w'), sort_keys=True, indent=2)

View file

@ -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')