-
Notifications
You must be signed in to change notification settings - Fork 8
/
query.py
68 lines (56 loc) · 2.12 KB
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import re
AND_QUERY_RE_STR = '\\s*&\\s*'
class Query:
def __init__(self, query, strip_padding=False):
"""
Initializes a new CVE query object
:param query: The search string
:param strip_padding: Should we strip any padding from this query object?
"""
self.is_negative = False
self.required_tags = []
self.left_padded = False
self.right_padded = False
self.strip_padding = False
self.query = query
# Parse parameters from query
self.__parse_fields()
if strip_padding:
self.query = query.strip()
if self.left_padded:
self.query = self.query[2:]
self.query = ' {}'.format(query.lstrip())
if self.right_padded:
self.query = self.query[:-2]
self.query = '{} '.format(query.rstrip())
def __parse_fields(self):
split_query = re.split(AND_QUERY_RE_STR, self.query)
self.required_tags = [tag.strip() for tag in split_query[1:]]
self.query = split_query[0]
self.is_negative = self.query.startswith('-')
if self.is_negative:
self.query = self.query[1:]
self.left_padded = self.query.startswith('__')
if self.left_padded:
self.query = self.query[2:]
self.right_padded = self.query.endswith('__')
if self.right_padded:
self.query = self.query[:-2]
def matches(self, text):
if not self.is_negative:
matches = self.query in text
else:
matches = self.query not in text
if matches:
for extra in self.required_tags:
tag_is_negative = extra.startswith('-')
if tag_is_negative:
# If our required tag is blacklisted and not in the text
extra = extra[1:]
matches = extra.lower() not in text
else:
# If our required tag is whitelisted and in the text
matches = extra.lower() in text
if not matches:
break
return matches