From a59e8836265ef4e8bbb6e21bd7aea58d459090b0 Mon Sep 17 00:00:00 2001 From: moneromooo Date: Thu, 1 Jan 2015 10:06:09 +0000 Subject: [PATCH] Modules can now be reloaded --- tipbot.py | 20 ++++++++++++++++++++ tipbot/command_manager.py | 32 ++++++++++++++++++++++++++++---- tipbot/modules/payment.py | 2 +- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/tipbot.py b/tipbot.py index e34cdaa..3c80648 100644 --- a/tipbot.py +++ b/tipbot.py @@ -230,6 +230,25 @@ def SendToNick(nick,chan,msg): def IsRegistered(nick,chan,cmd): RunRegisteredCommand(nick,chan,SendToNick,"You are registered",SendToNick,"You are not registered") +def Reload(nick,chan,cmd): + sendto=GetSendTo(nick,chan) + modulename=GetParam(cmd,1) + if not modulename: + SendTo(sendto,"Usage: reload ") + return + if modulename=="builtin": + SendTo(sendto,"Cannot reload builtin module") + return + log_info('Unloading %s module' % modulename) + UnregisterCommands(modulename) + log_info('Reloading %s module' % modulename) + try: + reload(sys.modules[modulename]) + SendTo(sendto,'%s reloaded' % modulename) + except Exception,e: + log_error('Failed to load module "%s": %s' % (modulename, str(e))) + SendTo(sendto,'An error occured') + def OnIdle(): RunIdleFunctions([irc,redisdb]) @@ -249,6 +268,7 @@ def RegisterCommands(): RegisterCommand({'module': 'builtin', 'name': 'scanwho', 'function': ScanWho, 'admin': True, 'help': "Refresh users list in a channel"}) RegisterCommand({'module': 'builtin', 'name': 'dump_users', 'function': DumpUsers, 'admin': True, 'help': "Dump users table to log"}) RegisterCommand({'module': 'builtin', 'name': 'show_activity', 'function': ShowActivity, 'admin': True, 'help': "Show time since a user was last active"}) + RegisterCommand({'module': 'builtin', 'name': 'reload', 'function': Reload, 'admin': True, 'help': "Reload a module"}) def OnCommandProxy(cmd,chan,who): OnCommand(cmd,chan,who,RunAdminCommand,RunRegisteredCommand) diff --git a/tipbot/command_manager.py b/tipbot/command_manager.py index 629d97b..d122022 100644 --- a/tipbot/command_manager.py +++ b/tipbot/command_manager.py @@ -16,6 +16,7 @@ from tipbot.irc import * commands = dict() calltable=dict() idles = [] +cleanup = dict() def RunRegisteredCommand(nick,chan,ifyes,yesdata,ifno,nodata): if nick not in calltable: @@ -91,8 +92,11 @@ def Commands(nick,chan,cmd): def RegisterCommand(command): commands[command['name']] = command -def RegisterIdleFunction(function): - idles.append(function) +def RegisterIdleFunction(module,function): + idles.append((module,function)) + +def RegisterCleanupFunction(module,function): + cleanup.append((module,function)) def OnCommand(cmd,chan,who,check_admin,check_registered): if cmd[0] in commands: @@ -109,8 +113,28 @@ def OnCommand(cmd,chan,who,check_admin,check_registered): def RunIdleFunctions(param): for f in idles: try: - f(param) + f[1](param) except Exception,e: - log_error("Exception running idle function %s: %s" % (str(f),str(e))) + log_error("Exception running idle function %s from module %s: %s" % (str(f[1]),str(f[2]),str(e))) +def UnregisterCommands(module): + global commands + global idles + + if module in cleanup: + cleanup[module]() + del cleanup[module] + + new_idles = [] + for f in idles: + if f[0] != module: + new_idles.append(f) + idles = new_idles + + new_commands = dict() + for cmd in commands: + c = commands[cmd] + if c['module'] != module: + new_commands[cmd] = c + commands = new_commands diff --git a/tipbot/modules/payment.py b/tipbot/modules/payment.py index c2234b5..064b918 100644 --- a/tipbot/modules/payment.py +++ b/tipbot/modules/payment.py @@ -89,5 +89,5 @@ def UpdateCoin(param): log_error('UpdateCoin: Failed to get bulk payments: %s' % str(e)) last_wallet_update_time = time.time() -RegisterIdleFunction(UpdateCoin) +RegisterIdleFunction("payment",UpdateCoin)