|
16 | 16 |
|
17 | 17 | from strictdoc.backend.sdoc.document_reference import DocumentReference |
18 | 18 | from strictdoc.backend.sdoc.error_handling import StrictDocSemanticError |
19 | | -from strictdoc.backend.sdoc.models.document import SDocDocument |
20 | 19 | from strictdoc.backend.sdoc.models.document_grammar import ( |
21 | 20 | DocumentGrammar, |
22 | 21 | ) |
23 | | -from strictdoc.backend.sdoc.models.model import SDocDocumentIF, SDocNodeIF |
| 22 | +from strictdoc.backend.sdoc.models.model import SDocDocumentIF |
24 | 23 | from strictdoc.backend.sdoc.models.node import SDocNode |
25 | 24 | from strictdoc.backend.sdoc.models.reference import FileEntry, FileReference |
26 | 25 | from strictdoc.backend.sdoc_source_code.models.function import Function |
@@ -568,34 +567,9 @@ def validate_and_resolve( |
568 | 567 | # STEP: Create auto-generated documents created from source file comments. |
569 | 568 | # Register these documents with the main traceability index. |
570 | 569 | # |
571 | | - def create_folder_section( |
572 | | - parent__: Union[SDocDocumentIF, SDocNodeIF], |
573 | | - document__: SDocDocument, |
574 | | - path__: str, |
575 | | - ) -> SDocNode: |
576 | | - section_node = SDocNode( |
577 | | - parent=parent__, |
578 | | - node_type="SECTION", |
579 | | - fields=[], |
580 | | - relations=[], |
581 | | - is_composite=True, |
582 | | - node_type_close="SECTION", |
583 | | - # It is important that this autogenerated node is marked as such. |
584 | | - autogen=True, |
585 | | - ) |
586 | | - section_node.ng_document_reference = DocumentReference() |
587 | | - section_node.ng_document_reference.set_document(document__) |
588 | | - section_node.ng_including_document_reference = DocumentReference() |
589 | | - section_node.set_field_value( |
590 | | - field_name="TITLE", |
591 | | - form_field_index=0, |
592 | | - value=path__, |
593 | | - ) |
594 | | - return section_node |
595 | | - |
596 | 570 | documents_with_generated_content = set() |
597 | 571 |
|
598 | | - section_cache = {} |
| 572 | + section_cache: Dict[str, Union[SDocDocumentIF, SDocNode]] = {} |
599 | 573 | source_nodes_config: List[SourceNodesEntry] = ( |
600 | 574 | project_config.source_nodes |
601 | 575 | ) |
@@ -627,24 +601,7 @@ def create_folder_section( |
627 | 601 | document_uid = relevant_source_node_entry.uid |
628 | 602 | document = traceability_index.get_node_by_uid(document_uid) |
629 | 603 | documents_with_generated_content.add(document) |
630 | | - |
631 | | - current_top_node = document |
632 | | - path_components = path_to_source_file_.split("/") |
633 | | - for path_component_idx_, path_component_ in enumerate( |
634 | | - path_components |
635 | | - ): |
636 | | - if path_component_ not in section_cache: |
637 | | - path_component_title = ( |
638 | | - path_component_ + "/" |
639 | | - if path_component_idx_ < (len(path_components) - 1) |
640 | | - else path_component_ |
641 | | - ) |
642 | | - current_section = create_folder_section( |
643 | | - current_top_node, document, path_component_title |
644 | | - ) |
645 | | - current_top_node.section_contents.append(current_section) |
646 | | - section_cache[path_component_] = current_section |
647 | | - current_top_node = section_cache[path_component_] |
| 604 | + current_top_node = None |
648 | 605 |
|
649 | 606 | for source_node_ in traceability_info_.source_nodes: |
650 | 607 | if len(source_node_.fields) == 0: |
@@ -676,12 +633,20 @@ def create_folder_section( |
676 | 633 | document, |
677 | 634 | ) |
678 | 635 | sdoc_node_uid = assert_cast(sdoc_node.reserved_uid, str) |
679 | | - current_top_node.section_contents.append(sdoc_node) |
680 | 636 | traceability_index.graph_database.create_link( |
681 | 637 | link_type=GraphLinkType.UID_TO_NODE, |
682 | 638 | lhs_node=sdoc_node_uid, |
683 | 639 | rhs_node=sdoc_node, |
684 | 640 | ) |
| 641 | + if current_top_node is None: |
| 642 | + current_top_node = ( |
| 643 | + FileTraceabilityIndex.create_source_node_section( |
| 644 | + document, |
| 645 | + path_to_source_file_, |
| 646 | + section_cache, |
| 647 | + ) |
| 648 | + ) |
| 649 | + current_top_node.section_contents.append(sdoc_node) |
685 | 650 |
|
686 | 651 | self.connect_source_node_function( |
687 | 652 | source_node_, sdoc_node_uid, traceability_info_ |
@@ -1053,6 +1018,50 @@ def set_sdoc_node_fields( |
1053 | 1018 | value=field_value, |
1054 | 1019 | ) |
1055 | 1020 |
|
| 1021 | + @staticmethod |
| 1022 | + def create_source_node_section( |
| 1023 | + document: SDocDocumentIF, |
| 1024 | + path_to_source_file: str, |
| 1025 | + section_cache: Dict[str, Union[SDocDocumentIF, SDocNode]], |
| 1026 | + ) -> Union[SDocDocumentIF, SDocNode]: |
| 1027 | + """ |
| 1028 | + Add a subsection for each path components in a given file path. |
| 1029 | + """ |
| 1030 | + current_top_node: Union[SDocDocumentIF, SDocNode] = document |
| 1031 | + path_components = path_to_source_file.split("/") |
| 1032 | + for path_component_idx_, path_component_ in enumerate(path_components): |
| 1033 | + if path_component_ not in section_cache: |
| 1034 | + path_component_title = ( |
| 1035 | + path_component_ + "/" |
| 1036 | + if path_component_idx_ < (len(path_components) - 1) |
| 1037 | + else path_component_ |
| 1038 | + ) |
| 1039 | + current_section = SDocNode( |
| 1040 | + parent=current_top_node, |
| 1041 | + node_type="SECTION", |
| 1042 | + fields=[], |
| 1043 | + relations=[], |
| 1044 | + is_composite=True, |
| 1045 | + node_type_close="SECTION", |
| 1046 | + # It is important that this autogenerated node is marked as such. |
| 1047 | + autogen=True, |
| 1048 | + ) |
| 1049 | + current_section.ng_document_reference = DocumentReference() |
| 1050 | + current_section.ng_document_reference.set_document(document) |
| 1051 | + current_section.ng_including_document_reference = ( |
| 1052 | + DocumentReference() |
| 1053 | + ) |
| 1054 | + current_section.set_field_value( |
| 1055 | + field_name="TITLE", |
| 1056 | + form_field_index=0, |
| 1057 | + value=path_component_title, |
| 1058 | + ) |
| 1059 | + |
| 1060 | + current_top_node.section_contents.append(current_section) |
| 1061 | + section_cache[path_component_] = current_section |
| 1062 | + current_top_node = section_cache[path_component_] |
| 1063 | + return current_top_node |
| 1064 | + |
1056 | 1065 | def connect_sdoc_node_with_file_path( |
1057 | 1066 | self, sdoc_node: SDocNode, path_to_source_file_: str |
1058 | 1067 | ) -> None: |
|
0 commit comments