small tweaks to the refresh code

This commit is contained in:
Luke Rogers 2014-02-14 19:27:05 +13:00
parent e84b181f80
commit 3fde4692d1
5 changed files with 23 additions and 32 deletions

View file

@ -42,6 +42,7 @@ while True:
else:
if cloudbot.do_restart:
# create a new bot thread and start it
del cloudbot
cloudbot = bot.Bot()
cloudbot.start()
continue

View file

@ -45,7 +45,6 @@ def get_logger():
logger.addHandler(sh)
return logger
class Bot(threading.Thread):
def __init__(self):
# basic variables
@ -74,7 +73,6 @@ class Bot(threading.Thread):
threading.Thread.__init__(self)
def run(self):
"""recieves input from the IRC engine and processes it"""
self.logger.info("Starting main thread.")
@ -94,7 +92,6 @@ class Bot(threading.Thread):
while self.running and all(c.parsed_queue.empty() for c in self.connections):
time.sleep(.1)
def setup(self):
"""create the logger and config objects"""
# logging
@ -117,7 +114,6 @@ class Bot(threading.Thread):
self.db_session = scoped_session(db_factory)
self.logger.debug("Database system initalised.")
def connect(self):
"""connect to all the networks defined in the bot config"""
for conf in self.config['connections']:
@ -130,16 +126,15 @@ class Bot(threading.Thread):
self.logger.debug("({}) Creating connection to {}.".format(name, server))
if conf['connection'].get('ssl'):
self.connections.append(irc.SSLIRC(name, server, nick, config = conf,
port = port, channels = conf['channels'],
ignore_certificate_errors=conf['connection'].get('ignore_cert', True)))
self.connections.append(irc.SSLIRC(name, server, nick, config=conf,
port=port, channels=conf['channels'],
ignore_certificate_errors=conf['connection'].get('ignore_cert', True)))
self.logger.debug("({}) Created SSL connection.".format(name))
else:
self.connections.append(irc.IRC(name, server, nick, config = conf,
port = port, channels = conf['channels']))
self.connections.append(irc.IRC(name, server, nick, config=conf,
port=port, channels=conf['channels']))
self.logger.debug("({}) Created connection.".format(name))
def stop(self, reason=None):
"""quits all networks and shuts the bot down"""
self.logger.info("Stopping bot.")
@ -165,7 +160,6 @@ class Bot(threading.Thread):
self.running = False
def restart(self, reason=None):
"""shuts the bot down and restarts it"""
self.do_restart = True

View file

@ -29,10 +29,10 @@ def censor(text):
class ReceiveThread(threading.Thread):
"""receives messages from IRC and puts them in the input_queue"""
def __init__(self, socket, input_queue, timeout):
def __init__(self, sock, input_queue, timeout):
self.input_buffer = ""
self.input_queue = input_queue
self.socket = socket
self.socket = sock
self.timeout = timeout
self.shutdown = False
@ -76,8 +76,8 @@ class ReceiveThread(threading.Thread):
class SSLReceiveThread(ReceiveThread):
def __init__(self, socket, input_queue, timeout):
ReceiveThread.Thread.__init__(self, socket, input_queue, timeout)
def __init__(self, sock, input_queue, timeout):
ReceiveThread.__init__(self, sock, input_queue, timeout)
def recv_from_socket(self, nbytes):
return self.socket.read(nbytes)
@ -94,11 +94,11 @@ class SSLReceiveThread(ReceiveThread):
class SendThread(threading.Thread):
"""sends messages from output_queue to IRC"""
def __init__(self, socket, conn_name, output_queue):
def __init__(self, sock, conn_name, output_queue):
self.output_buffer = ""
self.output_queue = output_queue
self.conn_name = conn_name
self.socket = socket
self.socket = sock
self.shutdown = False
threading.Thread.__init__(self)
@ -148,11 +148,11 @@ class ParseThread(threading.Thread):
lastparam = paramlist[-1]
# put the parsed message in the response queue
self.parsed_queue.put([msg, prefix, command, params, nick, user, host,
mask, paramlist, lastparam])
mask, paramlist, lastparam])
# if the server pings us, pong them back
if command == "PING":
str = "PONG :" + paramlist[0]
self.output_queue.put(str)
string = "PONG :" + paramlist[0]
self.output_queue.put(string)
class IRCConnection(object):
@ -198,7 +198,7 @@ class SSLIRCConnection(IRCConnection):
def create_socket(self):
return wrap_socket(IRCConnection.create_socket(self), server_side=False,
cert_reqs=CERT_NONE if self.ignore_cert_errors else
cert_reqs=CERT_NONE if self.ignore_cert_errors else
CERT_REQUIRED)
@ -229,18 +229,17 @@ class IRC(object):
self.set_pass(self.config.get('server_password'))
self.set_nick(self.nick)
self.cmd("USER",
[self.config.get('user', 'cloudbot'), "3", "*", self.config.get('realname',
'CloudBot - http://git.io/cloudbot')])
[self.config.get('user', 'cloudbot'), "3", "*",
self.config.get('realname', 'CloudBot - http://git.io/cloudbot')])
self.parse_thread = ParseThread(self.input_queue, self.output_queue,
self.parsed_queue)
self.parse_thread.daemon = True
self.parse_thread.start()
def create_connection(self):
return IRCConnection(self.name, self.server, self.port,
self.input_queue, self.output_queue)
self.input_queue, self.output_queue)
def stop(self):
self.connection.stop()
@ -280,8 +279,8 @@ class IRC(object):
else:
self.send(command)
def send(self, str):
self.output_queue.put(str)
def send(self, string):
self.output_queue.put(string)
class SSLIRC(IRC):
@ -292,4 +291,4 @@ class SSLIRC(IRC):
def create_connection(self):
return SSLIRCConnection(self.name, self.server, self.port, self.input_queue,
self.output_queue, self.ignore_cert_errors)
self.output_queue, self.ignore_cert_errors)

View file

@ -39,12 +39,10 @@ class PluginLoader(object):
self.load_all()
def stop(self):
"""shuts down the plugin reloader"""
self.observer.stop()
def load_all(self):
"""runs load_file() on all python files in the plugins folder"""
files = set(glob.glob(os.path.join(self.path, '*.py')))
@ -52,7 +50,6 @@ class PluginLoader(object):
self.load_file(f, rebuild=True)
self.rebuild()
def load_file(self, path, rebuild=False):
"""loads (or reloads) all valid plugins from a specified file"""
filename = os.path.basename(path)
@ -118,7 +115,6 @@ class PluginLoader(object):
self.rebuild()
def rebuild(self):
"""rebuilds the cloudbot command and event hook lists"""
self.bot.commands = {}

View file

@ -1,3 +1,4 @@
sqlalchemy
oauth2
pygeoip
tweepy