| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
from __future__ import print_function |
| 80 |
|
| 81 |
|
| 82 |
STEPSIZE = 8 |
| 83 |
TABSIZE = 8 |
| 84 |
EXPANDTABS = False |
| 85 |
|
| 86 |
import io |
| 87 |
import re |
| 88 |
import sys |
| 89 |
|
| 90 |
next = {} |
| 91 |
next['if'] = next['elif'] = 'elif', 'else', 'end' |
| 92 |
next['while'] = next['for'] = 'else', 'end' |
| 93 |
next['try'] = 'except', 'finally' |
| 94 |
next['except'] = 'except', 'else', 'finally', 'end' |
| 95 |
next['else'] = next['finally'] = next['with'] = \ |
| 96 |
next['def'] = next['class'] = 'end' |
| 97 |
next['end'] = () |
| 98 |
start = 'if', 'while', 'for', 'try', 'with', 'def', 'class' |
| 99 |
|
| 100 |
class PythonIndenter: |
| 101 |
|
| 102 |
def __init__(self, fpi = sys.stdin, fpo = sys.stdout, |
| 103 |
indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 104 |
self.fpi = fpi |
| 105 |
self.fpo = fpo |
| 106 |
self.indentsize = indentsize |
| 107 |
self.tabsize = tabsize |
| 108 |
self.lineno = 0 |
| 109 |
self.expandtabs = expandtabs |
| 110 |
self._write = fpo.write |
| 111 |
self.kwprog = re.compile( |
| 112 |
r'^(?:\s|\\\n)*(?P<kw>[a-z]+)' |
| 113 |
r'((?:\s|\\\n)+(?P<id>[a-zA-Z_]\w*))?' |
| 114 |
r'[^\w]') |
| 115 |
self.endprog = re.compile( |
| 116 |
r'^(?:\s|\\\n)*#?\s*end\s+(?P<kw>[a-z]+)' |
| 117 |
r'(\s+(?P<id>[a-zA-Z_]\w*))?' |
| 118 |
r'[^\w]') |
| 119 |
self.wsprog = re.compile(r'^[ \t]*') |
| 120 |
|
| 121 |
|
| 122 |
def write(self, line): |
| 123 |
if self.expandtabs: |
| 124 |
self._write(line.expandtabs(self.tabsize)) |
| 125 |
else: |
| 126 |
self._write(line) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
def readline(self): |
| 131 |
line = self.fpi.readline() |
| 132 |
if line: self.lineno += 1 |
| 133 |
|
| 134 |
return line |
| 135 |
|
| 136 |
|
| 137 |
def error(self, fmt, *args): |
| 138 |
if args: fmt = fmt % args |
| 139 |
|
| 140 |
sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt)) |
| 141 |
self.write('### %s ###\n' % fmt) |
| 142 |
|
| 143 |
|
| 144 |
def getline(self): |
| 145 |
line = self.readline() |
| 146 |
while line[-2:] == '\\\n': |
| 147 |
line2 = self.readline() |
| 148 |
if not line2: break |
| 149 |
|
| 150 |
line += line2 |
| 151 |
|
| 152 |
return line |
| 153 |
|
| 154 |
|
| 155 |
def putline(self, line, indent): |
| 156 |
tabs, spaces = divmod(indent*self.indentsize, self.tabsize) |
| 157 |
i = self.wsprog.match(line).end() |
| 158 |
line = line[i:] |
| 159 |
if line[:1] not in ('\n', '\r', ''): |
| 160 |
line = '\t'*tabs + ' '*spaces + line |
| 161 |
|
| 162 |
self.write(line) |
| 163 |
|
| 164 |
|
| 165 |
def reformat(self): |
| 166 |
stack = [] |
| 167 |
while True: |
| 168 |
line = self.getline() |
| 169 |
if not line: break |
| 170 |
|
| 171 |
m = self.endprog.match(line) |
| 172 |
if m: |
| 173 |
kw = 'end' |
| 174 |
kw2 = m.group('kw') |
| 175 |
if not stack: |
| 176 |
self.error('unexpected end') |
| 177 |
elif stack.pop()[0] != kw2: |
| 178 |
self.error('unmatched end') |
| 179 |
|
| 180 |
self.putline(line, len(stack)) |
| 181 |
continue |
| 182 |
|
| 183 |
m = self.kwprog.match(line) |
| 184 |
if m: |
| 185 |
kw = m.group('kw') |
| 186 |
if kw in start: |
| 187 |
self.putline(line, len(stack)) |
| 188 |
stack.append((kw, kw)) |
| 189 |
continue |
| 190 |
|
| 191 |
if next.has_key(kw) and stack: |
| 192 |
self.putline(line, len(stack)-1) |
| 193 |
kwa, kwb = stack[-1] |
| 194 |
stack[-1] = kwa, kw |
| 195 |
continue |
| 196 |
|
| 197 |
|
| 198 |
self.putline(line, len(stack)) |
| 199 |
|
| 200 |
if stack: |
| 201 |
self.error('unterminated keywords') |
| 202 |
for kwa, kwb in stack: |
| 203 |
self.write('\t%s\n' % kwa) |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
def delete(self): |
| 209 |
begin_counter = 0 |
| 210 |
end_counter = 0 |
| 211 |
while True: |
| 212 |
line = self.getline() |
| 213 |
if not line: break |
| 214 |
|
| 215 |
m = self.endprog.match(line) |
| 216 |
if m: |
| 217 |
end_counter += 1 |
| 218 |
continue |
| 219 |
|
| 220 |
m = self.kwprog.match(line) |
| 221 |
if m: |
| 222 |
kw = m.group('kw') |
| 223 |
if kw in start: |
| 224 |
begin_counter += 1 |
| 225 |
|
| 226 |
|
| 227 |
self.write(line) |
| 228 |
|
| 229 |
if begin_counter - end_counter < 0: |
| 230 |
sys.stderr.write('Warning: input contained more end tags than expected\n') |
| 231 |
elif begin_counter - end_counter > 0: |
| 232 |
sys.stderr.write('Warning: input contained less end tags than expected\n') |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
def complete(self): |
| 237 |
stack = [] |
| 238 |
todo = [] |
| 239 |
currentws = thisid = firstkw = lastkw = topid = '' |
| 240 |
while True: |
| 241 |
line = self.getline() |
| 242 |
i = self.wsprog.match(line).end() |
| 243 |
m = self.endprog.match(line) |
| 244 |
if m: |
| 245 |
thiskw = 'end' |
| 246 |
endkw = m.group('kw') |
| 247 |
thisid = m.group('id') |
| 248 |
else: |
| 249 |
m = self.kwprog.match(line) |
| 250 |
if m: |
| 251 |
thiskw = m.group('kw') |
| 252 |
if not next.has_key(thiskw): |
| 253 |
thiskw = '' |
| 254 |
|
| 255 |
if thiskw in ('def', 'class'): |
| 256 |
thisid = m.group('id') |
| 257 |
else: |
| 258 |
thisid = '' |
| 259 |
|
| 260 |
elif line[i:i+1] in ('\n', '#'): |
| 261 |
todo.append(line) |
| 262 |
continue |
| 263 |
else: |
| 264 |
thiskw = '' |
| 265 |
|
| 266 |
|
| 267 |
indentws = line[:i] |
| 268 |
indent = len(indentws.expandtabs(self.tabsize)) |
| 269 |
current = len(currentws.expandtabs(self.tabsize)) |
| 270 |
while indent < current: |
| 271 |
if firstkw: |
| 272 |
if topid: |
| 273 |
s = '# end %s %s\n' % ( |
| 274 |
firstkw, topid) |
| 275 |
else: |
| 276 |
s = '# end %s\n' % firstkw |
| 277 |
|
| 278 |
self.write(currentws + s) |
| 279 |
firstkw = lastkw = '' |
| 280 |
|
| 281 |
currentws, firstkw, lastkw, topid = stack.pop() |
| 282 |
current = len(currentws.expandtabs(self.tabsize)) |
| 283 |
|
| 284 |
if indent == current and firstkw: |
| 285 |
if thiskw == 'end': |
| 286 |
if endkw != firstkw: |
| 287 |
self.error('mismatched end') |
| 288 |
|
| 289 |
firstkw = lastkw = '' |
| 290 |
elif not thiskw or thiskw in start: |
| 291 |
if topid: |
| 292 |
s = '# end %s %s\n' % ( |
| 293 |
firstkw, topid) |
| 294 |
else: |
| 295 |
s = '# end %s\n' % firstkw |
| 296 |
|
| 297 |
self.write(currentws + s) |
| 298 |
firstkw = lastkw = topid = '' |
| 299 |
|
| 300 |
|
| 301 |
if indent > current: |
| 302 |
stack.append((currentws, firstkw, lastkw, topid)) |
| 303 |
if thiskw and thiskw not in start: |
| 304 |
|
| 305 |
thiskw = '' |
| 306 |
|
| 307 |
currentws, firstkw, lastkw, topid = \ |
| 308 |
indentws, thiskw, thiskw, thisid |
| 309 |
|
| 310 |
if thiskw: |
| 311 |
if thiskw in start: |
| 312 |
firstkw = lastkw = thiskw |
| 313 |
topid = thisid |
| 314 |
else: |
| 315 |
lastkw = thiskw |
| 316 |
|
| 317 |
|
| 318 |
for l in todo: self.write(l) |
| 319 |
|
| 320 |
todo = [] |
| 321 |
if not line: break |
| 322 |
|
| 323 |
self.write(line) |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
def complete_filter(input = sys.stdin, output = sys.stdout, |
| 334 |
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 335 |
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 336 |
pi.complete() |
| 337 |
|
| 338 |
|
| 339 |
def delete_filter(input= sys.stdin, output = sys.stdout, |
| 340 |
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 341 |
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 342 |
pi.delete() |
| 343 |
|
| 344 |
|
| 345 |
def reformat_filter(input = sys.stdin, output = sys.stdout, |
| 346 |
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 347 |
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 348 |
pi.reformat() |
| 349 |
|
| 350 |
|
| 351 |
def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 352 |
input = io.BytesIO(source) |
| 353 |
output = io.BytesIO() |
| 354 |
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 355 |
pi.complete() |
| 356 |
return output.getvalue() |
| 357 |
|
| 358 |
|
| 359 |
def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 360 |
input = io.BytesIO(source) |
| 361 |
output = io.BytesIO() |
| 362 |
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 363 |
pi.delete() |
| 364 |
return output.getvalue() |
| 365 |
|
| 366 |
|
| 367 |
def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 368 |
input = io.BytesIO(source) |
| 369 |
output = io.BytesIO() |
| 370 |
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) |
| 371 |
pi.reformat() |
| 372 |
return output.getvalue() |
| 373 |
|
| 374 |
|
| 375 |
def make_backup(filename): |
| 376 |
import os, os.path |
| 377 |
backup = filename + '~' |
| 378 |
if os.path.lexists(backup): |
| 379 |
try: |
| 380 |
os.remove(backup) |
| 381 |
except os.error: |
| 382 |
print("Can't remove backup %r" % (backup,), file=sys.stderr) |
| 383 |
|
| 384 |
|
| 385 |
try: |
| 386 |
os.rename(filename, backup) |
| 387 |
except os.error: |
| 388 |
print("Can't rename %r to %r" % (filename, backup), file=sys.stderr) |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 393 |
with open(filename, 'r') as f: |
| 394 |
source = f.read() |
| 395 |
|
| 396 |
result = complete_string(source, stepsize, tabsize, expandtabs) |
| 397 |
if source == result: return 0 |
| 398 |
|
| 399 |
make_backup(filename) |
| 400 |
with open(filename, 'w') as f: |
| 401 |
f.write(result) |
| 402 |
|
| 403 |
return 1 |
| 404 |
|
| 405 |
|
| 406 |
def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 407 |
with open(filename, 'r') as f: |
| 408 |
source = f.read() |
| 409 |
|
| 410 |
result = delete_string(source, stepsize, tabsize, expandtabs) |
| 411 |
if source == result: return 0 |
| 412 |
|
| 413 |
make_backup(filename) |
| 414 |
with open(filename, 'w') as f: |
| 415 |
f.write(result) |
| 416 |
|
| 417 |
return 1 |
| 418 |
|
| 419 |
|
| 420 |
def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): |
| 421 |
with open(filename, 'r') as f: |
| 422 |
source = f.read() |
| 423 |
|
| 424 |
result = reformat_string(source, stepsize, tabsize, expandtabs) |
| 425 |
if source == result: return 0 |
| 426 |
|
| 427 |
make_backup(filename) |
| 428 |
with open(filename, 'w') as f: |
| 429 |
f.write(result) |
| 430 |
|
| 431 |
return 1 |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
usage = """ |
| 437 |
usage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ... |
| 438 |
-c : complete a correctly indented program (add #end directives) |
| 439 |
-d : delete #end directives |
| 440 |
-r : reformat a completed program (use #end directives) |
| 441 |
-s stepsize: indentation step (default %(STEPSIZE)d) |
| 442 |
-t tabsize : the worth in spaces of a tab (default %(TABSIZE)d) |
| 443 |
-e : expand TABs into spaces (default OFF) |
| 444 |
[file] ... : files are changed in place, with backups in file~ |
| 445 |
If no files are specified or a single - is given, |
| 446 |
the program acts as a filter (reads stdin, writes stdout). |
| 447 |
""" % vars() |
| 448 |
|
| 449 |
def error_both(op1, op2): |
| 450 |
sys.stderr.write('Error: You can not specify both '+op1+' and -'+op2[0]+' at the same time\n') |
| 451 |
sys.stderr.write(usage) |
| 452 |
sys.exit(2) |
| 453 |
|
| 454 |
|
| 455 |
def test(): |
| 456 |
import getopt |
| 457 |
try: |
| 458 |
opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e') |
| 459 |
except getopt.error, msg: |
| 460 |
sys.stderr.write('Error: %s\n' % msg) |
| 461 |
sys.stderr.write(usage) |
| 462 |
sys.exit(2) |
| 463 |
|
| 464 |
action = None |
| 465 |
stepsize = STEPSIZE |
| 466 |
tabsize = TABSIZE |
| 467 |
expandtabs = EXPANDTABS |
| 468 |
for o, a in opts: |
| 469 |
if o == '-c': |
| 470 |
if action: error_both(o, action) |
| 471 |
|
| 472 |
action = 'complete' |
| 473 |
elif o == '-d': |
| 474 |
if action: error_both(o, action) |
| 475 |
|
| 476 |
action = 'delete' |
| 477 |
elif o == '-r': |
| 478 |
if action: error_both(o, action) |
| 479 |
|
| 480 |
action = 'reformat' |
| 481 |
elif o == '-s': |
| 482 |
stepsize = int(a) |
| 483 |
elif o == '-t': |
| 484 |
tabsize = int(a) |
| 485 |
elif o == '-e': |
| 486 |
expandtabs = True |
| 487 |
|
| 488 |
|
| 489 |
if not action: |
| 490 |
sys.stderr.write( |
| 491 |
'You must specify -c(omplete), -d(elete) or -r(eformat)\n') |
| 492 |
sys.stderr.write(usage) |
| 493 |
sys.exit(2) |
| 494 |
|
| 495 |
if not args or args == ['-']: |
| 496 |
action = eval(action + '_filter') |
| 497 |
action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs) |
| 498 |
else: |
| 499 |
action = eval(action + '_file') |
| 500 |
for filename in args: |
| 501 |
action(filename, stepsize, tabsize, expandtabs) |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
if __name__ == '__main__': |
| 507 |
test() |
| 508 |
|