|
| 1 | +from typing import Any, Iterable, Iterator, List, Optional, Tuple, Union |
| 2 | + |
| 3 | +from .polyfile import Match, Submatch |
| 4 | + |
| 5 | + |
| 6 | +class Node: |
| 7 | + def __init__(self, |
| 8 | + name: str, |
| 9 | + value: Optional[bytes] = None, |
| 10 | + offset: Optional[int] = None, |
| 11 | + length: Optional[int] = None, |
| 12 | + older_sibling: Optional["Node"] = None, |
| 13 | + children: Iterable["Node"] = () |
| 14 | + ): |
| 15 | + self.name: str = name |
| 16 | + self.value: Optional[bytes] = value |
| 17 | + self._offset: Optional[int] = offset |
| 18 | + self._length: Optional[int] = length |
| 19 | + self.older_sibling: Optional[Node] = older_sibling |
| 20 | + self.children: Tuple[Node, ...] = tuple(children) |
| 21 | + |
| 22 | + def __repr__(self): |
| 23 | + r = f"{self.__class__.__name__}(name={self.name!r}" |
| 24 | + if self.value is not None: |
| 25 | + r = f"{r}, value={self.value!r}" |
| 26 | + r = f"{r}, offset={self.offset!r}, length={self.length!r}, older_sibling={self.older_sibling!r}, " \ |
| 27 | + f"children={self.children!r})" |
| 28 | + return r |
| 29 | + |
| 30 | + @property |
| 31 | + def offset(self) -> int: |
| 32 | + if self._offset is None: |
| 33 | + if self.children: |
| 34 | + self._offset = self.children[0].offset |
| 35 | + elif self.older_sibling is not None: |
| 36 | + self._offset = self.older_sibling.offset + self.older_sibling.length |
| 37 | + else: |
| 38 | + raise ValueError(f"{self!r} must either have an explicit offset, an older sibling, or a child!") |
| 39 | + return self._offset |
| 40 | + |
| 41 | + @property |
| 42 | + def length(self) -> int: |
| 43 | + if self._length is None: |
| 44 | + if self.value is not None: |
| 45 | + self._length = len(self.value) |
| 46 | + elif not self.children: |
| 47 | + self._length = 0 |
| 48 | + else: |
| 49 | + self._length = self.children[-1].offset + self.children[-1].length - self.offset |
| 50 | + return self._length |
| 51 | + |
| 52 | + def to_matches(self, parent: Optional[Match] = None) -> Iterator[Submatch]: |
| 53 | + stack: List[Tuple["Node", Optional[Match]]] = [(self, parent)] |
| 54 | + while stack: |
| 55 | + node, parent = stack.pop() |
| 56 | + if parent is None: |
| 57 | + parent_offset = 0 |
| 58 | + else: |
| 59 | + parent_offset = parent.offset |
| 60 | + match = Submatch( |
| 61 | + name=node.name, |
| 62 | + match_obj=node.value, |
| 63 | + relative_offset=node.offset - parent_offset, |
| 64 | + length=node.length, |
| 65 | + parent=parent |
| 66 | + ) |
| 67 | + yield match |
| 68 | + for child in reversed(node.children): |
| 69 | + stack.append((child, match)) |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def load(cls, obj: Any) -> "Node": |
| 73 | + ancestors: List[Tuple[Any, Optional[Tuple[Node, ...]]]] = [ |
| 74 | + (obj, None) |
| 75 | + ] |
| 76 | + children: Optional[Tuple[Node, ...]] |
| 77 | + while True: |
| 78 | + obj, children = ancestors.pop() |
| 79 | + if children is None: |
| 80 | + if hasattr(obj, "children") and obj.children: |
| 81 | + ancestors.append((obj, ())) |
| 82 | + ancestors.extend((child, None) for child in reversed(obj.children)) |
| 83 | + continue |
| 84 | + children = () |
| 85 | + if not hasattr(obj, "name"): |
| 86 | + raise ValueError(f"{obj!r} does not have a `name` attribute!") |
| 87 | + name: str = obj.name |
| 88 | + if hasattr(obj, "value"): |
| 89 | + value: Union[Optional[bytes], str] = obj.value |
| 90 | + else: |
| 91 | + value = None |
| 92 | + if isinstance(value, str): |
| 93 | + value = value.encode("utf-8") |
| 94 | + if hasattr(obj, "offset"): |
| 95 | + offset: Optional[int] = obj.offset |
| 96 | + else: |
| 97 | + offset = None |
| 98 | + if hasattr(obj, "length") and (value is None or obj.length >= len(value)): |
| 99 | + length: Optional[int] = obj.length |
| 100 | + else: |
| 101 | + length = None |
| 102 | + older_sibling: Optional[Node] = None |
| 103 | + for reversed_parent_index, (parent, siblings) in enumerate(reversed(ancestors)): |
| 104 | + if siblings is not None: |
| 105 | + if siblings: |
| 106 | + older_sibling: Optional[Node] = siblings[-1] |
| 107 | + break |
| 108 | + else: |
| 109 | + assert not ancestors |
| 110 | + node = cls(name=name, value=value, offset=offset, length=length, older_sibling=older_sibling, |
| 111 | + children=children) |
| 112 | + if not ancestors: |
| 113 | + return node |
| 114 | + ancestors[len(ancestors) - reversed_parent_index - 1] = parent, siblings + (node,) |
0 commit comments