Skip to content

Commit

Permalink
Merge pull request #164 from strictdoc-project/stanislaw/capella_fixture
Browse files Browse the repository at this point in the history
reqif_bundle: add iterate_specification_hierarchy_for_conversion() helper
  • Loading branch information
stanislaw authored Apr 27, 2024
2 parents da65059 + 9436e08 commit 6ea9a66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
30 changes: 29 additions & 1 deletion reqif/reqif_bundle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import collections
from typing import Deque, Dict, Iterator, List, Optional
from typing import Deque, Dict, Iterator, List, Optional, Any

from reqif.helpers.debug import auto_described
from reqif.models.error_handling import ReqIFSchemaError
Expand Down Expand Up @@ -75,6 +75,34 @@ def iterate_specification_hierarchy(
if current.children is not None:
task_list.extendleft(reversed(current.children))

def iterate_specification_hierarchy_for_conversion(
self,
specification: ReqIFSpecification,
root_node: Any,
get_level_lambda,
node_lambda,
):
section_stack: List[Any] = [root_node]

for current_hierarchy in self.iterate_specification_hierarchy(specification):
current_section = section_stack[-1]
section_level = get_level_lambda(current_section)

if current_hierarchy.level <= section_level:
for _ in range(
0,
(section_level - current_hierarchy.level) + 1,
):
assert len(section_stack) > 0
section_stack.pop()

current_section = section_stack[-1]
converted_node, converted_node_is_section = node_lambda(
current_hierarchy, current_section
)
if converted_node_is_section:
section_stack.append(converted_node)

def get_spec_object_by_ref(self, ref) -> ReqIFSpecObject:
return self.lookup.get_spec_object_by_ref(ref)

Expand Down
34 changes: 1 addition & 33 deletions tests/integration/examples/04_convert_reqif_to_json/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ def node_converter_lambda(
is_section = reqif_schema.is_spec_object_a_heading(spec_object)
return node, is_section

ReqIFToDictConverter._iterate(
reqif_bundle.iterate_specification_hierarchy_for_conversion(
specification,
reqif_bundle,
specification_dict,
lambda s: s.level,
node_converter_lambda,
Expand All @@ -127,37 +126,6 @@ def node_converter_lambda(

return reqif_dict

@staticmethod
def _iterate(
specification: ReqIFSpecification,
reqif_bundle: ReqIFBundle,
root_node: Any,
get_level_lambda,
node_converter_lambda,
):
section_stack: List = [root_node]

for current_hierarchy in reqif_bundle.iterate_specification_hierarchy(
specification
):
current_section = section_stack[-1]
section_level = get_level_lambda(current_section)

if current_hierarchy.level <= section_level:
for _ in range(
0,
(section_level - current_hierarchy.level) + 1,
):
assert len(section_stack) > 0
section_stack.pop()

current_section = section_stack[-1]
converted_node, converted_node_is_section = node_converter_lambda(
current_hierarchy, current_section
)
if converted_node_is_section:
section_stack.append(converted_node)

@staticmethod
def convert_spec_object_to_node(
spec_object: ReqIFSpecObject,
Expand Down

0 comments on commit 6ea9a66

Please sign in to comment.