Return the relative path from this node to the given ancestor node
(self, ancestor: Self)
| 779 | ) |
| 780 | |
| 781 | def _path_to_ancestor(self, ancestor: Self) -> NodePath: |
| 782 | """Return the relative path from this node to the given ancestor node""" |
| 783 | |
| 784 | if not self.same_tree(ancestor): |
| 785 | raise NotFoundInTreeError( |
| 786 | "Cannot find relative path to ancestor because nodes do not lie within the same tree" |
| 787 | ) |
| 788 | if ancestor.path not in [a.path for a in (self, *self.parents)]: |
| 789 | raise NotFoundInTreeError( |
| 790 | "Cannot find relative path to ancestor because given node is not an ancestor of this node" |
| 791 | ) |
| 792 | |
| 793 | parents_paths = [parent.path for parent in (self, *self.parents)] |
| 794 | generation_gap = list(parents_paths).index(ancestor.path) |
| 795 | path_upwards = "../" * generation_gap if generation_gap > 0 else "." |
| 796 | return NodePath(path_upwards) |
| 797 | |
| 798 | |
| 799 | class TreeIsomorphismError(ValueError): |
no test coverage detected