-
Notifications
You must be signed in to change notification settings - Fork 22
/
parse_properties.py
304 lines (243 loc) · 11.1 KB
/
parse_properties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python3
import bs4 #beautifulSoup
import sys
from props import props
add_props = [
{ 'ini_path': 'display/showBack',
'cpp_name': 'property_displayShowBack',
'cpp_type': 'bool',
'cpp_default': 'false',
},
{ 'ini_path': 'display/antiAliasSamples',
'cpp_name': 'property_displayAntiAliasSamples',
'cpp_type': 'bool',
'cpp_default': '0',
},
{ 'ini_path': 'display/useTrackZero', # this is the path inside the ini file
'cpp_name': 'property_setDisplay_useTrackZero', # cpp global symbol to access setting
'cpp_default': 'false', # default cpp value of setting; should be valid C++ expression
'cpp_type' : 'bool', # cpp value type
'qml_name': 'setDisplay_useTrackZero', # qml property name, accessed via the global settings object
'qml_type': 'bool', # qml javascript type. bool, color, point, rect, string, etc
'qml_default' : 'false', # qml property default value; should be valid javascript expression
},
{ 'ini_path': 'display/topTrackNum',
'cpp_name': 'property_setDisplay_topTrackNum',
'cpp_default' : 'false',
'cpp_type' : 'bool',
'qml_name': 'setDisplay_topTrackNum',
'qml_type': 'bool',
'qml_default': 'false',
},
{ 'ini_path': 'display/colorDayBackground',
'cpp_name': 'property_setDisplay_colorDayBackground',
'cpp_default' : 'QColor::fromRgb(245, 245, 245)',
'qml_name' : 'setDisplay_colorDayBackground'
'qml_default' : "#f5f5f5"
},
{ 'ini_path': 'display/colorNightBackground',
'cpp_name': 'property_setDisplay_colorNightBackground',
'cpp_default' : 'QColor::fromRgb(50, 50, 65)',
'qml_name' : 'setDisplay_colorNightBackground'
'qml_default' : "#323241"
},
{ 'ini_path': 'display/colorDayBorder',
'cpp_name': 'property_setDisplay_colorDayBorder',
'cpp_default' : 'QColor::fromRgb(215, 228, 242)',
'qml_name' : 'setDisplay_colorDayBorder'
'qml_default' : "#d7e4f2"
},
{ 'ini_path': 'display/colorNightBorder',
'cpp_name': 'property_setDisplay_colorNightBorder',
'cpp_default' : 'QColor::fromRgb(210, 210, 230)',
'qml_name' : 'setDisplay_colorNightBorder'
'qml_default' : "#d2d2e6"
},
]
def parse_settings(file):
cpp = []
qml_cpp = []
mock_qml = []
h = []
preamble = ['#include "aogproperty.h"','',
'//Generated by parse_properties.py','']
with file:
parser = bs4.BeautifulSoup(file.read(),'lxml-xml')
settings = parser.findAll('Setting')
for s in settings:
s['Name'] = s['Name'][0].lower() + s['Name'][1:]
t = s['Type']
n = s['Name']
#print (t)
if t == 'setFeatures':
#we'll parse these separately
pass
if t == 'System.Int32' or \
t == 'System.Double' or \
t == 'System.Decimal' or \
t == 'System.Byte':
default_value = s.Value.contents[0]
#special case for a bad default value in Settings.settings
if n == 'setVehicle_tankTrailingHitchLength' and default_value[0] != '-':
default_value = "-" + default_value
if t == 'System.Int32' or t == 'system.Byte':
mock_qml.append("property int %s: %s" % (n, default_value))
qt = 'int'
else:
mock_qml.append("property double %s: %s" % (n, default_value))
qt = 'double'
elif t == 'System.Boolean':
default_value = s.Value.contents[0].lower()
mock_qml.append("property bool %s: %s" % (n, default_value))
qt = 'bool'
elif t == 'System.String' and n == 'setTool_zones':
preamble.append('QVector<int> default_zones = { ' + s.Value.contents[0] + ' };')
mock_qml.append("property var %s: [ %s ]" % (n, s.Value.contents[0]))
default_value = 'default_zones'
qt = 'leavealone'
elif t == 'System.String' and n == 'setRelay_pinConfig':
preamble.append('QVector<int> default_relay_pinConfig = { ' + s.Value.contents[0] + ' };')
mock_qml.append("property var %s: [ %s ]" % (n, s.Value.contents[0]))
default_value = 'default_relay_pinConfig'
qt = 'leavealone'
elif t == 'System.String' or \
t == 'AgOpenGPS.TBrand' or \
t == 'AgOpenGPS.HBrand' or \
t == 'AgOpenGPS.WDBrand' or \
t == 'AgOpenGPS.CFeatureSettings':
c = s.Value.contents
if c:
default_value = '"' + s.Value.contents[0] + '"'
else:
default_value = '""'
mock_qml.append("property string %s: %s" % (n, default_value))
qt = 'QString'
elif t == 'System.Drawing.Point':
default_value = 'QPoint(%s)' % s.Value.contents[0]
mock_qml.append("property point %s: \"%s\"" % (n, s.Value.contents[0]))
qt = 'QPoint'
elif t == 'System.Drawing.Color':
fields = s.Value.contents[0].split(',')
if len(fields) > 1:
default_value = 'QColor(%s)' % s.Value.contents[0]
else:
default_value = 'QColor("%s")' % s.Value.contents[0]
if "," in s.Value.contents[0]:
values = s.Value.contents[0].split(',')
colorstring = "#%02x%02x%02x" % (int(values[0]), int(values[1]), int(values[2]))
else:
colorstring = s.Value.contents[0]
mock_qml.append("property string %s: \"%s\"" % (n, colorstring))
qt = 'QColor'
else:
if s.Value.contents:
default_value = '"' + s.Value.contents[0] + '"'
else:
default_value = '""'
mock_qml.append("property string %s: %s" % (n, default_value))
qt = "QString"
if s['Name'] in props:
qs_name = props[s['Name']]
else:
props[s['Name']] = ''
qs_name = ""
if not qs_name:
sys.stderr.write ("Warning! No ini path found for %s. Generate props.py and fix.\n" % s['Name'])
cpp.append('AOGProperty property_%s("%s",%s);'% (s['Name'], qs_name, default_value))
qml_cpp.append(' addKey(QString("%s"),QString("%s"),"%s");' % (s['Name'], qs_name, qt));
h.append('extern AOGProperty property_%s;' % s['Name'])
#preamble.extend(cpp)
return (preamble, cpp, h, qml_cpp, mock_qml)
def parse_csettings(file):
cpp = []
qml_cpp = []
h = []
mock_qml = []
with file:
for line in file.readlines():
if 'public bool' in line and 'is' in line:
line = line.strip()[12:]
line = line.split(';')[0]
parts = [x.strip() for x in line.split('=')]
name = 'setFeature_%s' % parts[0]
if name in props:
qs_name = props[name]
else:
props[name] = 'displayFeatures/%s' % parts[0]
qs_name = 'displayFeatures/%s' % parts[0]
cpp.append('AOGProperty property_%s("%s",%s);'% (name, qs_name, parts[1]))
qml_cpp.append(' addKey(QString("%s"),QString("%s"), "bool");' % (name, qs_name));
h.append('extern AOGProperty property_%s;' % name)
mock_qml.append('property bool %s: %s' % (name, parts[1]))
return ([], cpp, h, qml_cpp, mock_qml)
if __name__ == '__main__':
import argparse
argparser = argparse.ArgumentParser(prog = sys.argv[0],
description='Parse C# .settings file to create c++ declarations for AOGProperty')
argparser.add_argument('-c','--cpp', action = "store_true", help = 'Output code for cpp file')
argparser.add_argument('-q','--qmlcpp', action = "store_true", help = 'Output code for QMLSettings::setupKeys() cpp file')
argparser.add_argument('-m','--mockqml', action = "store_true", help = 'Output code for MockSettings.qml')
argparser.add_argument('-i','--header', action = "store_true", help = 'Output header file')
argparser.add_argument('-d','--dict', action = "store_true", help = 'output python dict of names to help with this script.')
argparser.add_argument('settings_file', help = 'path to AOG C# Settings.settings file')
argparser.add_argument('csettings_file', help = 'path to the AOG C# Classes/CSettings.cs file')
args = argparser.parse_args()
cpp_pre,cpp,h,qml_cpp, mock_qml = parse_settings(open(args.settings_file,'r'))
cpp_pre1,cpp1,h1,qml_cpp1, mock_qml1 = parse_csettings(open(args.csettings_file,'r'))
if (args.header):
print ('#ifndef PROPERTIES_H')
print ('#define PROPERTIES_H')
print ()
print ('#include "aogsettings.h"')
print ()
for line in h:
print (line)
for line in h1:
print (line)
for i in add_props:
print ('extern AOGProperty %s;' % i['cpp_name'])
print ()
print ('#endif // PROPERTIES_H')
elif args.dict:
import pprint
print("props = ", end='')
pprint.pprint (props, sort_dicts = False)
elif args.mockqml:
print ("import QtQuick 2.15")
print ()
print ("//generated by parse_properties.py -m")
print ()
print ("Item {" )
print (" id: mockSettings")
for line in mock_qml:
print (" %s" % line)
for line in mock_qml1:
print (" %s" % line)
for prop in add_props:
if 'qml_name' in prop and 'qml_type' in prop:
if 'qml_default' in prop:
print (' property %s %s: %s' % (prop['qml_type'], prop['qml_name'], prop['qml_default']))
else:
print (' property %s %s' % (prop['qml_type'], prop['qml_name']))
print ("}")
elif args.qmlcpp:
print ('#include "qmlsettings.h"')
print ()
print ('void QMLSettings::setupKeys() {')
for line in qml_cpp:
print (line)
for line in qml_cpp1:
print (line)
for prop in add_props:
if 'qml_name' in prop and 'cpp_type' in prop:
print (' addKey(QString("%s"),QString("%s"),"%s");' % (prop['qml_name'], prop['ini_path'], prop['cpp_type']))
print ('}')
else:
for line in cpp_pre:
print (line)
for line in cpp:
print (line)
for line in cpp1:
print (line)
for i in add_props:
print ('AOGProperty %s("%s",false);' % (i['cpp_name'], i['ini_path']))