paul@333 | 1 | #!/usr/bin/env python |
paul@333 | 2 | |
paul@333 | 3 | """ |
paul@333 | 4 | Prettyprinted document tree nodes. |
paul@333 | 5 | |
paul@333 | 6 | Copyright (C) 2017, 2018, 2019, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@333 | 7 | |
paul@333 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@333 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@333 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@333 | 11 | version. |
paul@333 | 12 | |
paul@333 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@333 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@333 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@333 | 16 | details. |
paul@333 | 17 | |
paul@333 | 18 | You should have received a copy of the GNU General Public License along with |
paul@333 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@333 | 20 | """ |
paul@333 | 21 | |
paul@333 | 22 | from moinformat.tree.moin import Container |
paul@333 | 23 | |
paul@333 | 24 | class Node: |
paul@333 | 25 | |
paul@333 | 26 | "A simplified tree node representation." |
paul@333 | 27 | |
paul@333 | 28 | def __init__(self, name): |
paul@333 | 29 | self.name = name |
paul@333 | 30 | self.nodes = [] |
paul@333 | 31 | |
paul@333 | 32 | def __repr__(self): |
paul@333 | 33 | return "Node(%r, %r)" % (self.name, self.nodes) |
paul@333 | 34 | |
paul@333 | 35 | def visit(self, visitor): |
paul@333 | 36 | visitor.node(self) |
paul@333 | 37 | |
paul@333 | 38 | def append(self, node): |
paul@333 | 39 | self.nodes.append(node) |
paul@333 | 40 | |
paul@333 | 41 | def test(self, other): |
paul@333 | 42 | |
paul@333 | 43 | """ |
paul@333 | 44 | Test whether this node is considered equivalent to 'other', where |
paul@333 | 45 | 'other' is a moinparser.tree node. |
paul@333 | 46 | |
paul@333 | 47 | Return any failing tree nodes or None. |
paul@333 | 48 | """ |
paul@333 | 49 | |
paul@333 | 50 | if other.__class__.__name__ != self.name: |
paul@333 | 51 | return self, other, "name" |
paul@333 | 52 | |
paul@333 | 53 | if isinstance(other, Container): |
paul@333 | 54 | for node, other_node in map(None, self.nodes, other.nodes): |
paul@333 | 55 | if node is None or other_node is None: |
paul@333 | 56 | return self, other, node is None and "simple" or "document" |
paul@333 | 57 | t = node.test(other_node) |
paul@333 | 58 | if t: |
paul@333 | 59 | return t |
paul@333 | 60 | elif self.nodes: |
paul@333 | 61 | return self, other, "empty" |
paul@333 | 62 | |
paul@333 | 63 | return None |
paul@333 | 64 | |
paul@333 | 65 | # vim: tabstop=4 expandtab shiftwidth=4 |