tools/configcollector.py

changeset 982
a3a2e62a581b
parent 972
a34b73114823
child 1012
413ecd6b9801
equal deleted inserted replaced
981:5d5d84ab2c48 982:a3a2e62a581b
36 import outputfile 36 import outputfile
37 import re 37 import re
38 from pprint import pprint 38 from pprint import pprint
39 39
40 passbyvalue = {'int', 'bool', 'float', 'double' } 40 passbyvalue = {'int', 'bool', 'float', 'double' }
41 variantconversions = {
42 'QString': 'toString',
43 'bool': 'toBool',
44 'int': 'toInt',
45 'float': 'toFloat',
46 'double': 'toDouble',
47 'QChar': 'toChar',
48 'QBitArray': 'toBitArray',
49 'QDate': 'toDate',
50 'QDateTime': 'toDateTime',
51 'uint': 'toUInt',
52 'unsigned int': 'toUInt',
53 'QUrl': 'toUrl',
54 'QTime': 'toTime',
55 'QPoint': 'toPoint',
56 'QPointF': 'toPointF',
57 'QSize': 'toSize',
58 'QSizeF': 'toSizeF',
59 'qreal': 'toReal',
60 'QRect': 'toRect',
61 'QRectF': 'toRectF',
62 'QLine': 'toLine',
63 'QLineF': 'toLineF',
64 'QEasingCurve': 'toEasingCurve',
65 'qlonglong': 'toLongLong',
66 'qulonglong': 'toULongLong',
67 }
41 68
42 class ConfigCollector (object): 69 class ConfigCollector (object):
43 def __init__ (self, args): 70 def __init__ (self, args):
44 self.pattern = re.compile (r'\s*ConfigOption\s*\((.+)\)\s*') 71 self.pattern = re.compile (r'\s*ConfigOption\s*\((.+)\)\s*')
45 self.declpattern = re.compile (r'^([A-Za-z0-9,<>\[\]\(\)\{\}\s]+)\s+(\w+)(\s*=\s*(.+))?$') 72 self.declpattern = re.compile (r'^([A-Za-z0-9,<>\[\]\(\)\{\}\s]+)\s+(\w+)(\s*=\s*(.+))?$')
95 decl['enumname'] = enumname 122 decl['enumname'] = enumname
96 decl['getter'] = caseconversions.convert_case (decl['name'], style='java') 123 decl['getter'] = caseconversions.convert_case (decl['name'], style='java')
97 decl['varname'] = 'm_' + decl['getter'] 124 decl['varname'] = 'm_' + decl['getter']
98 decl['setter'] = 'set' + caseconversions.convert_case (decl['name'], style='camel') 125 decl['setter'] = 'set' + caseconversions.convert_case (decl['name'], style='camel')
99 decl['typecref'] = 'const %s&' % decl['type'] if decl['type'] not in passbyvalue else decl['type'] 126 decl['typecref'] = 'const %s&' % decl['type'] if decl['type'] not in passbyvalue else decl['type']
127
128 try:
129 decl['valuefunc'] = variantconversions[decl['type']]
130 except KeyError:
131 decl['valuefunc'] = 'value<' + decl['type'] + '>'
100 132
101 if enumname not in self.enums: 133 if enumname not in self.enums:
102 self.enums[enumname] = dict ( 134 self.enums[enumname] = dict (
103 name = enumname, 135 name = enumname,
104 typename = decl['type'], 136 typename = decl['type'],
130 write ('\t~ConfigurationValueBag();\n') 162 write ('\t~ConfigurationValueBag();\n')
131 write ('\tbool existsEntry (const QString& name);\n') 163 write ('\tbool existsEntry (const QString& name);\n')
132 write ('\tQVariant defaultValueByName (const QString& name);\n') 164 write ('\tQVariant defaultValueByName (const QString& name);\n')
133 165
134 for decl in self.decls: 166 for decl in self.decls:
135 write ('\t{type} {getter}();\n'.format (**decl)) 167 write ('\t{type} {getter}() const;\n'.format (**decl))
136 for decl in self.decls: 168 for decl in self.decls:
137 write ('\tvoid {setter} ({typecref} value);\n'.format (**decl)) 169 write ('\tvoid {setter} ({typecref} value);\n'.format (**decl))
138 170
139 write ('\n') 171 write ('\n')
140 write ('private:\n') 172 write ('private:\n')
181 fp.write ('\treturn m_defaults.find (name) != m_defaults.end();\n') 213 fp.write ('\treturn m_defaults.find (name) != m_defaults.end();\n')
182 fp.write ('}\n') 214 fp.write ('}\n')
183 fp.write ('\n') 215 fp.write ('\n')
184 216
185 for decl in self.decls: 217 for decl in self.decls:
186 fp.write ('{type} ConfigurationValueBag::{getter}()\n'.format (**decl)) 218 fp.write ('{type} ConfigurationValueBag::{getter}() const\n'.format (**decl))
187 fp.write ('{\n') 219 fp.write ('{\n')
188 fp.write ('\tstatic QVariant defaultvalue = QVariant::fromValue<{type}> ({default});\n'.format (**decl)) 220 fp.write ('\tstatic const QVariant defaultvalue = QVariant::fromValue<{type}> ({default});\n'.format (**decl))
189 fp.write ('\treturn m_settings->value ("{name}", defaultvalue).value<{type}>();\n'.format (**decl)) 221 fp.write ('\treturn m_settings->value ("{name}", defaultvalue).{valuefunc}();\n'.format (**decl))
190 fp.write ('}\n') 222 fp.write ('}\n')
191 fp.write ('\n') 223 fp.write ('\n')
192 224
193 for decl in self.decls: 225 for decl in self.decls:
194 fp.write ('void ConfigurationValueBag::{setter} ({typecref} value)\n'.format (**decl)) 226 fp.write ('void ConfigurationValueBag::{setter} ({typecref} value)\n'.format (**decl))

mercurial