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