Commits

Dafydd Francis committed c588ab8

Exceptions in trtl.py doesn't quit the repl

  • Participants
  • Parent commits 09a08f3

Comments (0)

Files changed (2)

test/fake_tbot.py

 import re
 
 
+class Logger():
+    def log(self, *args):
+        pass
+
+
 class TestedBot:
     bot_messages = []
+    logger = Logger()
     functions = []
     messages = {}
+    __funcs = {}
 
     def __init__(self):
         pass
     def msg(self, channel, message):
         self.bot_messages.append((channel, message))
 
-    def register(self, func):
+    def register(self, func, name=None):
         self.functions.append(func)
+        if name:
+            self.__funcs[name] = func.rule
+
+    def rules(self):
+        messages = ["The rules and functions are as follows:"]
+        for func in self.__funcs:
+            messages.append("    %s = %s" % (func, self.__funcs[func]))
+        return messages
 
     def last_message(self):
         if len(self.bot_messages):
         for func in self.functions:
             if re.match(func.rule, message):
                 func(self, usr, channel, message)
+        return self.bot_messages
 """
 import os
 import sys
+import traceback
 import readline
 import re
 regex = re.compile("\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE)
         for function in dir(MODULES[module]):
             glob = MODULES[module].__dict__[function]
             if hasattr(glob, 'rule'):
-                TBOT.register(glob)
+                TBOT.register(glob, function)
     except:
         pass
 
         print "Bye!"
         sys.exit()
     if msg and msg[0] == "/":
-        (command, value) = msg.split(" ", 1)
+        try:
+            (command, value) = msg.split(" ", 1)
+        except:
+            command = msg
         if command == "/nick":
             USER = value
             print "(Nick has changed to '%s')" % USER
         elif command == "/j":
             CHANNEL = value
             print "(Channel has changed to '%s')" % CHANNEL
+        elif command == "/rules":
+            print "\n".join(TBOT.rules())
         else:
             print "(Not a recognised command)"
     else:
-        TBOT.listen(USER, CHANNEL, msg)
-        if [] != TBOT.bot_messages:
-            print regex.sub('', "\n".join([x[1] for x in TBOT.bot_messages]))
-            TBOT.bot_messages = []
+        try:
+            TBOT.listen(USER, CHANNEL, msg)
+            if [] != TBOT.bot_messages:
+                print regex.sub('', "\n".join([x[1] for x in TBOT.bot_messages]))
+                TBOT.bot_messages = []
+        except:
+            print traceback.format_exc()