Represents a path to a node in a hierarchical workflow. A node path is a sequence of segments, each identifying a node instance, typically in the form 'node_name@run_id' or just 'node_name'.
| 18 | |
| 19 | |
| 20 | class _NodePathBuilder: |
| 21 | """Represents a path to a node in a hierarchical workflow. |
| 22 | |
| 23 | A node path is a sequence of segments, each identifying a node instance, |
| 24 | typically in the form 'node_name@run_id' or just 'node_name'. |
| 25 | """ |
| 26 | |
| 27 | def __init__(self, segments: list[str]): |
| 28 | """Initializes a _NodePathBuilder with a list of segments.""" |
| 29 | self._segments = segments |
| 30 | |
| 31 | @classmethod |
| 32 | def from_string(cls, path_str: str) -> _NodePathBuilder: |
| 33 | """Parses a _NodePathBuilder from a string representation. |
| 34 | |
| 35 | Example: 'wf@1/node@2'. |
| 36 | """ |
| 37 | if not path_str: |
| 38 | return cls([]) |
| 39 | return cls(path_str.split('/')) |
| 40 | |
| 41 | def __str__(self) -> str: |
| 42 | """Returns the string representation of the path.""" |
| 43 | return '/'.join(self._segments) |
| 44 | |
| 45 | def __eq__(self, other: object) -> bool: |
| 46 | """Returns True if segments are equal.""" |
| 47 | if not isinstance(other, _NodePathBuilder): |
| 48 | return NotImplemented |
| 49 | return self._segments == other._segments |
| 50 | |
| 51 | @property |
| 52 | def node_name(self) -> str: |
| 53 | """Returns the node name of the leaf segment.""" |
| 54 | if not self._segments: |
| 55 | return '' |
| 56 | return self._segments[-1].rsplit('@', 1)[0] |
| 57 | |
| 58 | @property |
| 59 | def leaf_segment(self) -> str: |
| 60 | """Returns the full leaf segment.""" |
| 61 | if not self._segments: |
| 62 | return '' |
| 63 | return self._segments[-1] |
| 64 | |
| 65 | @property |
| 66 | def run_id(self) -> str | None: |
| 67 | """Returns the run ID of the leaf segment, if any.""" |
| 68 | if not self._segments: |
| 69 | return None |
| 70 | parts = self._segments[-1].rsplit('@', 1) |
| 71 | return parts[1] if len(parts) > 1 else None |
| 72 | |
| 73 | @property |
| 74 | def parent(self) -> _NodePathBuilder | None: |
| 75 | """Returns the parent _NodePathBuilder, or None if this is a root path.""" |
| 76 | if len(self._segments) <= 1: |
| 77 | return None |
no outgoing calls