comparison prettyprint.py @ 8:2da2b691345d

unfinished pretty printer for feed's plain text representation
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 27 Apr 2010 03:42:08 +0200
parents
children bb3c851b18b1
comparison
equal deleted inserted replaced
7:215c34f61e95 8:2da2b691345d
1
2 import sys
3
4 newlineAfter = [ '{', '[', ',' ]
5 newlineBefore = [ '}', ']' ]
6
7 def prettyPrint(file):
8 indent = 0
9 doIndent = False
10 printCommand = None
11 for line in file:
12 for char in line:
13 if char in newlineAfter:
14 if char is not ',':
15 indent = indent + 1
16 doIndent = True
17 printCommand = p_nl
18 elif char in newlineBefore:
19 indent = indent - 1
20 doIndent = True
21 printCommand = nl_p
22 else:
23 printCommand = p
24 doIndent = False
25
26 printCommand(char)
27 if doIndent:
28 for x in range(indent): #@UnusedVariable
29 sys.stdout.write(" ")
30 doIndent = False
31
32 def p(char):
33 sys.stdout.write(char)
34
35 def p_nl(char):
36 sys.stdout.write(char)
37 sys.stdout.write("\n")
38
39 def nl_p(char):
40 sys.stdout.write("\n")
41 sys.stdout.write(char)
42
43 if __name__ == "__main__":
44 if len(sys.argv) < 2:
45 print("usage: %s <file>" % (sys.argv[0]))
46 sys.exit(1)
47
48 filename = sys.argv[1]
49 with open(filename) as file:
50 prettyPrint(file)