-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python3Tester.py
95 lines (80 loc) · 3.2 KB
/
Python3Tester.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import re
import json
import sys
class Python3Tester:
def __init__(self):
pass
def test_regex(self, config, test_strings):
try:
flags = 0
if "IGNORECASE" in config and config["IGNORECASE"] is True:
flags |= re.I
if "LOCALE" in config and config["LOCALE"] is True:
flags |= re.L
if "MULTILINE" in config and config["MULTILINE"] is True:
flags |= re.M
if "DOTALL" in config and config["DOTALL"] is True:
flags |= re.D
if "ASCII" in config and config["ASCII"] is True:
flags |= re.A
if "VERBOSE" in config and config["VERBOSE"] is True:
flags |= re.V
if "test_type" not in config:
raise Exception("test_type parameter doesn't exists.")
test_type = config["test_type"]
regex = config["regex"]
pattern = re.compile(regex, flags)
result = {
"type": "",
"result": {
"resultList": []
}
}
if test_type == "match":
result["type"] = "MATCH"
for test_string in test_strings:
if pattern.match(test_string):
result["result"]["resultList"].append(True)
else:
result["result"]["resultList"].append(False)
elif test_type == "group":
result["type"] = "GROUP"
for test_string in test_strings:
iterator = pattern.finditer(test_string)
groupsList = {"list": []}
result["result"]["resultList"].append(groupsList)
for match in iterator:
groups = []
for group in match.groups():
groups.append(group)
groupsList["list"].append(groups);
elif test_type == "replace":
for test_string in test_strings:
result["type"] = "STRING"
replace_string = ""
if "replace" in config:
replace_string = config["replace"]
replaced = pattern.sub(replace_string, test_string)
result["result"]["resultList"].append(replaced)
elif test_type == "findall":
result["type"] = "GROUP"
for test_string in test_strings:
groupsList = {"list": [[]]}
result["result"]["resultList"].append(groupsList)
found = pattern.findall(test_string)
for word in found:
groupsList["list"][0].append(word);
except Exception as e:
result = {
"exception": str(e)
}
print("##START_RESULT##")
print(json.dumps(result))
print("##END_RESULT##")
def main():
config = json.loads(sys.argv[1])
test_strings = json.loads(sys.argv[2])
tester = Python3Tester()
tester.test_regex(config, test_strings)
if __name__ == "__main__":
main()