changeset 2127:42232c6ac004

Remove code duplication in xmlrpcbot, initial support for getPageInfo()
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 15 Jun 2007 22:03:39 +0200
parents 8b7d6d70fc95
children 401d49b9f1ac
files jabberbot/commands.py jabberbot/xmlrpcbot.py jabberbot/xmppbot.py
diffstat 3 files changed, 88 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/jabberbot/commands.py	Fri Jun 15 21:53:58 2007 +0200
+++ b/jabberbot/commands.py	Fri Jun 15 22:03:39 2007 +0200
@@ -73,3 +73,12 @@
     
     def __init__(self, jid):
         BaseDataCommand.__init__(self, jid)
+
+class GetPageInfo(BaseDataCommand):
+    
+    description = u"show detailed information about a page"
+    parameter_list = u"pagename"
+    
+    def __init__(self, jid, pagename):
+        BaseDataCommand.__init__(self, jid)
+        self.pagename = pagename
--- a/jabberbot/xmlrpcbot.py	Fri Jun 15 21:53:58 2007 +0200
+++ b/jabberbot/xmlrpcbot.py	Fri Jun 15 22:03:39 2007 +0200
@@ -57,93 +57,112 @@
             self.get_page_html(command)
         elif isinstance(command, cmd.GetPageList):
             self.get_page_list(command)
+        elif isinstance(command, cmd.GetPageInfo):
+            self.get_page_info(command)
     
     def get_auth_token(self, jid):
         token = self.connection.getAuthToken(jid, self.config.secret)
         if token:
             self.token = token
     
+    def _xmlrpc_decorator(function):
+        """A decorator function, which adds some maintenance code
+        
+        This function takes care of preparing a MultiCall object and
+        an authentication token, and deleting them at the end.
+        
+        """
+        def wrapped_func(self, command):
+            self.token = None
+            self.multicall = MultiCall(self.connection)
+            self.get_auth_token(command.jid)
+            
+            if self.token:
+                self.multicall.applyAuthToken(self.token)
+   
+            try:
+                function(self, command)
+                self.commands_out.put_nowait(command)
+            except xmlrpclib.Fault, fault:
+                msg = u"""Your request has failed. The reason is:\n%s"""
+                notification = cmd.NotificationCommand(command.jid, msg % (fault.faultString, ))
+                self.commands_out.put_nowait(notification)
+
+            del self.token
+            del self.multicall
+                
+        return wrapped_func
+    
     def get_page(self, command):
-        self.token = None
-        self.multicall = MultiCall(self.connection)
-        self.get_auth_token(command.jid)
-            
+        """Returns a raw page"""
+        
+        self.multicall.getPage(command.pagename)
+        
         if not self.token:
             # FIXME: notify the user that he may not have full rights on the wiki
-            self.multicall.getPage(command.pagename)
             getpage_result = self.multicall()
         else:
-            self.multicall.applyAuthToken(self.token)
-            self.multicall.getPage(command.pagename)
-            token_result, getpage_result = self.multicall()
+            getpage_result, token_result = self.multicall()
 
-        # We get a dict only when Fault happens
-        if isinstance(getpage_result[0], dict):
-            error_str = u"""The page couldn't be retrieved. The reason is: "%s"."""
-            command.data = error_str % getpage_result[0]["faultString"]
-        else:
-            command.data = getpage_result[0]
-     
-        self.commands_out.put_nowait(command)
+        # FIXME: warn if token turned out being wrong
+        command.data = getpage_result[0]
+            
+    get_page = _xmlrpc_decorator(get_page)
         
-        del self.multicall
-        del self.token
         
     def get_page_html(self, command):
-        self.token = None
-        self.multicall = MultiCall(self.connection)
-        self.get_auth_token(command.jid)
-            
+        """Returns a html-formatted page"""
+        
+        self.multicall.getPageHTML(command.pagename)
+        
         if not self.token:
-            # FIXME: notify the user that he may not have full rights on the wiki
-            self.multicall.getPageHTML(command.pagename)
-            getpage_result = self.multicall()
+            # FIXME: notify the user that he may not have full rights on the wiki    
+            getpagehtml_result = self.multicall()
         else:
-            self.multicall.applyAuthToken(self.token)
-            self.multicall.getPageHTML(command.pagename)
-            token_result, getpage_result = self.multicall()
+            token_result, getpagehtml_result = self.multicall()
 
-        # We get a dict only when Fault happens
-        if isinstance(getpage_result[0], dict):
-            error_str = u"""The page couldn't be retrieved. The reason is: "%s"."""
-            command.data = error_str % getpage_result[0]["faultString"]
-        else:
-            command.data = getpage_result[0]
-     
-        self.commands_out.put_nowait(command)
+        # FIXME: warn if token turned out being wrong
+        command.data = getpagehtml_result[0]
+            
+    get_page_html = _xmlrpc_decorator(get_page_html)
         
-        del self.multicall
-        del self.token
         
     def get_page_list(self, command):
-        self.token = None
-        self.multicall = MultiCall(self.connection)
-        self.get_auth_token(command.jid)
+        """Returns a list of all accesible pages"""
         
         txt = u"""This command may take a while to complete, please be patient..."""
         info = cmd.NotificationCommand(command.jid, txt)
         self.commands_out.put_nowait(info)
         
+        self.multicall.getAllPages()
+        
         if not self.token:
             # FIXME: notify the user that he may not have full rights on the wiki
-            self.multicall.getAllPages()
-            getpage_result = self.multicall()
+            getpagelist_result = self.multicall()
         else:
-            self.multicall.applyAuthToken(self.token)
-            self.multicall.getAllPages()
-            token_result, getpage_result = self.multicall()
+            token_result, getpagelist_result = self.multicall()
 
-        # We get a dict only when Fault happens
-        if isinstance(getpage_result[0], dict):
-            error_str = u"""List couldn't be retrieved. The reason is: "%s"."""
-            command.data = error_str % getpage_result[0]["faultString"]
+        # FIXME: warn if token turned out being wrong
+        command.data = getpagelist_result[0]
+            
+    get_page_list = _xmlrpc_decorator(get_page_list)
+    
+    
+    def get_page_info(self, command):
+        """Returns detailed information about a given page"""
+        
+        self.multicall.getPageInfo(command.pagename)
+        
+        if not self.token:
+            # FIXME: notify the user that he may not have full rights on the wiki
+            getpageinfo_result = self.multicall()
         else:
-            command.data = getpage_result[0]
-     
-        self.commands_out.put_nowait(command)
+            token_result, getpageinfo_result = self.multicall()
         
-        del self.multicall
-        del self.token
+        # FIXME: warn if token turned out being wrong
+        command.data = getpageinfo_result[0]
+        
+    get_page_info = _xmlrpc_decorator(get_page_info)
 
 
 class XMLRPCServer(Thread):
--- a/jabberbot/xmppbot.py	Fri Jun 15 21:53:58 2007 +0200
+++ b/jabberbot/xmppbot.py	Fri Jun 15 22:03:39 2007 +0200
@@ -120,7 +120,7 @@
         # A dictionary of contact objects, ordered by bare JID
         self.contacts = { }
 
-        self.known_xmlrpc_cmds = [cmd.GetPage, cmd.GetPageHTML, cmd.GetPageList] 
+        self.known_xmlrpc_cmds = [cmd.GetPage, cmd.GetPageHTML, cmd.GetPageList, cmd.GetPageInfo] 
         self.internal_commands = ["ping", "help"]
         
         self.xmlrpc_commands = {}
@@ -209,6 +209,10 @@
             list = "\n".join(command.data)
             self.send_message(command.jid, msg % (list, ))
             
+        elif isinstance(command, cmd.GetPageInfo):
+            msg = u"""Here's some more detailed information on page "%s":\n\n%s"""
+            self.send_message(command.jid, msg % (command.pagename, command.data))
+            
     def ask_for_subscription(self, jid):
         """Sends a <presence/> stanza with type="subscribe"