From 9436e08f1fbf9929e0e9206da1590ce7c0aed022 Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Sat, 27 Apr 2024 16:54:03 +0200 Subject: [PATCH] reqif_bundle: add iterate_specification_hierarchy_for_conversion() helper --- reqif/reqif_bundle.py | 30 +++++++++++++++- .../04_convert_reqif_to_json/script.py | 34 +------------------ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/reqif/reqif_bundle.py b/reqif/reqif_bundle.py index 4cdbdf2..026f5a8 100644 --- a/reqif/reqif_bundle.py +++ b/reqif/reqif_bundle.py @@ -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 @@ -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) diff --git a/tests/integration/examples/04_convert_reqif_to_json/script.py b/tests/integration/examples/04_convert_reqif_to_json/script.py index e87fff0..821b69b 100644 --- a/tests/integration/examples/04_convert_reqif_to_json/script.py +++ b/tests/integration/examples/04_convert_reqif_to_json/script.py @@ -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, @@ -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,