Started project refresh

This commit is contained in:
Luke Rogers 2013-10-01 19:12:41 +13:00
parent dd40b71252
commit 28cb06c84b
4 changed files with 62 additions and 35 deletions

View file

@ -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 <http://git.io/cloudbotirc>'
# 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():

0
core/__init__.py Normal file
View file

46
core/bot.py Normal file
View file

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

View file

@ -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
def save(self):
pass