Skip to content

Instantly share code, notes, and snippets.

@d33tah
Created July 30, 2014 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d33tah/e06d6195fc62decf538f to your computer and use it in GitHub Desktop.
Save d33tah/e06d6195fc62decf538f to your computer and use it in GitHub Desktop.
Converts Python indentation to C-like braces style (sometimes).
#!/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