Modernize the code now that Python 2.5 is no longer supported

- Use print function instead of print statement;
- Use new exception handling;
- Use in operator instead of has_key();
- Do not use tuple arguments in functions;
- Other miscellaneous improvements.

This is based on output of `futurize --stage1`, with some
manual corrections.
This commit is contained in:
Dmitry Shachnev
2014-01-19 14:17:10 +04:00
parent fa9695dbe0
commit ce2185ce27
64 changed files with 338 additions and 320 deletions

View File

@@ -38,6 +38,7 @@ file is generated with shutil.copy(), but some corner cases regarding
user/group and permissions could leave the backup file more readable that
you'd prefer. You can always use the --nobackup option to prevent this.
"""
from __future__ import print_function
__version__ = "1"
@@ -59,8 +60,8 @@ makebackup = True
def usage(msg=None):
if msg is not None:
print >> sys.stderr, msg
print >> sys.stderr, __doc__
print(msg, file=sys.stderr)
print(__doc__, file=sys.stderr)
def errprint(*args):
sep = ""
@@ -75,7 +76,7 @@ def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "drnvh",
["dryrun", "recurse", "nobackup", "verbose", "help"])
except getopt.error, msg:
except getopt.error as msg:
usage(msg)
return
for o, a in opts:
@@ -101,7 +102,7 @@ def main():
def check(file):
if os.path.isdir(file) and not os.path.islink(file):
if verbose:
print "listing directory", file
print("listing directory", file)
names = os.listdir(file)
for name in names:
fullname = os.path.join(file, name)
@@ -113,10 +114,10 @@ def check(file):
return
if verbose:
print "checking", file, "...",
print("checking", file, "...", end=' ')
try:
f = open(file)
except IOError, msg:
except IOError as msg:
errprint("%s: I/O Error: %s" % (file, str(msg)))
return
@@ -124,24 +125,24 @@ def check(file):
f.close()
if r.run():
if verbose:
print "changed."
print("changed.")
if dryrun:
print "But this is a dry run, so leaving it alone."
print("But this is a dry run, so leaving it alone.")
if not dryrun:
bak = file + ".bak"
if makebackup:
shutil.copyfile(file, bak)
if verbose:
print "backed up", file, "to", bak
print("backed up", file, "to", bak)
f = open(file, "w")
r.write(f)
f.close()
if verbose:
print "wrote new", file
print("wrote new", file)
return True
else:
if verbose:
print "unchanged."
print("unchanged.")
return False
def _rstrip(line, JUNK='\n \t'):
@@ -262,7 +263,7 @@ class Reindenter:
return line
# Line-eater for tokenize.
def tokeneater(self, type, token, (sline, scol), end, line,
def tokeneater(self, type, token, position, end, line,
INDENT=tokenize.INDENT,
DEDENT=tokenize.DEDENT,
NEWLINE=tokenize.NEWLINE,
@@ -285,7 +286,7 @@ class Reindenter:
elif type == COMMENT:
if self.find_stmt:
self.stats.append((sline, -1))
self.stats.append((position[0], -1))
# but we're still looking for a new stmt, so leave
# find_stmt alone
@@ -298,7 +299,7 @@ class Reindenter:
# ENDMARKER.
self.find_stmt = 0
if line: # not endmarker
self.stats.append((sline, self.level))
self.stats.append((position[0], self.level))
# Count number of leading blanks.
def getlspace(line):