# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""Try to standalize indentation"""
import sys
import re
from collections import defaultdict
INDENT = 4 # what we should set it to
_whitespace_re = re.compile(r'\s*')
_blank_re = re.compile(r'^\s+$')
class RedentError(Exception):
pass
def main():
data = sys.stdin.read()
if u'\t' in data:
raise RedentError(u'omg tabs detected, fix that before running this')
lines = data.splitlines()
changes = defaultdict(int)
last_indent = None
parsed = []
for line in lines:
line = _blank_re.sub(u'', line)
indent = len(_whitespace_re.match(line).group(0))
tail = line[indent:]
parsed.append((indent, tail))
if last_indent is not None:
change = last_indent - indent
if change < 0:
change *= -1
if change:
changes[change] += 1
last_indent = indent
changes = sorted(changes.iteritems(), key=lambda item: item[1],
reverse=True)
detected_indent = changes[0][0]
if detected_indent == INDENT:
raise RedentError(u'no work needs to be done, indent level is same')
for i, line in enumerate(parsed):
indent, tail = line
if indent % detected_indent:
raise RedentError(u'uneven indentation detected')
level = indent / detected_indent
print (u' ' * INDENT * level) + tail
return 0
if __name__ == u'__main__':
sys.exit(main())
在付费自习室元年的年末,我去体验了一下
4 years ago
0 comments:
Post a Comment