4242__version__ = "1"
4343
4444import tokenize
45- import os , shutil
45+ import os
46+ import shutil
4647import sys
4748
48- verbose = 0
49- recurse = 0
50- dryrun = 0
49+ verbose = False
50+ recurse = False
51+ dryrun = False
5152makebackup = True
5253
54+
5355def usage (msg = None ):
54- if msg is not None :
55- print (msg , file = sys .stderr )
56- print (__doc__ , file = sys .stderr )
56+ if msg is None :
57+ msg = __doc__
58+ print (msg , file = sys .stderr )
59+
5760
5861def errprint (* args ):
59- sep = ""
60- for arg in args :
61- sys .stderr .write (sep + str (arg ))
62- sep = " "
62+ sys .stderr .write (" " .join (str (arg ) for arg in args ))
6363 sys .stderr .write ("\n " )
6464
65+
6566def main ():
6667 import getopt
6768 global verbose , recurse , dryrun , makebackup
@@ -73,13 +74,13 @@ def main():
7374 return
7475 for o , a in opts :
7576 if o in ('-d' , '--dryrun' ):
76- dryrun += 1
77+ dryrun = True
7778 elif o in ('-r' , '--recurse' ):
78- recurse += 1
79+ recurse = True
7980 elif o in ('-n' , '--nobackup' ):
8081 makebackup = False
8182 elif o in ('-v' , '--verbose' ):
82- verbose += 1
83+ verbose = True
8384 elif o in ('-h' , '--help' ):
8485 usage ()
8586 return
@@ -91,6 +92,7 @@ def main():
9192 for arg in args :
9293 check (arg )
9394
95+
9496def check (file ):
9597 if os .path .isdir (file ) and not os .path .islink (file ):
9698 if verbose :
@@ -108,13 +110,12 @@ def check(file):
108110 if verbose :
109111 print ("checking" , file , "..." , end = ' ' )
110112 try :
111- f = open (file )
113+ with open (file ) as f :
114+ r = Reindenter (f )
112115 except IOError as msg :
113116 errprint ("%s: I/O Error: %s" % (file , str (msg )))
114117 return
115118
116- r = Reindenter (f )
117- f .close ()
118119 if r .run ():
119120 if verbose :
120121 print ("changed." )
@@ -126,9 +127,8 @@ def check(file):
126127 shutil .copyfile (file , bak )
127128 if verbose :
128129 print ("backed up" , file , "to" , bak )
129- f = open (file , "w" )
130- r .write (f )
131- f .close ()
130+ with open (file , "w" ) as f :
131+ r .write (f )
132132 if verbose :
133133 print ("wrote new" , file )
134134 return True
@@ -137,6 +137,7 @@ def check(file):
137137 print ("unchanged." )
138138 return False
139139
140+
140141def _rstrip (line , JUNK = '\n \t ' ):
141142 """Return line stripped of trailing spaces, tabs, newlines.
142143
@@ -146,10 +147,11 @@ def _rstrip(line, JUNK='\n \t'):
146147 """
147148
148149 i = len (line )
149- while i > 0 and line [i - 1 ] in JUNK :
150+ while i > 0 and line [i - 1 ] in JUNK :
150151 i -= 1
151152 return line [:i ]
152153
154+
153155class Reindenter :
154156
155157 def __init__ (self , f ):
@@ -192,9 +194,9 @@ def run(self):
192194 # we see a line with *something* on it.
193195 i = stats [0 ][0 ]
194196 after .extend (lines [1 :i ])
195- for i in range (len (stats )- 1 ):
197+ for i in range (len (stats ) - 1 ):
196198 thisstmt , thislevel = stats [i ]
197- nextstmt = stats [i + 1 ][0 ]
199+ nextstmt = stats [i + 1 ][0 ]
198200 have = getlspace (lines [thisstmt ])
199201 want = thislevel * 4
200202 if want < 0 :
@@ -206,7 +208,7 @@ def run(self):
206208 want = have2want .get (have , - 1 )
207209 if want < 0 :
208210 # Then it probably belongs to the next real stmt.
209- for j in range (i + 1 , len (stats )- 1 ):
211+ for j in range (i + 1 , len (stats ) - 1 ):
210212 jline , jlevel = stats [j ]
211213 if jlevel >= 0 :
212214 if have == getlspace (lines [jline ]):
@@ -216,11 +218,11 @@ def run(self):
216218 # comment like this one,
217219 # in which case we should shift it like its base
218220 # line got shifted.
219- for j in range (i - 1 , - 1 , - 1 ):
221+ for j in range (i - 1 , - 1 , - 1 ):
220222 jline , jlevel = stats [j ]
221223 if jlevel >= 0 :
222- want = have + getlspace (after [jline - 1 ]) - \
223- getlspace (lines [jline ])
224+ want = have + ( getlspace (after [jline - 1 ]) -
225+ getlspace (lines [jline ]) )
224226 break
225227 if want < 0 :
226228 # Still no luck -- leave it alone.
@@ -295,12 +297,14 @@ def tokeneater(self, type, token, slinecol, end, line,
295297 if line : # not endmarker
296298 self .stats .append ((slinecol [0 ], self .level ))
297299
300+
298301# Count number of leading blanks.
299302def getlspace (line ):
300303 i , n = 0 , len (line )
301304 while i < n and line [i ] == " " :
302305 i += 1
303306 return i
304307
308+
305309if __name__ == '__main__' :
306310 main ()
0 commit comments