mirror of
https://github.com/opentx/opentx.git
synced 2025-07-20 06:45:10 +03:00
Use with open(...) as ...:
context managers
Instead of manually opening and closes file objects, use the context manager that will properly handle opening and closing files for us, even in the event of some error
This commit is contained in:
parent
82c3230981
commit
4bd90d79d0
7 changed files with 234 additions and 249 deletions
|
@ -8,12 +8,14 @@ import glob
|
||||||
|
|
||||||
def addLine(filename, newline, after):
|
def addLine(filename, newline, after):
|
||||||
print(filename, newline)
|
print(filename, newline)
|
||||||
lines = file(filename, 'r').readlines()
|
with open(filename, 'r') as f:
|
||||||
|
lines = f.readlines()
|
||||||
for i, line in enumerate(lines):
|
for i, line in enumerate(lines):
|
||||||
if after in line:
|
if after in line:
|
||||||
lines.insert(i + 1, newline + '\n')
|
lines.insert(i + 1, newline + '\n')
|
||||||
break
|
break
|
||||||
file(filename, 'w').writelines(lines)
|
with open(filename, 'w') as f:
|
||||||
|
f.writelines(lines)
|
||||||
|
|
||||||
|
|
||||||
def modifyTranslations(constant, translation, after):
|
def modifyTranslations(constant, translation, after):
|
||||||
|
|
|
@ -1,22 +1,20 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import division
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
filename = sys.argv[1]
|
filename = sys.argv[1]
|
||||||
fileout = sys.argv[2]
|
fileout = sys.argv[2]
|
||||||
|
|
||||||
fr = open(filename, "rb")
|
# Read entire file
|
||||||
fw = open(fileout, "w")
|
with open(filename, "rb") as fr:
|
||||||
|
sts = fr.read()
|
||||||
|
# Parse into chunks of 16 bytes
|
||||||
|
sts = [sts[i * 16:(i + 1) * 16] for i in range(len(sts) // 16)]
|
||||||
|
|
||||||
st = fr.read(16)
|
with open(fileout, "w") as fw:
|
||||||
|
for st in sts:
|
||||||
while st:
|
|
||||||
for b in st:
|
for b in st:
|
||||||
fw.write("0x%02x," % ord(b))
|
fw.write("0x%02x," % ord(b))
|
||||||
fw.write("\n")
|
fw.write("\n")
|
||||||
st = fr.read(16)
|
|
||||||
|
|
||||||
fw.write("\n")
|
fw.write("\n")
|
||||||
|
|
||||||
fw.close()
|
|
||||||
fr.close()
|
|
||||||
|
|
|
@ -13,9 +13,8 @@ def writeheader(filename, header):
|
||||||
header should be a list of strings
|
header should be a list of strings
|
||||||
skip should be a regex
|
skip should be a regex
|
||||||
"""
|
"""
|
||||||
f = open(filename,"r")
|
with open(filename, "r") as f:
|
||||||
inpt = f.readlines()
|
inpt = f.readlines()
|
||||||
f.close()
|
|
||||||
output = []
|
output = []
|
||||||
|
|
||||||
# comment out the next 3 lines if you don't wish to preserve shebangs
|
# comment out the next 3 lines if you don't wish to preserve shebangs
|
||||||
|
@ -36,18 +35,16 @@ def writeheader(filename, header):
|
||||||
for line in inpt:
|
for line in inpt:
|
||||||
output.append(line)
|
output.append(line)
|
||||||
try:
|
try:
|
||||||
f = open(filename,'w')
|
with open(filename, 'w') as f:
|
||||||
f.writelines(output)
|
f.writelines(output)
|
||||||
f.close()
|
|
||||||
print("added header to %s" % filename)
|
print("added header to %s" % filename)
|
||||||
except IOError as err:
|
except IOError as err:
|
||||||
print("something went wrong trying to add header to %s: %s" % (filename, err))
|
print("something went wrong trying to add header to %s: %s" % (filename, err))
|
||||||
|
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main(args=sys.argv):
|
||||||
headerfile = open(args[1])
|
with open(args[1]) as headerfile:
|
||||||
header = headerfile.readlines()
|
header = headerfile.readlines()
|
||||||
headerfile.close()
|
|
||||||
writeheader(args[2], header)
|
writeheader(args[2], header)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ def createFontBitmap(filename, fontname, fontsize, fontbold, foreground, backgro
|
||||||
painter.end()
|
painter.end()
|
||||||
image.save(filename + ".png")
|
image.save(filename + ".png")
|
||||||
if coordsfile:
|
if coordsfile:
|
||||||
f = file(filename + ".specs", "w")
|
with open(filename + ".specs", "w") as f:
|
||||||
f.write("{ ")
|
f.write("{ ")
|
||||||
f.write(",".join(str(tmp) for tmp in coords))
|
f.write(",".join(str(tmp) for tmp in coords))
|
||||||
if extraWidth:
|
if extraWidth:
|
||||||
|
@ -96,7 +96,6 @@ def createFontBitmap(filename, fontname, fontsize, fontbold, foreground, backgro
|
||||||
f.write(", %d" % (int(coords[-1]) + i * (extraWidth / 12)))
|
f.write(", %d" % (int(coords[-1]) + i * (extraWidth / 12)))
|
||||||
# f.write(file("fonts/extra_%dpx.specs" % fontsize).read())
|
# f.write(file("fonts/extra_%dpx.specs" % fontsize).read())
|
||||||
f.write(" }")
|
f.write(" }")
|
||||||
f.close()
|
|
||||||
return coords
|
return coords
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -16,8 +16,7 @@ def writeSize(f, width, height):
|
||||||
f.write("%d,%d,\n" % (width, height))
|
f.write("%d,%d,\n" % (width, height))
|
||||||
|
|
||||||
|
|
||||||
f = open(sys.argv[2], "w")
|
with open(sys.argv[2], "w") as f:
|
||||||
|
|
||||||
lcdwidth = int(sys.argv[3])
|
lcdwidth = int(sys.argv[3])
|
||||||
|
|
||||||
if len(sys.argv) > 4:
|
if len(sys.argv) > 4:
|
||||||
|
|
|
@ -6,9 +6,8 @@ import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
for filename in sys.argv[1:]:
|
for filename in sys.argv[1:]:
|
||||||
f = file(filename, "r")
|
with open(filename, "r") as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
f.close()
|
|
||||||
|
|
||||||
newguard = "_" + os.path.basename(filename).upper().replace(".", "_") + "_"
|
newguard = "_" + os.path.basename(filename).upper().replace(".", "_") + "_"
|
||||||
|
|
||||||
|
@ -24,7 +23,6 @@ for filename in sys.argv[1:]:
|
||||||
while not lines[end].strip().startswith("#endif"):
|
while not lines[end].strip().startswith("#endif"):
|
||||||
end -= 1
|
end -= 1
|
||||||
lines[end] = "#endif // %s\n" % newguard
|
lines[end] = "#endif // %s\n" % newguard
|
||||||
f = file(filename, "w")
|
with open(filename, "w") as f:
|
||||||
f.write("".join(lines))
|
f.write("".join(lines))
|
||||||
f.close()
|
|
||||||
break
|
break
|
||||||
|
|
|
@ -66,8 +66,7 @@ print("Output file %s" % outputFile)
|
||||||
if docFile:
|
if docFile:
|
||||||
print("Documentation file %s" % docFile)
|
print("Documentation file %s" % docFile)
|
||||||
|
|
||||||
np = open(inputFile, "r")
|
with open(inputFile, "r") as inp:
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line = inp.readline()
|
line = inp.readline()
|
||||||
if len(line) == 0:
|
if len(line) == 0:
|
||||||
|
@ -101,10 +100,7 @@ while True:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
error = True
|
error = True
|
||||||
|
|
||||||
inp.close()
|
with open(outputFile, "w") as out:
|
||||||
|
|
||||||
out = open(outputFile, "w")
|
|
||||||
|
|
||||||
out.write("//This file was generated by luaexport.py script on %s for OpenTX version %s\n\n\n"
|
out.write("//This file was generated by luaexport.py script on %s for OpenTX version %s\n\n\n"
|
||||||
% (time.asctime(), version))
|
% (time.asctime(), version))
|
||||||
|
|
||||||
|
@ -145,8 +141,6 @@ out.write(",\n".join(data))
|
||||||
out.write("\n};\n\n")
|
out.write("\n};\n\n")
|
||||||
print("Generated %d items in luaMultipleFields[]" % len(exports_multiple))
|
print("Generated %d items in luaMultipleFields[]" % len(exports_multiple))
|
||||||
|
|
||||||
out.close()
|
|
||||||
|
|
||||||
if docFile:
|
if docFile:
|
||||||
# prepare fields
|
# prepare fields
|
||||||
all_exports = [(name, desc) for (id, name, desc) in exports]
|
all_exports = [(name, desc) for (id, name, desc) in exports]
|
||||||
|
@ -160,7 +154,7 @@ if docFile:
|
||||||
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key[0])]
|
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key[0])]
|
||||||
all_exports.sort(key=alphanum_key)
|
all_exports.sort(key=alphanum_key)
|
||||||
|
|
||||||
out = open(docFile, "w")
|
with open(docFile, "w") as out:
|
||||||
out.write("Alphabetical list of Lua fields for OpenTX version %s\n\n\n" % version)
|
out.write("Alphabetical list of Lua fields for OpenTX version %s\n\n\n" % version)
|
||||||
FIELD_NAME_WIDTH = 25
|
FIELD_NAME_WIDTH = 25
|
||||||
out.write("Field name%sField description\n" % (' ' * (FIELD_NAME_WIDTH - len('Field name'))))
|
out.write("Field name%sField description\n" % (' ' * (FIELD_NAME_WIDTH - len('Field name'))))
|
||||||
|
@ -169,8 +163,6 @@ if docFile:
|
||||||
out.write("\n".join(data))
|
out.write("\n".join(data))
|
||||||
out.write("\n")
|
out.write("\n")
|
||||||
|
|
||||||
out.close()
|
|
||||||
|
|
||||||
if warning:
|
if warning:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
elif error:
|
elif error:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue