|
3 | 3 | import os |
4 | 4 | from dataclasses import dataclass, field |
5 | 5 | from pathlib import Path |
6 | | -from typing import Dict, List, Optional, Union |
| 6 | +from typing import Any, Dict, List, Optional |
7 | 7 |
|
8 | 8 | from dataclasses_json import dataclass_json |
9 | 9 |
|
|
15 | 15 | ReqIFDataTypeDefinitionXHTML, |
16 | 16 | ) |
17 | 17 | from reqif.models.reqif_spec_object import ReqIFSpecObject |
| 18 | +from reqif.models.reqif_specification import ReqIFSpecification |
18 | 19 | from reqif.parser import ReqIFParser, ReqIFZParser |
19 | 20 | from reqif.reqif_bundle import ReqIFBundle, ReqIFZBundle |
20 | 21 |
|
@@ -96,54 +97,67 @@ def convert(reqif_bundle: ReqIFBundle) -> ReqDict: |
96 | 97 | specification_dict: Specification = Specification( |
97 | 98 | name=specification.long_name, nodes=[] |
98 | 99 | ) |
99 | | - section_stack: List[Union[Specification, Node]] = [ |
100 | | - specification_dict |
101 | | - ] |
102 | 100 |
|
103 | | - for ( |
104 | | - current_hierarchy |
105 | | - ) in reqif_bundle.iterate_specification_hierarchy(specification): |
| 101 | + def node_converter_lambda( |
| 102 | + current_hierarchy_, |
| 103 | + current_section_, |
| 104 | + ): |
106 | 105 | spec_object = reqif_bundle.get_spec_object_by_ref( |
107 | | - current_hierarchy.spec_object |
| 106 | + current_hierarchy_.spec_object |
108 | 107 | ) |
109 | 108 | node: Node = ReqIFToDictConverter.convert_spec_object_to_node( |
110 | 109 | spec_object, |
111 | 110 | reqif_bundle, |
112 | 111 | reqif_schema, |
113 | | - current_hierarchy.level, |
| 112 | + current_hierarchy_.level, |
114 | 113 | ) |
115 | | - current_node = section_stack[-1] |
116 | | - if not reqif_schema.is_spec_object_a_heading(spec_object): |
117 | | - if node.level > current_node.level: |
118 | | - assert node.level == ( |
119 | | - current_node.level + 1 |
120 | | - ), "Something went wrong with the spec hierarchy levels." |
121 | | - current_node.nodes.append(node) |
122 | | - elif node.level == current_node.level: |
123 | | - section_stack.pop() |
124 | | - current_node = section_stack[-1] |
125 | | - current_node.nodes.append(node) |
126 | | - else: |
127 | | - raise NotImplementedError |
128 | | - else: |
129 | | - if node.level > current_node.level: |
130 | | - section_stack.append(node) |
131 | | - current_node.nodes.append(node) |
132 | | - elif node.level == current_node.level: |
133 | | - section_stack[-1] = node |
134 | | - current_node.nodes.append(node) |
135 | | - else: |
136 | | - while current_node.level >= node.level: |
137 | | - assert not isinstance(current_node, Specification) |
138 | | - section_stack.pop() |
139 | | - current_node = section_stack[-1] |
140 | | - section_stack.append(node) |
141 | | - current_node.nodes.append(node) |
| 114 | + current_section_.nodes.append(node) |
| 115 | + is_section = reqif_schema.is_spec_object_a_heading(spec_object) |
| 116 | + return node, is_section |
| 117 | + |
| 118 | + ReqIFToDictConverter._iterate( |
| 119 | + specification, |
| 120 | + reqif_bundle, |
| 121 | + specification_dict, |
| 122 | + lambda s: s.level, |
| 123 | + node_converter_lambda, |
| 124 | + ) |
142 | 125 |
|
143 | 126 | reqif_dict.documents.append(specification_dict) |
144 | 127 |
|
145 | 128 | return reqif_dict |
146 | 129 |
|
| 130 | + @staticmethod |
| 131 | + def _iterate( |
| 132 | + specification: ReqIFSpecification, |
| 133 | + reqif_bundle: ReqIFBundle, |
| 134 | + root_node: Any, |
| 135 | + get_level_lambda, |
| 136 | + node_converter_lambda, |
| 137 | + ): |
| 138 | + section_stack: List = [root_node] |
| 139 | + |
| 140 | + for current_hierarchy in reqif_bundle.iterate_specification_hierarchy( |
| 141 | + specification |
| 142 | + ): |
| 143 | + current_section = section_stack[-1] |
| 144 | + section_level = get_level_lambda(current_section) |
| 145 | + |
| 146 | + if current_hierarchy.level <= section_level: |
| 147 | + for _ in range( |
| 148 | + 0, |
| 149 | + (section_level - current_hierarchy.level) + 1, |
| 150 | + ): |
| 151 | + assert len(section_stack) > 0 |
| 152 | + section_stack.pop() |
| 153 | + |
| 154 | + current_section = section_stack[-1] |
| 155 | + converted_node, converted_node_is_section = node_converter_lambda( |
| 156 | + current_hierarchy, current_section |
| 157 | + ) |
| 158 | + if converted_node_is_section: |
| 159 | + section_stack.append(converted_node) |
| 160 | + |
147 | 161 | @staticmethod |
148 | 162 | def convert_spec_object_to_node( |
149 | 163 | spec_object: ReqIFSpecObject, |
|
0 commit comments