2 # -*- coding: utf-8 -*-
4 from __future__
import print_function
20 exports_script_outputs
= []
29 print("WARNING: Duplicate name %s found for constant %s" % (name
, CONSTANT_VALUE
))
31 dups_name
.append(name
)
32 if name
!= name
.lower():
33 print("WARNING: Name not in lower case %s found for constant %s" % (name
, CONSTANT_VALUE
))
37 def LEXP(name
, description
):
38 # print "LEXP %s, %s" % (name, description)
40 exports
.append((CONSTANT_VALUE
, name
, description
))
43 def LEXP_MULTIPLE(nameFormat
, descriptionFormat
, valuesCount
):
44 # print "LEXP_MULTIPLE %s, %s, %s" % (nameFormat, descriptionFormat, valuesCount)
45 for v
in range(valuesCount
):
46 name
= nameFormat
+ str(v
)
49 exports_multiple
.append((CONSTANT_VALUE
, nameFormat
, descriptionFormat
, valuesCount
))
53 print("Error: not enough arguments!")
55 print(" luaexport.py <version> <input txt> <output cpp> [<doc output>]")
58 inputFile
= sys
.argv
[2]
59 outputFile
= sys
.argv
[3]
61 if len(sys
.argv
) >= 4:
63 print("Version %s" % version
)
64 print("Input file %s" % inputFile
)
65 print("Output file %s" % outputFile
)
67 print("Documentation file %s" % docFile
)
69 with
open(inputFile
, "r") as inp
:
74 line
= line
.strip('\r\n')
76 # print "line: %s" % line
78 if line
.find('LEXP') < 0:
81 parts
= line
.split('LEXP')
84 print("Wrong line: %s" % line
)
86 cmd
= 'LEXP' + parts
[1]
87 cnst
= parts
[0].rstrip(', ')
88 if cnst
.find('=') != -1:
90 cnst
= cnst
.split('=')[0].strip()
91 # print "Found constant %s with command: %s" % (cnst, cmd)
94 # if CONSTANT_VALUE in dups_cnst:
95 # print "WARNING: Duplicate CONSTANT_VALUE found: %s" % CONSTANT_VALUE
98 # dups_cnst.append(CONSTANT_VALUE)
102 print("ERROR: problem with the definition: %s" % line
)
103 traceback
.print_exc()
106 with
open(outputFile
, "w") as out
:
107 out
.write("//This file was generated by luaexport.py script on %s for OpenTX version %s\n\n\n"
108 % (time
.asctime(), version
))
111 struct LuaSingleField {
117 struct LuaMultipleField {
128 // The list of Lua fields
129 // this aray is alphabetically sorted by the second field (name)
130 const LuaSingleField luaSingleFields[] = {
132 exports
.sort(key
=lambda x
: x
[1]) # sort by name
133 data
= [" {%s, \"%s\", \"%s\"}" % export
for export
in exports
]
134 out
.write(",\n".join(data
))
135 out
.write("\n};\n\n")
136 print("Generated %d items in luaFields[]" % len(exports
))
139 // The list of Lua fields that have a range of values
140 const LuaMultipleField luaMultipleFields[] = {
142 data
= [" {%s, \"%s\", \"%s\", %d}" % export
for export
in exports_multiple
]
143 out
.write(",\n".join(data
))
144 out
.write("\n};\n\n")
145 print("Generated %d items in luaMultipleFields[]" % len(exports_multiple
))
149 all_exports
= [(name
, desc
) for (id, name
, desc
) in exports
]
150 for (id, nameFormat
, descriptionFormat
, valuesCount
) in exports_multiple
:
151 for v
in range(1, valuesCount
+ 1):
152 name
= nameFormat
+ str(v
)
153 desc
= descriptionFormat
% v
154 all_exports
.append((name
, desc
))
155 # natural sort by field name
156 convert
= lambda text
: int(text
) if text
.isdigit() else text
157 alphanum_key
= lambda key
: [convert(c
) for c
in re
.split('([0-9]+)', key
[0])]
158 all_exports
.sort(key
=alphanum_key
)
160 with
open(docFile
, "w") as out
:
161 out
.write("Alphabetical list of Lua fields for OpenTX version %s\n\n\n" % version
)
162 FIELD_NAME_WIDTH
= 25
163 out
.write("Field name%sField description\n" % (' ' * (FIELD_NAME_WIDTH
- len('Field name'))))
164 out
.write("----------------------------------------------\n")
165 data
= ["%s%s%s" % (name_
, ' ' * (FIELD_NAME_WIDTH
- len(name_
)), desc_
) for (name_
, desc_
) in all_exports
]
166 out
.write("\n".join(data
))
172 os
.remove(outputFile
)