/
catalog.py
457 lines (397 loc) · 18 KB
/
catalog.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of VoltDB.
# Copyright (C) 2008-2010 VoltDB Inc.
#
# VoltDB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# VoltDB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with VoltDB. If not, see <http://www.gnu.org/licenses/>.
"""
VoltDB catalog code generator.
"""
from catalog_utils import *
from string import Template
from subprocess import Popen
#
# Code generation (shared).
#
def writer( f ):
def write( *args ):
f.write( ' '.join( map( str, args ) ) + '\n' )
return write
def interp(text, params = locals()):
t = Template(text)
#return t.safe_substitute(params)
return t.substitute(params)
#
# Java code generation.
#
def javatypify( x ):
if x == 'string': return 'String'
elif x == 'int': return 'int'
elif x == 'bool': return 'boolean'
elif x[-1] == '*': return 'CatalogMap<%s>' % x.rstrip('*')
elif x[-1] == '?': return x.rstrip('?')
else: raise Exception( 'bad type: ' + x )
def javaobjectify( x ):
if x == 'string': return 'String'
elif x == 'int': return 'Integer'
elif x == 'bool': return 'Boolean'
elif x[-1] == '*': return 'CatalogMap<%s>' % x.rstrip('*')
elif x[-1] == '?': return x.rstrip('?')
else: raise Exception( 'bad type: ' + x )
def genjava( classes, prepath, postpath, package ):
##########
# SETUP
##########
pkgdir = package.replace('.', '/')
os.system( interp( "rm -rf $postpath/*", locals() ) )
os.system( interp( "mkdir -p $postpath/", locals() ) )
os.system( interp( "cp $prepath/Catalog.java $postpath", locals() ) )
os.system( interp( "cp $prepath/CatalogType.java $postpath", locals() ) )
os.system( interp( "cp $prepath/CatalogMap.java $postpath", locals() ) )
os.system( interp( "cp $prepath/CatalogException.java $postpath", locals() ) )
os.system( interp( "cp $prepath/CatalogDiffEngine.java $postpath", locals() ) )
##########
# WRITE THE SOURCE FILES
##########
for cls in classes:
clsname = cls.name
javapath = postpath + "/" + clsname + '.java'
#ensure_relative_path_exists(postpath + "/" + pkgdir)
f = file( javapath, 'w' )
if not f:
raise OSError("Can't create file %s for writing" % javapath)
write = writer( f )
write (gpl_header)
write (auto_gen_warning)
write('package', package + ';\n')
if cls.has_comment():
write('/**\n *', cls.comment)
write(' */')
write( interp( 'public class $clsname extends CatalogType {\n', locals() ) )
# fields
for field in cls.fields:
ftype = javatypify( field.type )
fname = field.name
#realtype = field.type[:-1]
#methname = fname.capitalize()
if ftype == "String":
write( interp( ' String m_$fname = new String();', locals() ) )
elif field.type[-1] == '?':
pass # don't keep local cached vars for references
else:
write( interp( ' $ftype m_$fname;', locals() ) )
write( '' )
# setBaseValues
write( ' void setBaseValues(Catalog catalog, CatalogType parent, String path, String name) {' )
write( ' super.setBaseValues(catalog, parent, path, name);')
for field in cls.fields:
ftype = javatypify( field.type )
fname = field.name
realtype = field.type[:-1]
#methname = fname.capitalize()
if field.type[-1] == '*':
write( interp( ' m_$fname = new $ftype(catalog, this, path + "/" + "$fname", $realtype.class);', locals() ) )
write( interp( ' m_childCollections.put("$fname", m_$fname);', locals() ) )
elif field.type[-1] == '?':
write( interp( ' m_fields.put("$fname", null);', locals() ) )
else:
write( interp( ' m_fields.put("$fname", m_$fname);', locals() ) )
write( ' }\n' )
# update
write ( ' public void update() {' )
for field in cls.fields:
ftype = javatypify( field.type )
fobjtype = javaobjectify( field.type )
fname = field.name
realtype = field.type[:-1]
methname = fname.capitalize()
if field.type[-1] == '?':
pass # don't keep local cached vars for references
elif field.type[-1] != '*':
write( interp( ' m_$fname = ($fobjtype) m_fields.get("$fname");', locals() ) )
write( ' }\n' )
# getter methods
for field in cls.fields:
ftype = javatypify( field.type )
fname = field.name
realtype = field.type[:-1]
methname = fname.capitalize()
if field.has_comment():
write(' /** GETTER:', field.comment, '*/')
write( interp( ' public $ftype get$methname() {', locals() ) )
if field.type[-1] == '?':
write( interp( ' Object o = getField("$fname");', locals() ) )
write( interp( ' if (o instanceof UnresolvedInfo) {', locals() ) )
write( interp( ' UnresolvedInfo ui = (UnresolvedInfo) o;', locals() ) )
write( interp( ' $ftype retval = ($ftype) m_catalog.getItemForRef(ui.path);', locals() ) )
write( interp( ' assert(retval != null);', locals() ) )
write( interp( ' m_fields.put("$fname", retval);', locals() ) )
write( interp( ' return retval;', locals() ) )
write( interp( ' }', locals() ) )
write( interp( ' return ($ftype) o;', locals() ) )
else:
write( interp( ' return m_$fname;', locals() ) )
write( ' }\n' )
# setter methods
for field in cls.fields:
ftype = javatypify( field.type )
fname = field.name
realtype = field.type[:-1]
methname = fname.capitalize()
if field.type[-1] == '*':
continue
if field.has_comment():
write(' /** SETTER:', field.comment, '*/')
write( interp( ' public void set$methname($ftype value) {', locals() ) )
if field.type[-1] == '?':
write( interp( ' m_fields.put("$fname", value);', locals() ) )
else:
write( interp( ' m_$fname = value; m_fields.put("$fname", value);', locals() ) )
write( ' }\n' )
# wrap up
write( '}' )
#
# C++ code generation.
#
def cpptypify( x ):
if x == 'string': return 'std::string'
elif x == 'int': return 'int32_t'
elif x == 'bool': return 'bool'
elif x[-1] == '*': return 'CatalogMap<%s>' % x.rstrip('*')
elif x[-1] == '?': return 'CatalogType*'
else: raise Exception( 'bad type: ' + x )
def gencpp( classes, prepath, postpath ):
##########
# SETUP
##########
os.system( interp( "rm -rf $postpath/*", locals() ) )
os.system( interp( "mkdir -p $postpath/", locals() ) )
os.system( interp( "cp $prepath/catalog.h $postpath", locals() ) )
os.system( interp( "cp $prepath/catalogtype.h $postpath", locals() ) )
os.system( interp( "cp $prepath/catalogmap.h $postpath", locals() ) )
os.system( interp( "cp $prepath/catalog.cpp $postpath", locals() ) )
os.system( interp( "cp $prepath/catalogtype.cpp $postpath", locals() ) )
##########
# WRITE THE SOURCE FILES
##########
for cls in reversed( classes ):
clsname = cls.name
referencedClasses = []
for field in cls.fields:
classType = field.type[:-1]
if (field.type[-1] == "*") or (field.type[-1] == '?'):
if classType not in referencedClasses:
referencedClasses.append(classType)
if cls.name in referencedClasses:
referencedClasses.remove(cls.name)
##########
# WRITE THE HEADER FILE
##########
f = file( postpath + "/" + clsname.lower() + ".h", 'w' )
write = writer( f )
write (gpl_header)
write (auto_gen_warning)
pp_unique_str = "CATALOG_" + clsname.upper() + "_H_"
write("#ifndef", pp_unique_str);
write("#define", pp_unique_str);
write("")
write('#include <string>')
write('#include "catalogtype.h"')
write('#include "catalogmap.h"\n')
write('namespace catalog {\n')
for classType in referencedClasses:
write("class " + classType + ";")
if cls.has_comment():
write('/**\n *', cls.comment)
write(' */')
write( interp( 'class $clsname : public CatalogType {', locals() ) )
write( ' friend class Catalog;' )
write( interp( ' friend class CatalogMap<$clsname>;', locals() ) )
# protected section
write( '\nprotected:')
# constructor
write( ' ' + clsname + '(Catalog * catalog, CatalogType * parent, const std::string &path, const std::string &name);' )
# Field Member variables.
for field in cls.fields:
ftype = cpptypify( field.type )
privname = 'm_' + field.name
write( interp( ' $ftype $privname;', locals() ) )
# update method
write("\n virtual void update();\n")
# add method
write(" virtual CatalogType * addChild(const std::string &collectionName, const std::string &name);")
# getChild method
write(" virtual CatalogType * getChild(const std::string &collectionName, const std::string &childName) const;")
# removeChild method
write(" virtual bool removeChild(const std::string &collectionName, const std::string &childName);")
# public section
write("\npublic:")
# destructor
write(' ~' + clsname + '();\n');
# getter methods
for field in cls.fields:
ftype = cpptypify( field.type )
privname = 'm_' + field.name
pubname = field.name
Pubname = pubname.capitalize()
if field.has_comment():
write(' /** GETTER:', field.comment, '*/')
if field.type == 'string':
write ( interp( ' const std::string & $pubname() const;', locals() ) )
elif field.type[-1] == '?':
write ( " const " + field.type[:-1] + " * " + pubname + "() const;")
elif field.type[-1] == '*':
write ( interp( ' const $ftype & $pubname() const;', locals() ) )
else:
write ( interp( ' $ftype $pubname() const;', locals() ) )
write( '};\n' )
write( '} // namespace catalog\n' )
write ("#endif // ", pp_unique_str)
##########
# WRITE THE CPP FILE
##########
f = file( postpath + "/" + clsname.lower() + ".cpp", 'w' )
write = writer( f )
write (gpl_header)
write (auto_gen_warning)
filename = clsname.lower()
write ( '#include <cassert>' )
write ( interp( '#include "$filename.h"', locals() ) )
write ( '#include "catalog.h"' )
otherhdrs = ['#include "%s.h"' % field.type[:-1].lower() for field in cls.fields if field.type[-1] in ['*', '?'] ]
uniques = {}
for hdr in otherhdrs:
uniques[hdr] = hdr
for hdr in uniques.keys():
write( hdr )
write ( '\nusing namespace catalog;' )
write ( 'using namespace std;\n' )
# write the constructor
mapcons = ["m_%s(catalog)" % field.name for field in cls.fields if field.type[-1] == '*']
write ( interp( '$clsname::$clsname(Catalog *catalog, CatalogType *parent, const string &path, const string &name)', locals() ) )
comma = ''
if len(mapcons): comma = ','
write ( interp( ': CatalogType(catalog, parent, path, name)$comma', locals()))
mapcons = ["m_%s(catalog, this, path + \"/\" + \"%s\")" % (field.name, field.name) for field in cls.fields if field.type[-1] == '*']
if len(mapcons) > 0:
write( " " + ", ".join(mapcons))
write('{')
# init the fields and childCollections
write( ' CatalogValue value;' )
for field in cls.fields:
ftype = cpptypify( field.type )
privname = 'm_' + field.name
pubname = field.name
if field.type[-1] == '*':
write( interp( ' m_childCollections["$pubname"] = &$privname;', locals() ) )
else:
write( interp( ' m_fields["$pubname"] = value;', locals() ) )
write ( "}\n" )
# write the destructor
itr_exists = [ ]
write(clsname + '::~' + clsname + '() {');
for field in cls.fields:
if field.type[-1] == '*':
ftype = field.type.rstrip('*')
itr = ftype.lower() + '_iter'
privname = 'm_' + field.name
tab = ' '
if itr in itr_exists:
write(interp('$tab $itr = $privname.begin();', locals()))
else:
write(interp('$tab std::map<std::string, $ftype*>::const_iterator $itr = $privname.begin();', locals()))
write(interp('$tab while ($itr != $privname.end()) {', locals()))
write(interp('$tab $tab delete $itr->second;', locals()))
write(interp('$tab $tab $itr++;', locals()))
write(interp('$tab }', locals()))
write(interp('$tab $privname.clear();\n', locals()))
itr_exists.append(itr)
write('}\n')
# write update()
write ( interp( 'void $clsname::update() {', locals() ) )
for field in cls.fields:
ftype = cpptypify( field.type )
privname = 'm_' + field.name
pubname = field.name
if field.type[-1] == '?':
ext = "typeValue"
write( interp( ' $privname = m_fields["$pubname"].$ext;', locals() ) )
elif field.type[-1] != '*':
ext = "intValue"
if (ftype == 'std::string'):
ext = "strValue.c_str()"
write( interp( ' $privname = m_fields["$pubname"].$ext;', locals() ) )
write ( "}\n" )
# write add(...)
write ( interp( 'CatalogType * $clsname::addChild(const std::string &collectionName, const std::string &childName) {', locals() ) )
for field in cls.fields:
if field.type[-1] == "*":
privname = 'm_' + field.name
pubname = field.name
write ( interp( ' if (collectionName.compare("$pubname") == 0) {', locals() ) )
write ( interp( ' CatalogType *exists = $privname.get(childName);', locals() ) )
write ( ' if (exists)\n return NULL;' )
write ( interp( ' return $privname.add(childName);\n }', locals() ) )
write ( ' return NULL;\n}\n' )
# write getChild(...)
write ( interp( 'CatalogType * $clsname::getChild(const std::string &collectionName, const std::string &childName) const {', locals() ) )
for field in cls.fields:
if field.type[-1] == "*":
privname = 'm_' + field.name
pubname = field.name
write ( interp( ' if (collectionName.compare("$pubname") == 0)', locals() ) )
write ( interp( ' return $privname.get(childName);', locals() ) )
write ( ' return NULL;\n}\n' )
# write removeChild(...)
write ( interp( 'bool $clsname::removeChild(const std::string &collectionName, const std::string &childName) {', locals() ) )
write ( interp( ' assert (m_childCollections.find(collectionName) != m_childCollections.end());', locals() ) )
for field in cls.fields:
if field.type[-1] == "*":
privname = 'm_' + field.name
pubname = field.name
write ( interp( ' if (collectionName.compare("$pubname") == 0) {', locals() ) )
write ( interp( ' return $privname.remove(childName);', locals() ) )
write ( interp( ' }', locals() ) )
write ( interp( ' return false;', locals() ) )
write ( '}\n' )
# write field getters
for field in cls.fields:
ftype = cpptypify( field.type )
privname = 'm_' + field.name
pubname = field.name
outertype = field.type[:-1]
if field.type == 'string':
write ( interp( 'const string & $clsname::$pubname() const {\n return $privname;\n}\n', locals() ) )
elif field.type[-1] == '?':
write ( interp( 'const $outertype * $clsname::$pubname() const {', locals() ) )
write ( interp( ' return dynamic_cast<$outertype*>($privname);\n}\n', locals() ) )
elif field.type[-1] == '*':
write ( interp( 'const $ftype & $clsname::$pubname() const {\n return $privname;\n}\n', locals() ) )
else:
write ( interp( '$ftype $clsname::$pubname() const {\n return $privname;\n}\n', locals() ) )
#
# Main.
#
def main():
specpath = "spec.txt"
javapkg = 'org.voltdb.catalog'
cpp_postpath = 'out/cppsrc'
cpp_prepath = 'in/cppsrc'
java_prepath = 'in/javasrc'
java_postpath = 'out/javasrc'
f = file( specpath )
classes = parse( f.read() )
genjava( classes, java_prepath, java_postpath, javapkg )
gencpp( classes, cpp_prepath, cpp_postpath )
main()