Newer
Older
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
import re
class ParseApiUtilsFile:
def __init__(self, filename: str):
with open(filename, 'r') as file:
# Read the contents of the file
self.file_contents = file.read()
self.variables = dict()
self.get_variables_data()
def get_response(self, keyword):
string = self.get_substring(initial_string=keyword, final_string='RETURN', include=True)
index = string.find(' ${response}')
string = string[index:]
string = string.split('\n')
index = None
for i, item in enumerate(string):
if 'response' in item:
regex = "\s{4}\$\{response\}=\s{4}(.*)"
match = re.match(pattern=regex, string=item)
if match:
verb = match.groups()[0]
elif 'url' in item:
url = item.split('/')[1]
return verb, url
def get_variables_data(self):
string = self.get_substring(initial_string='*** Variables ***\n', final_string='*** ', include=False)
regex = "(\$\{.*\})\s*(.*)\n"
matches = re.finditer(regex, string, re.MULTILINE)
for match in matches:
# Check that we have two groups matched
if len(match.groups()) == 2:
self.variables[match.group(1)] = match.group(2)
else:
print("Error, the variable is not following the format ${thing} = <value>")
def get_substring(self, initial_string: str, final_string: str, include: bool) -> str:
index_start = self.file_contents.find(initial_string)
if include:
string = self.file_contents[index_start:]
else:
string = self.file_contents[index_start+len(initial_string):]
index_end = string.find(final_string)
string = string[:index_end]
return string