Use with statements to open/close files on wml{indent,lint,scope}.

This commit is contained in:
Javier Novoa 2015-07-27 22:15:24 -05:00
parent ff34814956
commit 8be183bdd9
3 changed files with 19 additions and 26 deletions

View file

@ -252,11 +252,8 @@ def convertor(linefilter, arglist, exclude):
continue
else:
try:
infp = open(filename, "rb")
outfp = open(filename + ".out", "wb")
linefilter(filename, infp, outfp)
infp.close()
outfp.close()
with open(filename,"rb") as infp, open(filename, "rb") as outfp:
linefilter(filename, infp, outfp)
except bailout as e:
sys.stderr.write('wmlindent: "%s", %d: %s\n' % (e.filename, e.lineno, e.msg))
os.remove(filename + ".out")

View file

@ -2932,13 +2932,11 @@ In your case, your system interprets your arguments as:
os.remove(backup)
os.rename(fn, backup)
if fn.endswith(".gz"):
ofp = gzip.open(fn, "w")
ofp.write(changed)
ofp.close()
with gzip.open(fn, "w") as ofp:
ofp.write(changed)
else:
ofp = open(fn, "w")
ofp.write(changed)
ofp.close()
with open(fn, "w") as ofp:
ofp.write(changed)
#except maptransform_error, e:
# sys.stderr.write("wmllint: " + `e` + "\n")
except:

View file

@ -192,8 +192,8 @@ class CrossRefLister(CrossRef):
for (namespace, filename) in xref.filelist.generator():
if filename.endswith(".png"):
try:
im = Image.open(filename)
(x, y) = im.size
with Image.open(filename) as im:
(x, y) = im.size
if x <= 60 or y <= 60:
print "%s: %d by %d" % (filename, x, y)
except IOError:
@ -300,16 +300,15 @@ class CrossRefLister(CrossRef):
outstr += "<h1 id='file:" + displayname + "' class='file_header'>From file: " + displayname + "</h1>\n"
filenamelist.append(displayname)
hdr = []
dfp = open(filename)
for line in dfp:
line = line.lstrip()
if line and line.startswith("#textdomain"):
continue
if line and line[0] == '#':
hdr.append(line[1:])
else:
break
dfp.close()
with open(filename) as dfp:
for line in dfp:
line = line.lstrip()
if line and line.startswith("#textdomain"):
continue
if line and line[0] == '#':
hdr.append(line[1:])
else:
break
if hdr:
outstr += interpret(hdr, "file_explanation")
outstr += "<dl>\n"
@ -458,9 +457,8 @@ Usage: wmlscope [options] dirpath
if collisions:
collisions = []
for (namespace, filename) in xref.filelist.generator():
ifp = open(filename)
collisions.append(hashlib.md5(ifp.read()).digest())
ifp.close()
with open(filename) as ifp:
collisions.append(hashlib.md5(ifp.read()).digest())
collisions = zip(xref.filelist.flatten(), collisions)
hashcounts = {}
for (n, h) in collisions: