changeset 2626:a791fc964e09

Attempt to move i18n from xmlrpc to xmpp thread.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Mon, 23 Jul 2007 21:49:11 +0200
parents 0a90c1bad60f
children 8e767010c418
files jabberbot/commands.py jabberbot/xmlrpcbot.py jabberbot/xmppbot.py
diffstat 3 files changed, 99 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/jabberbot/commands.py	Sun Jul 22 05:43:25 2007 +0200
+++ b/jabberbot/commands.py	Mon Jul 23 21:49:11 2007 +0200
@@ -27,6 +27,29 @@
         self.text = text
         self.subject = subject
         self.async = async
+        
+class NotificationCommandI18n(NotificationCommand):
+    """Notification request that should be translated by the XMPP bot"""
+    def __init__(self, jids, text, data={}, subject="", async=True):
+        """A constructor
+        
+        Params as in NotificationCommand.
+        
+        @param text: text to send, use %(foo)s syntax to interpolate it with data
+        @param data: dictionary of strings to interpolate into a translated message
+        @type data: dict
+        
+        """
+        NotificationCommand.__init__(self, jids, text, subject, async)
+        self.data = data
+        
+    def translate(self, gettext_func):
+        """Translate the message using a provided gettext function
+        
+        @param gettext_func: a unary gettext function
+        @return: translated message
+        """
+        return gettext_func(self.text) % self.data
 
 class AddJIDToRosterCommand:
     """Class representing a request to add a new jid to roster"""
--- a/jabberbot/xmlrpcbot.py	Sun Jul 22 05:43:25 2007 +0200
+++ b/jabberbot/xmlrpcbot.py	Mon Jul 23 21:49:11 2007 +0200
@@ -14,8 +14,6 @@
 from jabberbot.multicall import MultiCall
 from jabberbot.i18n import get_text
 
-_ = get_text
-
 class ConfigurationError(Exception):
     def __init__(self, message):
         Exception.__init__()
@@ -39,7 +37,7 @@
         self.log = logging.getLogger("log")
 
         if not config.secret:
-            error = _("You must set a (long) secret string!")
+            error = "You must set a (long) secret string!"
             self.log.critical(error)
             raise ConfigurationError(error)
 
@@ -78,8 +76,11 @@
         elif isinstance(command, cmd.GetUserLanguage):
             self.get_language_by_jid(command)
 
-    def report_error(self, jid, text):
-        report = cmd.NotificationCommand(jid, text, _("Error"), async=False)
+    def report_error(self, jid, text, data={}):
+        # Dummy function, so that the string appears in a .po file
+        _ = lambda x: x
+        
+        report = cmd.NotificationCommandI18n(jid, text, _("Error"), data, async=False)
         self.commands_out.put_nowait(report)
 
     def get_auth_token(self, jid):
@@ -101,6 +102,9 @@
 
         """
         def wrapped_func(self, command):
+            # Dummy function, so that the string appears in a .po file
+            _ = lambda x: x
+            
             self.token = None
             self.multicall = MultiCall(self.connection)
             jid = command.jid
@@ -113,16 +117,19 @@
                     if self.token:
                         self.multicall.applyAuthToken(self.token)
 
-                    function(self, command)
+                    if function(self, command) != u"SUCCESS":
+                        self.warn_no_credentials(command.jid)
+                    
                     self.commands_out.put_nowait(command)
+                    
                 except xmlrpclib.Fault, fault:
-                    msg = _("Your request has failed. The reason is:\n%s")
+                    msg = _("Your request has failed. The reason is:\n%(error)s")
                     self.log.error(str(fault))
-                    self.report_error(jid, msg % (fault.faultString, ))
+                    self.report_error(jid, msg, {'error': fault.faultString})
                 except xmlrpclib.Error, err:
-                    msg = _("A serious error occured while processing your request:\n%s")
+                    msg = _("A serious error occured while processing your request:\n%(error)s")
                     self.log.error(str(err))
-                    self.report_error(jid, msg % (str(err), ))
+                    self.report_error(jid, msg, {'error': str(err)})
                 except Exception, exc:
                     msg = _("An internal error has occured, please contact the administrator.")
                     self.log.critical(str(exc))
@@ -135,24 +142,33 @@
         return wrapped_func
 
     def warn_no_credentials(self, jid):
+        """Warn a given JID that credentials check failed
+        
+        @param jid: full JID to notify about failure
+        @type jid: str
+        
+        """
+        # Dummy function, so that the string appears in a .po file
+        _ = lambda x: x
+        
         msg = _("Credentials check failed, you may be unable to see all information.")
-        warning = cmd.NotificationCommand([jid], msg, async=False)
+        warning = cmd.NotificationCommandI18n([jid], msg, async=False)
         self.commands_out.put_nowait(warning)
 
     def get_page(self, command):
         """Returns a raw page"""
 
+        token_result = u"FAILURE"
         self.multicall.getPage(command.pagename)
-
+        
         if not self.token:
-            self.warn_no_credentials(command.jid)
             getpage_result = self.multicall()[0]
         else:
             token_result, getpage_result = self.multicall()
-            if token_result != u"SUCCESS":
-                self.warn_no_credentials(command.jid)
 
         command.data = getpage_result
+        return token_result
+        
 
     get_page = _xmlrpc_decorator(get_page)
 
@@ -160,39 +176,40 @@
     def get_page_html(self, command):
         """Returns a html-formatted page"""
 
+        token_result = u"FAILURE"
         self.multicall.getPageHTML(command.pagename)
 
         if not self.token:
-            self.warn_no_credentials(command.jid)
             getpagehtml_result = self.multicall()[0]
         else:
             token_result, getpagehtml_result = self.multicall()
-            if token_result != u"SUCCESS":
-                self.warn_no_credentials(command.jid)
 
         command.data = getpagehtml_result
+        return token_result
 
     get_page_html = _xmlrpc_decorator(get_page_html)
 
 
     def get_page_list(self, command):
         """Returns a list of all accesible pages"""
-
+                
+        # Dummy function, so that the string appears in a .po file
+        _ = lambda x: x
+        
+        token_result = u"FAILURE"
         txt = _("This command may take a while to complete, please be patient...")
-        info = cmd.NotificationCommand([command.jid], txt, async=False)
+        info = cmd.NotificationCommandI18n([command.jid], txt, async=False)
         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
             getpagelist_result = self.multicall()[0]
         else:
             token_result, getpagelist_result = self.multicall()
-            if token_result != u"SUCCESS":
-                self.warn_no_credentials(command.jid)
 
         command.data = getpagelist_result
+        return token_result
 
     get_page_list = _xmlrpc_decorator(get_page_list)
 
@@ -200,37 +217,16 @@
     def get_page_info(self, command):
         """Returns detailed information about a given page"""
 
+        token_result = u"FAILURE"
         self.multicall.getPageInfo(command.pagename)
 
         if not self.token:
-            self.warn_no_credentials(command.jid)
             getpageinfo_result = self.multicall()[0]
         else:
             token_result, getpageinfo_result = self.multicall()
-            if token_result != u"SUCCESS":
-                self.warn_no_credentials(command.jid)
-
-        author = getpageinfo_result['author']
-        if author.startswith("Self:"):
-            author = getpageinfo_result['author'][5:]
 
-        datestr = str(getpageinfo_result['lastModified'])
-        date = u"%(year)s-%(month)s-%(day)s at %(time)s" % {
-                    'year': datestr[:4],
-                    'month': datestr[4:6],
-                    'day': datestr[6:8],
-                    'time': datestr[9:17],
-                }
-
-        msg = _("""Last author: %(author)s
-Last modification: %(modification)s
-Current version: %(version)s""") % {
-             'author': author,
-             'modification': date,
-             'version': getpageinfo_result['version'],
-         }
-
-        command.data = msg
+        command.data = getpageinfo_result
+        return token_result
 
     get_page_info = _xmlrpc_decorator(get_page_info)
 
@@ -300,7 +296,7 @@
         """
         def protected_func(secret, *args):
             if secret != self.secret:
-                raise xmlrpclib.Fault(1, _("You are not allowed to use this bot!"))
+                raise xmlrpclib.Fault(1, "You are not allowed to use this bot!")
             else:
                 return function(self, *args)
 
--- a/jabberbot/xmppbot.py	Sun Jul 22 05:43:25 2007 +0200
+++ b/jabberbot/xmppbot.py	Mon Jul 23 21:49:11 2007 +0200
@@ -258,7 +258,13 @@
             for recipient in command.jids:
                 jid = JID(recipient)
                 jid_text = jid.bare().as_utf8()
-                text = command.text
+                
+                if isinstance(command, cmd.NotificationCommandI18n):
+                    # Translate&interpolate the message with data
+                    gettext_func = self.get_text(recipient)
+                    text = command.translate(gettext_func)
+                else:
+                    text = command.text
 
                 # Check if contact is DoNotDisturb.
                 # If so, queue the message for delayed delivery.
@@ -300,13 +306,32 @@
             self.send_message(command.jid, msg % (pagelist, ))
 
         elif isinstance(command, cmd.GetPageInfo):
-            msg = _("""Following detailed information on page "%(pagename)s" \
-is available::\n\n%(data)s""")
+            intro = _("""Following detailed information on page "%(pagename)s" \
+is available:""")
+            
+            if command.data['author'].startswith("Self:"):
+                author = command.data['author'][5:]
+            else:
+                author = command.data['author']
 
-            self.send_message(command.jid, msg % {
-                      'pagename': command.pagename,
-                      'data': command.data,
-            })
+            datestr = str(command.data['lastModified'])
+            date = u"%(year)s-%(month)s-%(day)s at %(time)s" % {
+                        'year': datestr[:4],
+                        'month': datestr[4:6],
+                        'day': datestr[6:8],
+                        'time': datestr[9:17],
+            }
+        
+            msg = _("""Last author: %(author)s
+Last modification: %(modification)s
+Current version: %(version)s""") % {
+             'author': author,
+             'modification': date,
+             'version': command.data['version'],
+            }
+
+            self.send_message(command.jid, intro % {'pagename': command.pagename})
+            self.send_message(command.jid, msg)
 
         elif isinstance(command, cmd.GetUserLanguage):
             if command.jid in self.contacts: