1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-19 06:15: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:
Sean Vig 2015-12-13 18:57:36 -06:00
parent 82c3230981
commit 4bd90d79d0
7 changed files with 234 additions and 249 deletions

View file

@ -66,86 +66,80 @@ print("Output file %s" % outputFile)
if docFile:
print("Documentation file %s" % docFile)
np = open(inputFile, "r")
with open(inputFile, "r") as inp:
while True:
line = inp.readline()
if len(line) == 0:
break
line = line.strip('\r\n')
line = line.strip()
# print "line: %s" % line
while True:
line = inp.readline()
if len(line) == 0:
break
line = line.strip('\r\n')
line = line.strip()
# print "line: %s" % line
parts = line.split('LEXP')
# print parts
if len(parts) != 2:
print("Wrong line: %s" % line)
continue
cmd = 'LEXP' + parts[1]
cnst = parts[0].rstrip(', ')
if cnst.find('=') != -1:
# constant contains =
cnst = cnst.split('=')[0].strip()
# print "Found constant %s with command: %s" % (cnst, cmd)
try:
CONSTANT_VALUE = cnst
# if CONSTANT_VALUE in dups_cnst:
# print "WARNING: Duplicate CONSTANT_VALUE found: %s" % CONSTANT_VALUE
# warning = True
# continue
# dups_cnst.append(CONSTANT_VALUE)
# print cmd
eval(cmd)
except:
print("ERROR: problem with the definition: %s" % line)
traceback.print_exc()
error = True
parts = line.split('LEXP')
# print parts
if len(parts) != 2:
print("Wrong line: %s" % line)
continue
cmd = 'LEXP' + parts[1]
cnst = parts[0].rstrip(', ')
if cnst.find('=') != -1:
# constant contains =
cnst = cnst.split('=')[0].strip()
# print "Found constant %s with command: %s" % (cnst, cmd)
try:
CONSTANT_VALUE = cnst
# if CONSTANT_VALUE in dups_cnst:
# print "WARNING: Duplicate CONSTANT_VALUE found: %s" % CONSTANT_VALUE
# warning = True
# continue
# dups_cnst.append(CONSTANT_VALUE)
# print cmd
eval(cmd)
except:
print("ERROR: problem with the definition: %s" % line)
traceback.print_exc()
error = True
with open(outputFile, "w") as out:
out.write("//This file was generated by luaexport.py script on %s for OpenTX version %s\n\n\n"
% (time.asctime(), version))
inp.close()
header = """
struct LuaSingleField {
uint16_t id;
const char * name;
const char * desc;
};
out = open(outputFile, "w")
struct LuaMultipleField {
uint16_t id;
const char * name;
const char * desc;
uint8_t count;
};
out.write("//This file was generated by luaexport.py script on %s for OpenTX version %s\n\n\n"
% (time.asctime(), version))
"""
out.write(header)
header = """
struct LuaSingleField {
uint16_t id;
const char * name;
const char * desc;
};
out.write("""
// The list of Lua fields
// this aray is alphabetically sorted by the second field (name)
const LuaSingleField luaSingleFields[] = {
""")
exports.sort(key=lambda x: x[1]) # sort by name
data = [" {%s, \"%s\", \"%s\"}" % export for export in exports]
out.write(",\n".join(data))
out.write("\n};\n\n")
print("Generated %d items in luaFields[]" % len(exports))
struct LuaMultipleField {
uint16_t id;
const char * name;
const char * desc;
uint8_t count;
};
"""
out.write(header)
out.write("""
// The list of Lua fields
// this aray is alphabetically sorted by the second field (name)
const LuaSingleField luaSingleFields[] = {
""")
exports.sort(key=lambda x: x[1]) # sort by name
data = [" {%s, \"%s\", \"%s\"}" % export for export in exports]
out.write(",\n".join(data))
out.write("\n};\n\n")
print("Generated %d items in luaFields[]" % len(exports))
out.write("""
// The list of Lua fields that have a range of values
const LuaMultipleField luaMultipleFields[] = {
""")
data = [" {%s, \"%s\", \"%s\", %d}" % export for export in exports_multiple]
out.write(",\n".join(data))
out.write("\n};\n\n")
print("Generated %d items in luaMultipleFields[]" % len(exports_multiple))
out.close()
out.write("""
// The list of Lua fields that have a range of values
const LuaMultipleField luaMultipleFields[] = {
""")
data = [" {%s, \"%s\", \"%s\", %d}" % export for export in exports_multiple]
out.write(",\n".join(data))
out.write("\n};\n\n")
print("Generated %d items in luaMultipleFields[]" % len(exports_multiple))
if docFile:
# prepare fields
@ -160,16 +154,14 @@ if docFile:
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key[0])]
all_exports.sort(key=alphanum_key)
out = open(docFile, "w")
out.write("Alphabetical list of Lua fields for OpenTX version %s\n\n\n" % version)
FIELD_NAME_WIDTH = 25
out.write("Field name%sField description\n" % (' ' * (FIELD_NAME_WIDTH - len('Field name'))))
out.write("----------------------------------------------\n")
data = ["%s%s%s" % (name_, ' ' * (FIELD_NAME_WIDTH - len(name_)), desc_) for (name_, desc_) in all_exports]
out.write("\n".join(data))
out.write("\n")
out.close()
with open(docFile, "w") as out:
out.write("Alphabetical list of Lua fields for OpenTX version %s\n\n\n" % version)
FIELD_NAME_WIDTH = 25
out.write("Field name%sField description\n" % (' ' * (FIELD_NAME_WIDTH - len('Field name'))))
out.write("----------------------------------------------\n")
data = ["%s%s%s" % (name_, ' ' * (FIELD_NAME_WIDTH - len(name_)), desc_) for (name_, desc_) in all_exports]
out.write("\n".join(data))
out.write("\n")
if warning:
sys.exit(1)