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