# HG changeset patch # User Dirk Olmes # Date 1272332528 -7200 # Node ID 2da2b691345d9b18762396a21602c1364565a12c # Parent 215c34f61e95e07bc3cd980db713066836028087 unfinished pretty printer for feed's plain text representation diff -r 215c34f61e95 -r 2da2b691345d prettyprint.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prettyprint.py Tue Apr 27 03:42:08 2010 +0200 @@ -0,0 +1,50 @@ + +import sys + +newlineAfter = [ '{', '[', ',' ] +newlineBefore = [ '}', ']' ] + +def prettyPrint(file): + indent = 0 + doIndent = False + printCommand = None + for line in file: + for char in line: + if char in newlineAfter: + if char is not ',': + indent = indent + 1 + doIndent = True + printCommand = p_nl + elif char in newlineBefore: + indent = indent - 1 + doIndent = True + printCommand = nl_p + else: + printCommand = p + doIndent = False + + printCommand(char) + if doIndent: + for x in range(indent): #@UnusedVariable + sys.stdout.write(" ") + doIndent = False + +def p(char): + sys.stdout.write(char) + +def p_nl(char): + sys.stdout.write(char) + sys.stdout.write("\n") + +def nl_p(char): + sys.stdout.write("\n") + sys.stdout.write(char) + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("usage: %s " % (sys.argv[0])) + sys.exit(1) + + filename = sys.argv[1] + with open(filename) as file: + prettyPrint(file) \ No newline at end of file