tools/configcollector.py

Fri, 27 Aug 2021 00:55:32 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 27 Aug 2021 00:55:32 +0300
changeset 122
b54b350dff5d
parent 41
0abada2a9802
permissions
-rwxr-xr-x

Show concave polygons as red while drawing

41
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #!/usr/bin/env python3
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 # coding: utf-8
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 # Copyright 2015 - 2017 Teemu Piippo
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 # All rights reserved.
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 #
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 # Redistribution and use in source and binary forms, with or without
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 # modification, are permitted provided that the following conditions
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 # are met:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 #
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 # 1. Redistributions of source code must retain the above copyright
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 # notice, this list of conditions and the following disclaimer.
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 # 2. Redistributions in binary form must reproduce the above copyright
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 # notice, this list of conditions and the following disclaimer in the
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 # documentation and/or other materials provided with the distribution.
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 # 3. Neither the name of the copyright holder nor the names of its
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 # contributors may be used to endorse or promote products derived from
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 # this software without specific prior written permission.
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 #
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 from argparse import ArgumentParser
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 from collections import OrderedDict
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 import caseconversions
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 import outputfile
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 # These types are passed by value
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 passbyvalue = {'int', 'bool', 'float', 'double', 'qreal'}
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 def deduce_type(value):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 '''
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 Try to determine the type of value from the value itself.
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 '''
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 if value in('true', 'false'):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 return 'bool'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 elif value.startswith(('"', 'R"')) and value.endswith('"'):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 return 'QString'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 try:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 int(value)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 return 'int'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 except:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 pass
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 try:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 float(value)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 return 'double'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 except:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 pass
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 if value.endswith('f'):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 try:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 float(value[:-1])
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 return 'float'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 except:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 pass
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 raise ValueError('unable to deduce type of %r' % value)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 class ConfigCollector:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 def __init__(self, args):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 self.declarations = OrderedDict()
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 self.qtTypes = set()
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 self.args = args
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 def collect(self, filename):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 with open(filename) as file:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 for linenumber, line in enumerate(file, 1):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 try:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 line = line.strip()
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 if line and not line.startswith('#'):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 from re import search
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 match = search('^option (\w+) = (.+)$', line)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 if not match:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 raise ValueError('unable to parse: %r' % line)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 name, value = match.groups()
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 match = search(r'^([a-zA-Z0-9_<>]+)\s*\{(.*)\}$', value)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 try:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 typename = match.group(1)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 except:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 typename = deduce_type(value)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 self.declare(name, typename, value)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 except ValueError as error:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 from sys import stderr, exit
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 print(str.format(
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 '{file}:{line}: {error}',
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 file = filename,
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 line = linenumber,
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 error = str(error),
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 ), file = stderr)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 exit(1)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 # Sort the declarations in alphabetical order
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 self.declarations = OrderedDict(sorted(self.declarations.items(), key = lambda t: t[1]['name']))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 # Fill in additional information
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 for declaration in self.declarations.values():
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 declaration['readgate'] = caseconversions.convert_case(declaration['name'], style='java')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 declaration['writegate'] = 'set' + caseconversions.convert_case(declaration['name'], style='camel')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 declaration['togglefunction'] = 'toggle' + caseconversions.convert_case(declaration['name'], style='camel')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 if declaration['type'] in passbyvalue:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 declaration['typereference'] = declaration['type']
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 else:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 declaration['typereference'] = 'const %s&' % declaration['type']
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 def declare(self, name, typename, default):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 from re import findall
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 if name in self.declarations:
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 raise ValueError('Attempted to redeclare %r' % name)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 self.declarations[name] = {
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 'name': name,
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 'type': typename,
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 'default': default
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 }
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 # Keep a file of any Qt types, we'll need to #include them.
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 self.qtTypes.update(findall(r'Q\w+', typename))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 def writeHeader(self, device):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 device.write('#pragma once\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 device.write('#include <QMap>\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 device.write('#include <QSettings>\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 device.write('#include <glm/glm.hpp>\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 for qtType in sorted(self.qtTypes):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 device.write('#include <%s>\n' % qtType)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 device.write('#include "libraries.h"\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 device.write('\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 formatargs = {}
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 write = lambda value: device.write(value)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 write('class Configuration : private QSettings\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 write('{\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 write('public:\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 write('\tusing QSettings::QSettings;\n');
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142 write('\tbool exists(const QString& name);\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143 write('\tQVariant value(const QString& name);\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 write('\tQVariant setValue(const QString& name);\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 for declaration in self.declarations.values():
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 write('\t{type} {readgate}();\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 for declaration in self.declarations.values():
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 write('\tvoid {writegate}({typereference} value);\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 write('\tvoid {togglefunction}();\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 write('private:\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 write('\tusing QSettings::value;\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 write('\tusing QSettings::setValue;\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 write('\tstatic const QMap<QString, QVariant>& defaults();\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 write('};\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 def writeSource(self, device, headername):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 device.write('#include <QSet>\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 device.write('#include <QSettings>\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 device.write('#include <QVariant>\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 device.write('#include "%s/mainwindow.h"\n' % (self.args.sourcedir))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 device.write('#include "%s"\n' % headername)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 device.write(
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164 'const QMap<QString, QVariant>& Configuration::defaults()\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165 '{\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
166 '\tstatic const QMap<QString, QVariant> defaults {\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
167 )
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
168 for declaration in self.declarations.values():
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
169 device.write('\t\t{{"{name}", QVariant::fromValue<{type}>({default})}},\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
170 device.write('\t};\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171 '\treturn defaults;\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
172 '}\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 '\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
174 device.write('bool Configuration::exists(const QString& name)\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 device.write('{\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
176 device.write('\treturn defaults().contains(name);\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
177 device.write('}\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
178 device.write('\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
179 device.write('QVariant Configuration::value(const QString& name)\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
180 '{\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
181 '\treturn this->value(name, Configuration::defaults().value(name));\n'
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
182 '}\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
183 device.write('\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
184 for declaration in self.declarations.values():
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
185 device.write('{type} Configuration::{readgate}()\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 device.write('{\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
187 device.write('\tstatic const QVariant defaultvalue = QVariant::fromValue<{type}>({default});\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 device.write('\treturn this->value("{name}", defaultvalue).value<{type}>();\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 device.write('}\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
190 device.write('\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
191 for declaration in self.declarations.values():
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
192 device.write('void Configuration::{writegate}({typereference} value)\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
193 device.write('{\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
194 device.write('\tif (value != {default})\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
195 device.write('\t\tthis->setValue("{name}", QVariant::fromValue<{type}>(value));\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
196 device.write('\telse\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 device.write('\t\tthis->remove("{name}");\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198 device.write('}\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
199 device.write('\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
200 for declaration in filter(lambda declaration: declaration['type'] == 'bool', self.declarations.values()):
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
201 device.write('void Configuration::{togglefunction}()\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
202 device.write('{\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
203 device.write('\t{writegate}(not {readgate}());\n'.format(**declaration))
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
204 device.write('}\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
205 device.write('\n')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
206
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
207 def main():
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
208 parser = ArgumentParser(description='Collects a list of configuration objects')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
209 parser.add_argument('input')
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
210 parser.add_argument('--header', required=True)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
211 parser.add_argument('--source', required=True)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
212 parser.add_argument('--sourcedir', required=True)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
213 args = parser.parse_args()
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
214 collector = ConfigCollector(args)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
215 collector.collect(args.input)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
216 from outputfile import OutputFile
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
217 header = OutputFile(args.header)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
218 source = OutputFile(args.source)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
219 collector.writeSource(source, headername=args.header)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
220 collector.writeHeader(header)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
221 header.save(verbose = True)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
222 source.save(verbose = True)
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
223
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
224 if __name__ == '__main__':
0abada2a9802 added automated configuration collection
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
225 main()

mercurial