Changeset 391 for python/trunk/Tools/scripts/pindent.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Tools/scripts/pindent.py
r2 r391 77 77 # - optionally do much more thorough reformatting, a la C indent 78 78 79 from __future__ import print_function 80 79 81 # Defaults 80 82 STEPSIZE = 8 81 83 TABSIZE = 8 82 EXPANDTABS = 0 83 84 EXPANDTABS = False 85 86 import io 84 87 import re 85 88 import sys … … 90 93 next['try'] = 'except', 'finally' 91 94 next['except'] = 'except', 'else', 'finally', 'end' 92 next['else'] = next['finally'] = next['def'] = next['class'] = 'end' 95 next['else'] = next['finally'] = next['with'] = \ 96 next['def'] = next['class'] = 'end' 93 97 next['end'] = () 94 98 start = 'if', 'while', 'for', 'try', 'with', 'def', 'class' … … 106 110 self._write = fpo.write 107 111 self.kwprog = re.compile( 108 r'^ \s*(?P<kw>[a-z]+)'109 r'( \s+(?P<id>[a-zA-Z_]\w*))?'112 r'^(?:\s|\\\n)*(?P<kw>[a-z]+)' 113 r'((?:\s|\\\n)+(?P<id>[a-zA-Z_]\w*))?' 110 114 r'[^\w]') 111 115 self.endprog = re.compile( 112 r'^ \s*#?\s*end\s+(?P<kw>[a-z]+)'116 r'^(?:\s|\\\n)*#?\s*end\s+(?P<kw>[a-z]+)' 113 117 r'(\s+(?P<id>[a-zA-Z_]\w*))?' 114 118 r'[^\w]') … … 126 130 def readline(self): 127 131 line = self.fpi.readline() 128 if line: self.lineno = self.lineno +1132 if line: self.lineno += 1 129 133 # end if 130 134 return line … … 144 148 if not line2: break 145 149 # end if 146 line = line +line2150 line += line2 147 151 # end while 148 152 return line 149 153 # end def getline 150 154 151 def putline(self, line, indent = None): 152 if indent is None: 153 self.write(line) 154 return 155 # end if 155 def putline(self, line, indent): 156 156 tabs, spaces = divmod(indent*self.indentsize, self.tabsize) 157 i = 0 158 m = self.wsprog.match(line) 159 if m: i = m.end() 160 # end if 161 self.write('\t'*tabs + ' '*spaces + line[i:]) 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 # end if 162 self.write(line) 162 163 # end def putline 163 164 164 165 def reformat(self): 165 166 stack = [] 166 while 1:167 while True: 167 168 line = self.getline() 168 169 if not line: break # EOF … … 174 175 if not stack: 175 176 self.error('unexpected end') 176 elif stack [-1][0] != kw2:177 elif stack.pop()[0] != kw2: 177 178 self.error('unmatched end') 178 179 # end if 179 del stack[-1:]180 180 self.putline(line, len(stack)) 181 181 continue … … 209 209 begin_counter = 0 210 210 end_counter = 0 211 while 1:211 while True: 212 212 line = self.getline() 213 213 if not line: break # EOF … … 215 215 m = self.endprog.match(line) 216 216 if m: 217 end_counter = end_counter +1217 end_counter += 1 218 218 continue 219 219 # end if … … 222 222 kw = m.group('kw') 223 223 if kw in start: 224 begin_counter = begin_counter +1225 # end if 226 # end if 227 self. putline(line)224 begin_counter += 1 225 # end if 226 # end if 227 self.write(line) 228 228 # end while 229 229 if begin_counter - end_counter < 0: … … 235 235 236 236 def complete(self): 237 self.indentsize = 1238 237 stack = [] 239 238 todo = [] 240 thisid = '' 241 current, firstkw, lastkw, topid = 0, '', '', '' 242 while 1: 239 currentws = thisid = firstkw = lastkw = topid = '' 240 while True: 243 241 line = self.getline() 244 i = 0 245 m = self.wsprog.match(line) 246 if m: i = m.end() 247 # end if 242 i = self.wsprog.match(line).end() 248 243 m = self.endprog.match(line) 249 244 if m: … … 270 265 # end if 271 266 # end if 272 indent = len(line[:i].expandtabs(self.tabsize)) 267 indentws = line[:i] 268 indent = len(indentws.expandtabs(self.tabsize)) 269 current = len(currentws.expandtabs(self.tabsize)) 273 270 while indent < current: 274 271 if firstkw: … … 279 276 s = '# end %s\n' % firstkw 280 277 # end if 281 self. putline(s, current)278 self.write(currentws + s) 282 279 firstkw = lastkw = '' 283 280 # end if 284 current , firstkw, lastkw, topid = stack[-1]285 del stack[-1]281 currentws, firstkw, lastkw, topid = stack.pop() 282 current = len(currentws.expandtabs(self.tabsize)) 286 283 # end while 287 284 if indent == current and firstkw: … … 298 295 s = '# end %s\n' % firstkw 299 296 # end if 300 self. putline(s, current)297 self.write(currentws + s) 301 298 firstkw = lastkw = topid = '' 302 299 # end if 303 300 # end if 304 301 if indent > current: 305 stack.append((current , firstkw, lastkw, topid))302 stack.append((currentws, firstkw, lastkw, topid)) 306 303 if thiskw and thiskw not in start: 307 304 # error 308 305 thiskw = '' 309 306 # end if 310 current , firstkw, lastkw, topid = \311 indent, thiskw, thiskw, thisid307 currentws, firstkw, lastkw, topid = \ 308 indentws, thiskw, thiskw, thisid 312 309 # end if 313 310 if thiskw: … … 327 324 # end while 328 325 # end def complete 329 330 326 # end class PythonIndenter 331 327 … … 353 349 # end def reformat_filter 354 350 355 class StringReader:356 def __init__(self, buf):357 self.buf = buf358 self.pos = 0359 self.len = len(self.buf)360 # end def __init__361 def read(self, n = 0):362 if n <= 0:363 n = self.len - self.pos364 else:365 n = min(n, self.len - self.pos)366 # end if367 r = self.buf[self.pos : self.pos + n]368 self.pos = self.pos + n369 return r370 # end def read371 def readline(self):372 i = self.buf.find('\n', self.pos)373 return self.read(i + 1 - self.pos)374 # end def readline375 def readlines(self):376 lines = []377 line = self.readline()378 while line:379 lines.append(line)380 line = self.readline()381 # end while382 return lines383 # end def readlines384 # seek/tell etc. are left as an exercise for the reader385 # end class StringReader386 387 class StringWriter:388 def __init__(self):389 self.buf = ''390 # end def __init__391 def write(self, s):392 self.buf = self.buf + s393 # end def write394 def getvalue(self):395 return self.buf396 # end def getvalue397 # end class StringWriter398 399 351 def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): 400 input = StringReader(source)401 output = StringWriter()352 input = io.BytesIO(source) 353 output = io.BytesIO() 402 354 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) 403 355 pi.complete() … … 406 358 407 359 def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): 408 input = StringReader(source)409 output = StringWriter()360 input = io.BytesIO(source) 361 output = io.BytesIO() 410 362 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) 411 363 pi.delete() … … 414 366 415 367 def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): 416 input = StringReader(source)417 output = StringWriter()368 input = io.BytesIO(source) 369 output = io.BytesIO() 418 370 pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs) 419 371 pi.reformat() … … 421 373 # end def reformat_string 422 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 # end try 384 # end if 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 # end try 390 # end def make_backup 391 423 392 def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): 424 source = open(filename, 'r').read() 393 with open(filename, 'r') as f: 394 source = f.read() 395 # end with 425 396 result = complete_string(source, stepsize, tabsize, expandtabs) 426 397 if source == result: return 0 427 398 # end if 428 import os 429 try: os.rename(filename, filename + '~') 430 except os.error: pass 431 # end try 432 f = open(filename, 'w') 433 f.write(result) 434 f.close() 399 make_backup(filename) 400 with open(filename, 'w') as f: 401 f.write(result) 402 # end with 435 403 return 1 436 404 # end def complete_file 437 405 438 406 def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): 439 source = open(filename, 'r').read() 407 with open(filename, 'r') as f: 408 source = f.read() 409 # end with 440 410 result = delete_string(source, stepsize, tabsize, expandtabs) 441 411 if source == result: return 0 442 412 # end if 443 import os 444 try: os.rename(filename, filename + '~') 445 except os.error: pass 446 # end try 447 f = open(filename, 'w') 448 f.write(result) 449 f.close() 413 make_backup(filename) 414 with open(filename, 'w') as f: 415 f.write(result) 416 # end with 450 417 return 1 451 418 # end def delete_file 452 419 453 420 def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS): 454 source = open(filename, 'r').read() 421 with open(filename, 'r') as f: 422 source = f.read() 423 # end with 455 424 result = reformat_string(source, stepsize, tabsize, expandtabs) 456 425 if source == result: return 0 457 426 # end if 458 import os 459 try: os.rename(filename, filename + '~') 460 except os.error: pass 461 # end try 462 f = open(filename, 'w') 463 f.write(result) 464 f.close() 427 make_backup(filename) 428 with open(filename, 'w') as f: 429 f.write(result) 430 # end with 465 431 return 1 466 432 # end def reformat_file … … 475 441 -s stepsize: indentation step (default %(STEPSIZE)d) 476 442 -t tabsize : the worth in spaces of a tab (default %(TABSIZE)d) 477 -e : expand TABs into spaces (defa ilt OFF)443 -e : expand TABs into spaces (default OFF) 478 444 [file] ... : files are changed in place, with backups in file~ 479 445 If no files are specified or a single - is given, … … 518 484 tabsize = int(a) 519 485 elif o == '-e': 520 expandtabs = 1486 expandtabs = True 521 487 # end if 522 488 # end for
Note:
See TracChangeset
for help on using the changeset viewer.