summaryrefslogtreecommitdiff
path: root/service/app/search_query.py
diff options
context:
space:
mode:
Diffstat (limited to 'service/app/search_query.py')
-rw-r--r--service/app/search_query.py42
1 files changed, 42 insertions, 0 deletions
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