Skip to content

Instantly share code, notes, and snippets.

@oddstr13
Created October 3, 2019 21:35
Show Gist options
  • Save oddstr13/9265590f1591f2ef750e4d2bedd35eb1 to your computer and use it in GitHub Desktop.
Save oddstr13/9265590f1591f2ef750e4d2bedd35eb1 to your computer and use it in GitHub Desktop.
from __future__ import print_function, unicode_literals
import sys
WHITESPACE = [None, ' ', '\n', '\t']
UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def processStatement(st):
res = []
prev = None
for c in st:
if c in WHITESPACE and prev in WHITESPACE:
continue
res.append(c)
prev = c
st = ''.join(res).strip()
res = []
indent = 0
lines = st.splitlines()
for line in lines:
for i, c in enumerate(line):
if c not in WHITESPACE and c not in UPPERCASE:
indent = max(i-1, indent)
break
extra_indent = 4 - (indent % 4)
if extra_indent < 3: # Minimum spaces between statement and arguments
extra_indent += 4
indent = indent + extra_indent
for line in lines:
split = 0
for i, c in enumerate(line):
if c not in WHITESPACE and c not in UPPERCASE:
split = i
break
statement = line[:split].strip()
arguments = line[split:].strip()
indentchars = ' ' * (indent - len(statement))
res.append(statement)
res.append(indentchars)
res.append(arguments)
res.append('\n')
return ''.join(res).strip()
def main(fn):
with open(fn, "r") as fh:
data = fh.read()
res = []
pos = 0
while True:
new = data.find('"""', pos)
if new == -1:
res.append(data[pos:])
break
res.append(data[pos:new])
res.append('"""\n')
pos = new + 3
end = data.find('"""', pos)
st = data[pos:end]
res.append(processStatement(st))
res.append('\n"""')
pos = end + 3
outdata = ''.join(res)
with open(fn, "w") as fh:
fh.write(outdata)
if __name__ == "__main__":
for fn in sys.argv[1:]:
main(fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment