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