Mercurial > xib
changeset 216:d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sat, 06 Mar 2010 22:55:47 +0100 |
parents | b900585bdbbb |
children | 45065f6f674c |
files | bot.py bridge.py commands.py |
diffstat | 3 files changed, 21 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/bot.py +++ b/bot.py @@ -164,7 +164,7 @@ class Bot(threading.Thread): r = reason.getData() if r == 'The conference component is shutting down': # MUC server is going down, try to restart the bridges in 1 minute - bridges = self.find_bridges([from_.getDomain()]) + bridges = self.iter_bridges(patterns=[from_.getDomain()]) m = 'The MUC server '+from_.getDomain()+' seems to be going down, the bot will try to recreate all bridges related to this server in 1 minute' error = [say_levels.warning, m] self.restart_bridges_delayed(bridges, 60, error) @@ -272,7 +272,7 @@ class Bot(threading.Thread): if err == 'cancel remote-server-not-found': # Remote server not found # Stop bridges that depend on this server - bridges = self.find_bridges([from_.getDomain()]) + bridges = self.iter_bridges(patterns=[from_.getDomain()]) error = [say_levels.error, 'XMPP Remote server not found: '+from_.getDomain()] self.restart_bridges_delayed(bridges, 60, error) else: @@ -650,7 +650,7 @@ class Bot(threading.Thread): if event.eventtype() in ['cannotsendtochan', 'notonchannel', 'inviteonlychan']: self.error(2, debug_str, debug=True) - bridges = self.get_bridges(irc_room=event.arguments()[0], irc_server=connection.server) + bridges = self.iter_bridges(irc_room=event.arguments()[0], irc_server=connection.server) if len(bridges) > 1: raise Exception, 'more than one bridge for one irc chan, WTF ?' bridge = bridges[0] @@ -694,7 +694,7 @@ class Bot(threading.Thread): self.error(1, 'ignoring IRC mode "'+event.arguments()[0]+'" for "'+event.arguments()[1]+'"', debug=True) return self.error(2, debug_str, debug=True) - bridges = self.get_bridges(irc_room=event.target(), irc_server=connection.server) + bridges = self.iter_bridges(irc_room=event.target(), irc_server=connection.server) if len(bridges) > 1: raise Exception, 'more than one bridge for one irc chan, WTF ?' bridge = bridges[0] @@ -739,17 +739,6 @@ class Bot(threading.Thread): return b - def find_bridges(self, str_array): - # TODO: lock self.bridges for thread safety - bridges = [b for b in self.bridges] - for bridge in self.bridges: - for s in str_array: - if not s in str(bridge): - bridges.remove(bridge) - break - return bridges - - def format_message(self, importance, message): if importance < 0 or importance >= len(say_levels.levels): raise Exception('[Internal Error] unknown message importance') @@ -757,11 +746,11 @@ class Bot(threading.Thread): def get_bridge(self, **kwargs): - """Calls self.get_bridges and raises exceptions when there are 0 or more than 1 matches + """Calls self.iter_bridges and raises exceptions when there are 0 or more than 1 matches - See Bot.get_bridges for the list of args""" + See Bot.iter_bridges for the list of args""" - bridges = self.get_bridges(**kwargs) + bridges = [b for b in self.iter_bridges(**kwargs)] if len(bridges) == 0: raise Exception, 'no bridge matching '+str(kwargs) elif len(bridges) > 1: @@ -769,20 +758,19 @@ class Bot(threading.Thread): return bridges[0] - def get_bridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None): - # TODO: lock self.bridges for thread safety - bridges = [b for b in self.bridges] - for bridge in [b for b in bridges]: + def iter_bridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None, patterns=None): + for bridge in self.bridges: if irc_room != None and bridge.irc_room != irc_room: - bridges.remove(bridge) continue if irc_server != None and bridge.irc_server != irc_server: - bridges.remove(bridge) continue if xmpp_room_jid != None and bridge.xmpp_room_jid != xmpp_room_jid: - bridges.remove(bridge) continue - return bridges + if patterns != None: + for pattern in patterns: + if not pattern in str(bridge): + continue + yield bridge def get_xmpp_connection(self, nickname):
--- a/bridge.py +++ b/bridge.py @@ -100,7 +100,7 @@ class Bridge: def _RemoteServerNotFound_handler(self): server = xmpp.protocol.JID(self.xmpp_room_jid).getDomain() - bridges = self.bot.find_bridges([server]) + bridges = self.bot.iter_bridges(patterns=[server]) error = [say_levels.warning, 'The MUC server '+server+' seems to be down, the bot will try to recreate all bridges related to this server in 5 minutes'] self.bot.restart_bridges_delayed(bridges, 300, error)
--- a/commands.py +++ b/commands.py @@ -53,28 +53,28 @@ def execute(bot, command_line, bot_admin def _find_bridges(bot, args_array): ret = '' - b = [] + matches = [] for arg in args_array: try: bn = int(arg) if bn < 1: raise IndexError - b.append(bot.bridges[bn-1]) + matches.append(bot.bridges[bn-1]) except IndexError: ret += '\nInvalid bridge number "'+str(bn)+'".' except ValueError: - found_bridges = bot.find_bridges(arg) + found_bridges = [b for b in bot.iter_bridges(patterns=[arg])] if len(found_bridges) == 0: ret += '\nNo bridge found matching "'+arg+'".' else: - b.extend(found_bridges) + matches.extend(found_bridges) - if ret != '' or len(b) == 0: + if ret != '' or len(matches) == 0: if ret != '': ret += '\n\n' ret += bridges(bot, 'bridges', [], None)+'\n\n' - return (b, ret) + return (matches, ret) def add_bridge(bot, command, args_array, bridge):