[tor-commits] [gettor/master] Use locales

hiro at torproject.org hiro at torproject.org
Wed May 22 15:40:27 UTC 2019


commit ae37e31253e9a1f0a41ff2758b10616b7e436e16
Author: hiro <hiro at torproject.org>
Date:   Wed May 22 17:22:35 2019 +0200

    Use locales
---
 gettor.conf.json                    |  1 +
 gettor/parse/email.py               | 10 +++++++---
 gettor/services/email/sendmail.py   |  9 +++------
 gettor/utils/settings.py            |  1 +
 scripts/add_lins_to_db              | 14 +++++++++-----
 share/locale/available_locales.json |  4 +++-
 share/locale/es.json                | 16 ++++++++++++++++
 share/locale/pt.json                | 16 ++++++++++++++++
 tests/conftests.py                  |  1 +
 tests/test_email_service.py         | 11 +++++++++++
 10 files changed, 68 insertions(+), 15 deletions(-)

diff --git a/gettor.conf.json b/gettor.conf.json
index b3f331d..6a9b420 100644
--- a/gettor.conf.json
+++ b/gettor.conf.json
@@ -1,5 +1,6 @@
 {
   "platforms": ["linux", "osx", "windows"],
+  "languages": ["en", "es", "pt"],
   "dbname": "/srv/gettor.torproject.org/home/gettor/gettor.db",
   "email_parser_logfile": "/srv/gettor.torproject.org/home/gettor/log/email_parser.log",
   "email_requests_limit": 5,
diff --git a/gettor/parse/email.py b/gettor/parse/email.py
index 06a149b..d79a375 100644
--- a/gettor/parse/email.py
+++ b/gettor/parse/email.py
@@ -73,6 +73,7 @@ class EmailParser(object):
         """
 
         platforms = self.settings.get("platforms")
+        languages = self.settings.get("languages")
         log.msg("Building email message from string.", system="email parser")
         msg = message_from_string(msg_str)
 
@@ -137,26 +138,29 @@ class EmailParser(object):
             "id": norm_addr,
             "command": None,
             "platform": None,
+            "language": None,
             "service": "email"
         }
 
         if subject:
             subject = subject.group(1)
             for word in re.split(r"\s+", subject.strip()):
+                if word.lower() in languages:
+                    request["language"] = word.lower()
                 if word.lower() in platforms:
                     request["command"] = "links"
                     request["platform"] = word.lower()
-                    break
                 if word.lower() == "help":
                     request["command"] = "help"
                     break
 
-        if not request["command"]:
+        if not request["command"] and not request["language"]:
             for word in re.split(r"\s+", msg_str.strip()):
+                if word.lower() in languages:
+                    request["language"] = word.lower()
                 if word.lower() in platforms:
                     request["command"] = "links"
                     request["platform"] = word.lower()
-                    break
                 if word.lower() == "help":
                     request["command"] = "help"
                     break
diff --git a/gettor/services/email/sendmail.py b/gettor/services/email/sendmail.py
index bce20da..f18b335 100644
--- a/gettor/services/email/sendmail.py
+++ b/gettor/services/email/sendmail.py
@@ -117,13 +117,8 @@ class Sendmail(object):
             status="ONHOLD", command="links", service="email"
         )
 
-        """
-        Load strings for translations
-        """
-        # for now just english
-        strings.load_strings("en")
-
         if help_requests:
+            strings.load_strings("en")
             try:
                 log.info("Got new help request.")
 
@@ -164,6 +159,8 @@ class Sendmail(object):
                     id = request[0]
                     date = request[4]
                     platform = request[2]
+                    language = request[3]
+                    strings.load_strings(language)
 
                     log.info("Getting links for {}.".format(platform))
                     links = yield self.conn.get_links(
diff --git a/gettor/utils/settings.py b/gettor/utils/settings.py
index b301117..922cfe9 100644
--- a/gettor/utils/settings.py
+++ b/gettor/utils/settings.py
@@ -59,6 +59,7 @@ class Settings(object):
         else:
             self._settings = {
               "platforms": ["linux", "osx", "windows"],
+              "languages": ["en", "es", "pt"],
               "dbname": "/srv/gettor.torproject.org/home/gettor/gettor.db",
               "email_parser_logfile": "/srv/gettor.torproject.org/home/gettor/log/email_parser.log",
               "email_requests_limit": 5,
diff --git a/scripts/add_lins_to_db b/scripts/add_lins_to_db
index 55fe127..e8e89f2 100755
--- a/scripts/add_lins_to_db
+++ b/scripts/add_lins_to_db
@@ -77,9 +77,11 @@ def main():
     "linux": "_en-US.tar.xz"
   }
 
-  keys = {'osx', 'windows', 'linux'}
-  releases = {k: "".join(dic.get(k, version) for dic in (prefixes, versions, suffixes))  for k in keys}
+  keys = ['osx', 'windows', 'linux']
+
+  languages = ['en-US', 'es-ES', 'pt-BR']
 
+  releases = {k: "".join(dic.get(k, version) for dic in (prefixes, versions, suffixes))  for k in keys}
 
   if not abs_filename:
       print("Missing database filename.")
@@ -97,9 +99,11 @@ def main():
           )
           for k in keys:
               for p in providers:
-                  c.execute(
-                    "INSERT INTO links(link, platform, arch, version, provider, status)"
-                    "VALUES ('%s', '%s', '64', '%s', '%s', 'ACTIVE')" %(providers.get(p) + releases.get(k), k, version, p))
+                  for l in languages:
+                      release_link = releases.get(k).replace("en-US", l)
+                      c.execute(
+                        "INSERT INTO links(link, platform, arch, version, provider, status)"
+                        "VALUES ('%s', '%s', '64', '%s', '%s', 'ACTIVE')" %(providers.get(p) + release_link, k, version, p))
 
 
 if __name__ == "__main__":
diff --git a/share/locale/available_locales.json b/share/locale/available_locales.json
index a259b5c..d91a253 100644
--- a/share/locale/available_locales.json
+++ b/share/locale/available_locales.json
@@ -1,3 +1,5 @@
 {
-  "en": "English"
+  "en": "English",
+  "es": "Español",
+  "pt": "Português Brasil"
 }
diff --git a/share/locale/es.json b/share/locale/es.json
new file mode 100644
index 0000000..3006c7c
--- /dev/null
+++ b/share/locale/es.json
@@ -0,0 +1,16 @@
+{
+  "links_body": "You requested Tor Browser for {}.\n\nYou will need only one of the links below to download the bundle. If a link does not work for you, try the next one.\n\n{}\n\n \n--\nGetTor",
+  "links_subject": "[GetTor] Links for your request",
+  "help_body": "This is how you can request a tor browser bundle link.\n\n Send an email to: gettor at torproject.org\n\nIn the body of the email only write: <operating system> <language>.\n\nWe only support windows, osx and linux as operating systems.\n\nAt the moment please only use en as language.\n\n",
+  "help_subject": "[GetTor] Help Email",
+  "help_debug": "Log application errors to stdout",
+  "help_config": "Custom config file location (optional)",
+  "smtp_links_subject": "[GetTor] Links for your request",
+  "smtp_mirrors_subject": "[GetTor] Mirrors",
+  "smtp_help_subject": "[GetTor] Help",
+  "smtp_unsupported_locale_subject": "[GetTor] Unsupported locale",
+  "smtp_unsupported_locale_msg": "The locale you requested '{}' is not supported.",
+  "smtp_vlinks_msg": "You requested Tor Browser for {}.\n\nYou will need only one of the links below to download the bundle. If a link does not work for you, try the next one.\n\n{}\n\n \n--\nGetTor",
+  "smtp_mirrors_msg": "Hi! this is the GetTor robot.\n\nThank you for your request. Attached to this email you will find\nan updated list of mirrors of Tor Project's website.",
+  "smtp_help_msg": "Hi! This is the GetTor robot. I am here to help you download the\nlatest version of Tor Browser.\n\nPlease reply to this message with one of the options below:\n\nwindows\nlinux\nosx\nmirrors\n\nI will then send you the download instructions.\n\nIf you are unsure, just send a blank reply to this message."
+}
diff --git a/share/locale/pt.json b/share/locale/pt.json
new file mode 100644
index 0000000..3006c7c
--- /dev/null
+++ b/share/locale/pt.json
@@ -0,0 +1,16 @@
+{
+  "links_body": "You requested Tor Browser for {}.\n\nYou will need only one of the links below to download the bundle. If a link does not work for you, try the next one.\n\n{}\n\n \n--\nGetTor",
+  "links_subject": "[GetTor] Links for your request",
+  "help_body": "This is how you can request a tor browser bundle link.\n\n Send an email to: gettor at torproject.org\n\nIn the body of the email only write: <operating system> <language>.\n\nWe only support windows, osx and linux as operating systems.\n\nAt the moment please only use en as language.\n\n",
+  "help_subject": "[GetTor] Help Email",
+  "help_debug": "Log application errors to stdout",
+  "help_config": "Custom config file location (optional)",
+  "smtp_links_subject": "[GetTor] Links for your request",
+  "smtp_mirrors_subject": "[GetTor] Mirrors",
+  "smtp_help_subject": "[GetTor] Help",
+  "smtp_unsupported_locale_subject": "[GetTor] Unsupported locale",
+  "smtp_unsupported_locale_msg": "The locale you requested '{}' is not supported.",
+  "smtp_vlinks_msg": "You requested Tor Browser for {}.\n\nYou will need only one of the links below to download the bundle. If a link does not work for you, try the next one.\n\n{}\n\n \n--\nGetTor",
+  "smtp_mirrors_msg": "Hi! this is the GetTor robot.\n\nThank you for your request. Attached to this email you will find\nan updated list of mirrors of Tor Project's website.",
+  "smtp_help_msg": "Hi! This is the GetTor robot. I am here to help you download the\nlatest version of Tor Browser.\n\nPlease reply to this message with one of the options below:\n\nwindows\nlinux\nosx\nmirrors\n\nI will then send you the download instructions.\n\nIf you are unsure, just send a blank reply to this message."
+}
diff --git a/tests/conftests.py b/tests/conftests.py
index 4c692ef..206b2f4 100644
--- a/tests/conftests.py
+++ b/tests/conftests.py
@@ -4,3 +4,4 @@ from __future__ import unicode_literals
 
 from gettor.utils import options
 from gettor.services.email import sendmail
+from gettor.parse.email import EmailParser, AddressError, DKIMError
diff --git a/tests/test_email_service.py b/tests/test_email_service.py
index ef00d52..a4a7405 100644
--- a/tests/test_email_service.py
+++ b/tests/test_email_service.py
@@ -20,6 +20,17 @@ class EmailServiceTests(unittest.TestCase):
     def test_get_interval(self):
         self.assertEqual(self.settings.get("sendmail_interval"), self.sm_client.get_interval())
 
+    def test_help_email_parser(self):
+        ep = conftests.EmailParser(self.settings, "gettor at torproject.org")
+        request = ep.parse("From: \"silvia [hiro]\" <hiro at torproject.org>\n Subject: help\n Reply-To: hiro at torproject.org \nTo: gettor at torproject.org")
+        self.assertEqual(request["command"], "help")
+
+    def test_language_email_parser(self):
+        ep = conftests.EmailParser(self.settings, "gettor at torproject.org")
+        request = ep.parse("From: \"silvia [hiro]\" <hiro at torproject.org>\n Subject: osx en\r\n Reply-To: hiro at torproject.org \nTo: gettor at torproject.org\n osx English")
+        self.assertEqual(request["command"], "links")
+        self.assertEqual(request["platform"], "osx")
+        self.assertEqual(request["language"], "en")
 
 
 if __name__ == "__main__":





More information about the tor-commits mailing list