Created
July 30, 2014 14:27
-
-
Save d33tah/e06d6195fc62decf538f to your computer and use it in GitHub Desktop.
Converts Python indentation to C-like braces style (sometimes).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Converts Python indentation to C-like braces style (sometimes). | |
# | |
# Usage: | |
# | |
# python indentify.py < somefile.py | |
# | |
# Author: | |
# | |
# Jacek "d33tah" Wielemborek | |
import re | |
import sys | |
def count_first_spaces(s): | |
ret = re.findall('^ +', s) | |
return len(ret[0]) if len(ret) > 0 else 0 | |
stack = [0] | |
for line in sys.stdin: | |
line = line.rstrip(":\r\n") | |
before = stack[-1] | |
now = count_first_spaces(line) | |
if now > before: | |
print("%s{\n%s" % (" " * before, line)) | |
stack += [now] | |
elif now < before: | |
stack.pop() | |
print("%s}\n%s" % (" " * stack[-1], line)) | |
#if len(stack) != 0: | |
# print("len(stack)=%d" % len(stack)) | |
else: | |
print(line) | |
# FIXME: ugly and not really tested. | |
if len(stack) > 0: | |
print("}" * (len(stack)-1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment