view prettyprint.py @ 47:a8442c3487b5

add an option to Feed that allows loading an entry's link right away instead of displaying a feed's summary
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 16 May 2010 08:13:07 +0200
parents 2da2b691345d
children bb3c851b18b1
line wrap: on
line source


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 <file>" % (sys.argv[0]))
        sys.exit(1)
    
    filename = sys.argv[1]
    with open(filename) as file:
        prettyPrint(file)