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