commit 4fa4d6368c3c8dd40090196c6ff1cebb9c2ad5e7 Author: Cecylia Bocovich cohosh@torproject.org Date: Mon Jan 27 09:58:26 2020 -0500
Refactor email keyword parser
This commit refactors build_request to prevent duplicate code. It also moves the platforms definition to the parser constructor. --- gettor/parse/email.py | 38 +++++++++++++++++--------------------- tests/test_email_service.py | 3 +-- 2 files changed, 18 insertions(+), 23 deletions(-)
diff --git a/gettor/parse/email.py b/gettor/parse/email.py index 56b91bf..d487684 100644 --- a/gettor/parse/email.py +++ b/gettor/parse/email.py @@ -57,6 +57,7 @@ class EmailParser(object): self.dkim = dkim self.to_addr = to_addr self.locales = [] + self.platforms = self.settings.get("platforms")
def normalize(self, msg): # Normalization will convert <Alice Wonderland> alice@wonderland.net @@ -112,8 +113,20 @@ class EmailParser(object): else: return True
+ def parse_keywords(self, text, request): + + for word in re.split(r"\s+", text.strip()): + if word.lower() in self.locales: + request["language"] = word.lower() + if word.lower() in self.platforms: + request["command"] = "links" + request["platform"] = word.lower() + if word.lower() == "help": + request["command"] = "help" + break + return request
- def build_request(self, msg_str, norm_addr, platforms): + def build_request(self, msg_str, norm_addr): # Search for commands keywords subject_re = re.compile(r"Subject: (.*)\r\n") subject = subject_re.search(msg_str) @@ -128,26 +141,10 @@ class EmailParser(object):
if subject: subject = subject.group(1) - for word in re.split(r"\s+", subject.strip()): - if word.lower() in self.locales: - request["language"] = word.lower() - if word.lower() in platforms: - request["command"] = "links" - request["platform"] = word.lower() - if word.lower() == "help": - request["command"] = "help" - break + request = self.parse_keywords(subject, request)
if not request["command"] or not request["language"]: - for word in re.split(r"\s+", msg_str.strip()): - if word.lower() in self.locales: - request["language"] = word.lower() - if word.lower() in platforms: - request["command"] = "links" - request["platform"] = word.lower() - if word.lower() == "help": - request["command"] = "help" - break + request = self.parse_keywords(msg_str, request)
return request
@@ -186,7 +183,6 @@ class EmailParser(object):
log.msg("Building email message from string.", system="email parser")
- platforms = self.settings.get("platforms") msg = message_from_string(msg_str)
name, norm_addr, to_name, norm_to_addr = self.normalize(msg) @@ -212,7 +208,7 @@ class EmailParser(object): except ValueError as e: log.msg("DKIM error: {}".format(e.args))
- request = self.build_request(msg_str, norm_addr, platforms) + request = self.build_request(msg_str, norm_addr)
return request
diff --git a/tests/test_email_service.py b/tests/test_email_service.py index 5fa87fc..407937c 100644 --- a/tests/test_email_service.py +++ b/tests/test_email_service.py @@ -65,9 +65,8 @@ class EmailServiceTests(unittest.TestCase): ep = conftests.EmailParser(self.settings, "gettor@torproject.org") msg_str = "From: "silvia [hiro]" hiro@torproject.org\n Subject: \r\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org\r\n osx es" msg = conftests.message_from_string(msg_str) - platforms = self.settings.get('platforms') ep.locales = ["es", "en"] - request = ep.build_request(msg_str, "hiro@torproject.org", platforms) + request = ep.build_request(msg_str, "hiro@torproject.org") self.assertEqual(request["command"], "links") self.assertEqual(request["platform"], "osx") self.assertEqual(request["language"], "es")
tor-commits@lists.torproject.org