Returns the target node given a path string. Args: path (str): The path to the node, with node names connected by " -> ". missing_node_handling (str): How to handle missing nodes. Options are "abort", "create", and "raise error". Returns:
(
self,
path: str,
missing_node_handling="abort",
root: Optional[KnowledgeNode] = None,
)
| 636 | return "\n".join(to_return) |
| 637 | |
| 638 | def find_node_by_path( |
| 639 | self, |
| 640 | path: str, |
| 641 | missing_node_handling="abort", |
| 642 | root: Optional[KnowledgeNode] = None, |
| 643 | ): |
| 644 | """ |
| 645 | Returns the target node given a path string. |
| 646 | |
| 647 | Args: |
| 648 | path (str): The path to the node, with node names connected by " -> ". |
| 649 | missing_node_handling (str): How to handle missing nodes. Options are "abort", "create", and "raise error". |
| 650 | |
| 651 | Returns: |
| 652 | KnowledgeNode: The target node. |
| 653 | """ |
| 654 | node_names = path.split(" -> ") |
| 655 | current_node = self.root if root is None else root |
| 656 | |
| 657 | for name in node_names[1:]: |
| 658 | found_node = next( |
| 659 | (child for child in current_node.children if child.name == name), None |
| 660 | ) |
| 661 | if found_node is None: |
| 662 | if missing_node_handling == "abort": |
| 663 | return |
| 664 | elif missing_node_handling == "create": |
| 665 | new_node = current_node.add_child(child_node_name=name) |
| 666 | current_node = new_node |
| 667 | elif missing_node_handling == "raise error": |
| 668 | structure = self.get_node_hierarchy_string( |
| 669 | include_indent=True, |
| 670 | include_full_path=False, |
| 671 | include_hash_tag=True, |
| 672 | ) |
| 673 | raise Exception( |
| 674 | f"Insert information error. Unable to find node {{{name}}} under {{{current_node.name}}}\n{structure}" |
| 675 | ) |
| 676 | else: |
| 677 | current_node = found_node |
| 678 | return current_node |
| 679 | |
| 680 | def insert_information( |
| 681 | self, |
no test coverage detected