We want to parse a file and create a data structure of some sort to be used later (in Python). The content of file looks like this:
plan HELLO
feature A
measure X :
src = "Type ,Name"
endmeasure //X
measure Y :
src = "Type ,Name"
endmeasure //Y
feature Aa
measure AaX :
src = "Type ,Name"
"Type ,Name2"
"Type ,Name3"
endmeasure //AaX
measure AaY :
src = "Type ,Name"
endmeasure //AaY
feature Aab
.....
endfeature // Aab
endfeature //Aa
endfeature // A
feature B
......
endfeature //B
endplan
plan HOLA
endplan //HOLA
So there's a file that contain one or more plans and then each plan contains one or more feature, further each feature contains a measure that contains info (src, type, name) and feature can further contain more features.
We need to parse through the file and create a data structure that would have
plan (HELLO)
------------------------------
↓ ↓
Feature A Feature B
---------------------------- ↓
↓ ↓ ↓ ........
Measure X Measure Y Feature Aa
------------------------------
↓ ↓ ↓
Measure AaX Measure AaY Feature Aab
↓
.......
I am trying to parse through the file line by line and create a list of lists that would contain plan -> feature -> measure, feature
def getplans(s):
stack = [{}]
stack_list = []
for line in s.splitlines():
if ": " in line: # leaf
temp_stack = {}
key, value = line.split(": ", 1)
key = key.replace("source","").replace("=","").replace("\"","").replace(";","")
value = value.replace("\"","").replace(",","").replace(";","")
temp_stack[key.strip()] = value.strip()
stack_list.append(temp_stack)
stack[-1]["MEASURED_VAL"] = stack_list
elif line.strip()[:3] == "end":
stack.pop()
stack_list = []
elif line.strip():
collection, name, *_ = line.split()
stack.append({})
stack[-2].setdefault(collection, {})[name] = stack[-1]
return stack[0]
Looking at the file, I'd try to convert it the
plan/feature/measureto tags and then parse it with HTML parser, for examplebeautifulsoup(or you can try the same with YAML and then use Yaml parser):The converted text will be:
And output: