view prettyprint.py @ 246:7c719c4f5655

Fix all remaining code style bugs
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 09 Jun 2015 02:45:29 +0200
parents e34c53a3e407
children
line wrap: on
line source

# -*- coding: utf-8 -*-
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):
                    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)