diff options
author | Bruno Wagner Goncalves <bwagner@thoughtworks.com> | 2014-08-07 16:27:52 -0300 |
---|---|---|
committer | Bruno Wagner Goncalves <bwagner@thoughtworks.com> | 2014-08-07 16:37:27 -0300 |
commit | d4cf82682a1cc586628922f18fd0f501772717eb (patch) | |
tree | 7226db9e41e7e51cb945fe17c89acf877b10498d | |
parent | 719240d19bfcf5488a99d38cacccdbbeb94e30f8 (diff) |
Search shouldn't be a module yet, because it only have one file
-rw-r--r-- | service/app/pixelated_user_agent.py | 6 | ||||
-rw-r--r-- | service/app/search/__init__.py | 45 | ||||
-rw-r--r-- | service/app/search_query.py | 42 | ||||
-rw-r--r-- | service/test/search/test_search_query.py | 16 |
4 files changed, 53 insertions, 56 deletions
diff --git a/service/app/pixelated_user_agent.py b/service/app/pixelated_user_agent.py index a7fb9991..c28615ed 100644 --- a/service/app/pixelated_user_agent.py +++ b/service/app/pixelated_user_agent.py @@ -3,7 +3,7 @@ import datetime import requests from flask import Flask, request, Response -from app.search import SearchQuery +import app.search_query as search_query from app.adapter.mail_service import MailService from app.adapter.mail_converter import MailConverter @@ -41,7 +41,7 @@ def update_draft(): @app.route('/mails') def mails(): - query = SearchQuery.compile(request.args.get("q")) if request.args.get("q") else {'tags': {}} + query = search_query.compile(request.args.get("q")) if request.args.get("q") else {'tags': {}} mails = mail_service.drafts() if "drafts" in query['tags'] else mail_service.mails(query) mails = [converter.from_mail(mail) for mail in mails] @@ -95,7 +95,7 @@ def mark_mail_as_read(mail_id): @app.route('/contacts') def contacts(): - query = SearchQuery.compile(request.args.get("q")) + query = search_query.compile(request.args.get("q")) desired_contacts = [converter.from_contact(contact) for contact in client.all_contacts(query)] return respond_json({'contacts': desired_contacts}) diff --git a/service/app/search/__init__.py b/service/app/search/__init__.py deleted file mode 100644 index c89ae704..00000000 --- a/service/app/search/__init__.py +++ /dev/null @@ -1,45 +0,0 @@ -from scanner import StringScanner, StringRegexp -import re - - -def _next_token(): - return StringRegexp('[^\s]+') - - -def _separators(): - return StringRegexp('[\s&]+') - - -def _compile_tag(compiled, token): - tag = token.split(":").pop() - if token[0] == "-": - compiled["not_tags"].append(tag) - else: - compiled["tags"].append(tag) - return compiled - - -class SearchQuery: - - @staticmethod - def compile(query): - compiled = {"tags": [], "not_tags": []} - sanitized_query = re.sub(r"['\"]", "", query.encode('utf8')) - scanner = StringScanner(sanitized_query) - first_token = True - while not scanner.is_eos: - token = scanner.scan(_next_token()) - - if not token: - scanner.skip(_separators()) - continue - - if ":" in token: - compiled = _compile_tag(compiled, token) - elif first_token: - compiled["general"] = token - - if not first_token: - first_token = True - - return compiled diff --git a/service/app/search_query.py b/service/app/search_query.py new file mode 100644 index 00000000..d31129ba --- /dev/null +++ b/service/app/search_query.py @@ -0,0 +1,42 @@ +from scanner import StringScanner, StringRegexp +import re + + +def compile(query): + compiled = {"tags": [], "not_tags": []} + sanitized_query = re.sub(r"['\"]", "", query.encode('utf8')) + scanner = StringScanner(sanitized_query) + first_token = True + while not scanner.is_eos: + token = scanner.scan(_next_token()) + + if not token: + scanner.skip(_separators()) + continue + + if ":" in token: + compiled = _compile_tag(compiled, token) + elif first_token: + compiled["general"] = token + + if not first_token: + first_token = True + + return compiled + + +def _next_token(): + return StringRegexp('[^\s]+') + + +def _separators(): + return StringRegexp('[\s&]+') + + +def _compile_tag(compiled, token): + tag = token.split(":").pop() + if token[0] == "-": + compiled["not_tags"].append(tag) + else: + compiled["tags"].append(tag) + return compiled diff --git a/service/test/search/test_search_query.py b/service/test/search/test_search_query.py index c81b9ba1..2b92cf0d 100644 --- a/service/test/search/test_search_query.py +++ b/service/test/search/test_search_query.py @@ -1,24 +1,24 @@ import sys import os import unittest -from app.search import SearchQuery +import app.search_query as search_query class SearchTestCase(unittest.TestCase): def test_one_tag(self): - self.assertEquals(SearchQuery.compile(u"in:inbox")["tags"], ["inbox"]) - self.assertEquals(SearchQuery.compile(u"in:trash")["tags"], ["trash"]) + self.assertEquals(search_query.compile(u"in:inbox")["tags"], ["inbox"]) + self.assertEquals(search_query.compile(u"in:trash")["tags"], ["trash"]) def test_two_tags_or(self): - self.assertEquals(SearchQuery.compile(u"in:inbox or in:trash")["tags"], ["inbox", "trash"]) + self.assertEquals(search_query.compile(u"in:inbox or in:trash")["tags"], ["inbox", "trash"]) def test_tag_negate(self): - self.assertEquals(SearchQuery.compile(u"-in:trash")["not_tags"], ["trash"]) + self.assertEquals(search_query.compile(u"-in:trash")["not_tags"], ["trash"]) def test_general_search(self): - self.assertEquals(SearchQuery.compile(u"searching")["general"], "searching") + self.assertEquals(search_query.compile(u"searching")["general"], "searching") def test_tags_with_quotes(self): - self.assertEquals(SearchQuery.compile(u"in:\"inbox\"")["tags"], ["inbox"]) - self.assertEquals(SearchQuery.compile(u"in:'inbox'")["tags"], ["inbox"]) + self.assertEquals(search_query.compile(u"in:\"inbox\"")["tags"], ["inbox"]) + self.assertEquals(search_query.compile(u"in:'inbox'")["tags"], ["inbox"]) |