paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@104 | 3 | from os.path import abspath, split |
paul@44 | 4 | import sys |
paul@44 | 5 | |
paul@104 | 6 | # Locate and import the moinformat package. |
paul@104 | 7 | |
paul@44 | 8 | dirname = split(abspath(sys.argv[0]))[0] |
paul@44 | 9 | parent = split(dirname)[0] |
paul@44 | 10 | |
paul@44 | 11 | try: |
paul@44 | 12 | import moinformat |
paul@44 | 13 | except ImportError: |
paul@44 | 14 | if split(parent)[1] == "MoinLight": |
paul@44 | 15 | sys.path.append(parent) |
paul@44 | 16 | |
paul@104 | 17 | # Import specific objects. |
paul@104 | 18 | |
paul@104 | 19 | from moinformat import make_input, make_output, make_serialiser, parse, serialise |
paul@83 | 20 | from moinformat.tree.moin import Container |
paul@16 | 21 | |
paul@65 | 22 | def test_input(d, s): |
paul@65 | 23 | |
paul@65 | 24 | "Compare serialised output from 'd' with its original form 's'." |
paul@65 | 25 | |
paul@104 | 26 | output = make_output("standalone") |
paul@104 | 27 | expected = output.encode(s) |
paul@12 | 28 | |
paul@104 | 29 | result = serialise(d, make_serialiser("moin", output)) |
paul@104 | 30 | identical = result == expected |
paul@38 | 31 | |
paul@38 | 32 | if quiet: |
paul@60 | 33 | return identical |
paul@38 | 34 | |
paul@60 | 35 | # Show output versus input comparison result. |
paul@60 | 36 | |
paul@60 | 37 | print identical |
paul@26 | 38 | print "-" * 60 |
paul@104 | 39 | print result |
paul@65 | 40 | if not identical: |
paul@26 | 41 | print "-" * 60 |
paul@104 | 42 | print expected |
paul@26 | 43 | print "-" * 60 |
paul@120 | 44 | |
paul@120 | 45 | # Show HTML serialisation. |
paul@120 | 46 | |
paul@120 | 47 | output = make_output("standalone") |
paul@104 | 48 | print serialise(d, make_serialiser("html", output)) |
paul@26 | 49 | print "-" * 60 |
paul@26 | 50 | print |
paul@20 | 51 | |
paul@60 | 52 | return identical |
paul@60 | 53 | |
paul@65 | 54 | def test_tree(d, t, ts): |
paul@65 | 55 | |
paul@65 | 56 | "Compare tree structure 'd' with simplified, expected form 't' from 'ts'." |
paul@65 | 57 | |
paul@65 | 58 | failing = t.test(d) |
paul@65 | 59 | |
paul@65 | 60 | if quiet: |
paul@65 | 61 | return not failing |
paul@65 | 62 | |
paul@65 | 63 | # Show tree versus expected forms. |
paul@65 | 64 | |
paul@65 | 65 | print not failing |
paul@65 | 66 | print "-" * 60 |
paul@65 | 67 | print d.prettyprint() |
paul@65 | 68 | if failing: |
paul@68 | 69 | print "-" * 60 |
paul@68 | 70 | print ts |
paul@68 | 71 | simple, tree, error = failing |
paul@68 | 72 | print "-" * 60 |
paul@68 | 73 | print error |
paul@68 | 74 | print repr(simple) |
paul@68 | 75 | print repr(tree) |
paul@65 | 76 | print "-" * 60 |
paul@65 | 77 | print tree.prettyprint() |
paul@65 | 78 | print "-" * 60 |
paul@65 | 79 | print simple.prettyprint() |
paul@65 | 80 | print "-" * 60 |
paul@65 | 81 | print |
paul@65 | 82 | |
paul@65 | 83 | return not failing |
paul@65 | 84 | |
paul@65 | 85 | class Node: |
paul@65 | 86 | |
paul@65 | 87 | "A simplified tree node representation." |
paul@65 | 88 | |
paul@65 | 89 | def __init__(self, name): |
paul@65 | 90 | self.name = name |
paul@65 | 91 | self.nodes = [] |
paul@65 | 92 | |
paul@65 | 93 | def __repr__(self): |
paul@68 | 94 | return "Node(%r, %r)" % (self.name, self.nodes) |
paul@65 | 95 | |
paul@65 | 96 | def prettyprint(self, indent=""): |
paul@68 | 97 | l = [] |
paul@68 | 98 | l.append("%s%s%s" % (indent, self.name, len(self.nodes) and " nodes=%d" % len(self.nodes) or "")) |
paul@65 | 99 | for node in self.nodes: |
paul@65 | 100 | l.append(node.prettyprint(indent + " ")) |
paul@65 | 101 | return "\n".join(l) |
paul@65 | 102 | |
paul@65 | 103 | def append(self, node): |
paul@65 | 104 | self.nodes.append(node) |
paul@65 | 105 | |
paul@65 | 106 | def test(self, other): |
paul@65 | 107 | |
paul@65 | 108 | """ |
paul@65 | 109 | Test whether this node is considered equivalent to 'other', where |
paul@65 | 110 | 'other' is a moinparser.tree node. |
paul@65 | 111 | |
paul@65 | 112 | Return any failing tree nodes or None. |
paul@65 | 113 | """ |
paul@65 | 114 | |
paul@65 | 115 | if other.__class__.__name__ != self.name: |
paul@68 | 116 | return self, other, "name" |
paul@65 | 117 | |
paul@65 | 118 | if isinstance(other, Container): |
paul@65 | 119 | for node, other_node in map(None, self.nodes, other.nodes): |
paul@65 | 120 | if node is None or other_node is None: |
paul@68 | 121 | return self, other, node is None and "simple" or "document" |
paul@68 | 122 | t = node.test(other_node) |
paul@68 | 123 | if t: |
paul@68 | 124 | return t |
paul@68 | 125 | elif self.nodes: |
paul@68 | 126 | return self, other, "empty" |
paul@65 | 127 | |
paul@65 | 128 | return None |
paul@65 | 129 | |
paul@65 | 130 | def parse_tree(s): |
paul@65 | 131 | |
paul@65 | 132 | "Parse the tree structure representation in 's'." |
paul@65 | 133 | |
paul@65 | 134 | indent = 0 |
paul@65 | 135 | branches = [] |
paul@65 | 136 | |
paul@65 | 137 | for line in s.split("\n"): |
paul@65 | 138 | line = line.rstrip() |
paul@65 | 139 | if not line: |
paul@65 | 140 | continue |
paul@65 | 141 | |
paul@65 | 142 | new_indent = line.rfind(" ") + 1 |
paul@65 | 143 | node = Node(line[new_indent:]) |
paul@65 | 144 | |
paul@65 | 145 | # Establish a branch to add nodes to. |
paul@65 | 146 | |
paul@65 | 147 | if not branches: |
paul@65 | 148 | branches.append(node) |
paul@65 | 149 | else: |
paul@65 | 150 | # Note the current node as outermost branch. |
paul@65 | 151 | |
paul@65 | 152 | if new_indent > indent: |
paul@65 | 153 | branches.append(node) |
paul@65 | 154 | else: |
paul@65 | 155 | # Reduced indent involves obtaining an inner branch again. |
paul@65 | 156 | |
paul@65 | 157 | while indent > new_indent: |
paul@65 | 158 | del branches[-1] |
paul@65 | 159 | indent -= 2 |
paul@65 | 160 | |
paul@65 | 161 | # Note the current node as outermost branch. |
paul@65 | 162 | |
paul@65 | 163 | branches[-1] = node |
paul@65 | 164 | |
paul@65 | 165 | # Append the current node to the parent branch. |
paul@65 | 166 | |
paul@65 | 167 | branches[-2].append(node) |
paul@65 | 168 | |
paul@65 | 169 | indent = new_indent |
paul@65 | 170 | |
paul@65 | 171 | return branches[0] |
paul@65 | 172 | |
paul@26 | 173 | if __name__ == "__main__": |
paul@38 | 174 | args = sys.argv[1:] |
paul@65 | 175 | |
paul@38 | 176 | quiet = "-q" in args |
paul@38 | 177 | if quiet: |
paul@38 | 178 | del args[args.index("-q")] |
paul@65 | 179 | |
paul@104 | 180 | # Make an input context. |
paul@104 | 181 | |
paul@104 | 182 | input = make_input("directory", {"filename" : dirname}) |
paul@104 | 183 | |
paul@104 | 184 | # Obtain input filenames. |
paul@104 | 185 | |
paul@104 | 186 | filenames = args or input.select_files("test*.txt*") |
paul@26 | 187 | filenames.sort() |
paul@0 | 188 | |
paul@104 | 189 | # Process each filename, obtaining a corresponding tree definition. |
paul@104 | 190 | |
paul@26 | 191 | for filename in filenames: |
paul@104 | 192 | |
paul@104 | 193 | # Test for an explicit encoding suffix. |
paul@65 | 194 | |
paul@104 | 195 | t = filename.split(".") |
paul@104 | 196 | if len(t) > 2: |
paul@104 | 197 | text_filename = ".".join(t[:2]) |
paul@104 | 198 | encoding = t[2] |
paul@104 | 199 | else: |
paul@104 | 200 | text_filename = filename |
paul@104 | 201 | encoding = None |
paul@104 | 202 | |
paul@104 | 203 | tree_filename = "%s.tree" % text_filename.rsplit(".", 1)[0] |
paul@104 | 204 | |
paul@104 | 205 | # Read and parse the input. |
paul@104 | 206 | |
paul@104 | 207 | s = input.readfile(text_filename, encoding) |
paul@88 | 208 | d = parse(s) |
paul@65 | 209 | |
paul@104 | 210 | # Read and parse any tree definition. |
paul@104 | 211 | |
paul@104 | 212 | if input.exists(tree_filename): |
paul@104 | 213 | ts = input.readfile(tree_filename) |
paul@65 | 214 | t = parse_tree(ts) |
paul@65 | 215 | else: |
paul@65 | 216 | ts = None |
paul@65 | 217 | |
paul@104 | 218 | # Report the test results. |
paul@104 | 219 | |
paul@65 | 220 | if not quiet: |
paul@65 | 221 | print filename |
paul@65 | 222 | |
paul@65 | 223 | identical = test_input(d, s) |
paul@65 | 224 | tree_identical = ts and test_tree(d, t, ts) |
paul@65 | 225 | |
paul@65 | 226 | if quiet: |
paul@65 | 227 | print "%s %s: %s" % (identical, tree_identical, filename) |
paul@3 | 228 | |
paul@0 | 229 | # vim: tabstop=4 expandtab shiftwidth=4 |