Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython3Tester.py
More file actions
Latest commit
95 lines (80 loc) · 3.2 KB
/
Copy pathPython3Tester.py
File metadata and controls
95 lines (80 loc) · 3.2 KB
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
importre
importjson
importsys
classPython3Tester:
def__init__(self):
pass
deftest_regex(self, config, test_strings):
try:
flags=0
if"IGNORECASE"inconfigandconfig["IGNORECASE"] isTrue:
flags|=re.I
if"LOCALE"inconfigandconfig["LOCALE"] isTrue:
flags|=re.L
if"MULTILINE"inconfigandconfig["MULTILINE"] isTrue:
flags|=re.M
if"DOTALL"inconfigandconfig["DOTALL"] isTrue:
flags|=re.D
if"ASCII"inconfigandconfig["ASCII"] isTrue:
flags|=re.A
if"VERBOSE"inconfigandconfig["VERBOSE"] isTrue:
flags|=re.V
if"test_type"notinconfig:
raiseException("test_type parameter doesn't exists.")
test_type=config["test_type"]
regex=config["regex"]
pattern=re.compile(regex, flags)
result= {
"type": "",
"result": {
"resultList": []
}
}
iftest_type=="match":
result["type"] ="MATCH"
fortest_stringintest_strings:
ifpattern.match(test_string):
result["result"]["resultList"].append(True)
else:
result["result"]["resultList"].append(False)
eliftest_type=="group":
result["type"] ="GROUP"
fortest_stringintest_strings:
iterator=pattern.finditer(test_string)
groupsList= {"list": []}
result["result"]["resultList"].append(groupsList)
formatchiniterator:
groups= []
forgroupinmatch.groups():
groups.append(group)
groupsList["list"].append(groups);
eliftest_type=="replace":
fortest_stringintest_strings:
result["type"] ="STRING"
replace_string=""
if"replace"inconfig:
replace_string=config["replace"]
replaced=pattern.sub(replace_string, test_string)
result["result"]["resultList"].append(replaced)
eliftest_type=="findall":
result["type"] ="GROUP"
fortest_stringintest_strings:
groupsList= {"list": [[]]}
result["result"]["resultList"].append(groupsList)
found=pattern.findall(test_string)
forwordinfound:
groupsList["list"][0].append(word);
exceptExceptionase:
result= {
"exception": str(e)
}
print("##START_RESULT##")
print(json.dumps(result))
print("##END_RESULT##")
defmain():
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()