import os
import sys
import re
-import argparse
-from mako.template import Template
-from mako.exceptions import RichTraceback
-
-def write_template_to_string(template_filename, **kwargs):
- try:
- template = Template(filename=template_filename)
- # Split + Join fixes line-endings for whatever platform you are using
- return '\n'.join(template.render(**kwargs).splitlines())
- except:
- traceback = RichTraceback()
- for (filename, lineno, function, line) in traceback.traceback:
- print('File %s, line %s, in %s' % (filename, lineno, function))
- print(line, '\n')
- print('%s: %s' % (str(traceback.error.__class__.__name__), traceback.error))
-
-def write_template_to_file(template_filename, output_filename, **kwargs):
- with open(output_filename, 'w') as outfile:
- print(write_template_to_string(template_filename, **kwargs), file=outfile)
+from gen_common import *
def parse_event_fields(lines, idx, event_dict):
field_names = []
event_dict['names'] = enum_names
return idx
-def parse_protos(filename):
- protos = {}
+def parse_protos(protos, filename):
with open(filename, 'r') as f:
lines=f.readlines()
idx = 0
- protos['events'] = {} # event dictionary containing events with their fields
- protos['event_names'] = [] # needed to keep events in order parsed. dict is not ordered.
- protos['enums'] = {}
- protos['enum_names'] = []
-
eventId = 0
raw_text = []
while idx < len(lines):
protos['enums'][enum_name] = {}
idx = parse_enums(lines, idx, protos['enums'][enum_name])
- return protos
-
def main():
# Parse args...
- parser = argparse.ArgumentParser()
+ parser = ArgumentParser()
parser.add_argument('--proto', '-p', help='Path to proto file', required=True)
+ parser.add_argument('--proto_private', '-pp', help='Path to private proto file', required=True)
parser.add_argument('--output', '-o', help='Output filename (i.e. event.hpp)', required=True)
parser.add_argument('--gen_event_hpp', help='Generate event header', action='store_true', default=False)
parser.add_argument('--gen_event_cpp', help='Generate event cpp', action='store_true', default=False)
args = parser.parse_args()
proto_filename = args.proto
+ proto_private_filename = args.proto_private
(output_dir, output_filename) = os.path.split(args.output)
print('Error: Could not find proto file %s' % proto_filename, file=sys.stderr)
return 1
- protos = parse_protos(proto_filename)
-
- # Generate event header
- if args.gen_event_hpp:
- curdir = os.path.dirname(os.path.abspath(__file__))
- template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.hpp'])
- output_fullpath = os.sep.join([output_dir, output_filename])
-
- write_template_to_file(template_file, output_fullpath,
- filename=output_filename,
- protos=protos)
-
- # Generate event implementation
- if args.gen_event_cpp:
- curdir = os.path.dirname(os.path.abspath(__file__))
- template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.cpp'])
- output_fullpath = os.sep.join([output_dir, output_filename])
-
- write_template_to_file(template_file, output_fullpath,
- filename=output_filename,
- protos=protos)
-
- # Generate event handler header
- if args.gen_eventhandler_hpp:
- curdir = os.path.dirname(os.path.abspath(__file__))
- template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandler.hpp'])
- output_fullpath = os.sep.join([output_dir, output_filename])
-
- write_template_to_file(template_file, output_fullpath,
- filename=output_filename,
- event_header='gen_ar_event.hpp',
- protos=protos)
-
- # Generate event handler header
- if args.gen_eventhandlerfile_hpp:
- curdir = os.path.dirname(os.path.abspath(__file__))
- template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandlerfile.hpp'])
- output_fullpath = os.sep.join([output_dir, output_filename])
-
- write_template_to_file(template_file, output_fullpath,
- filename=output_filename,
- event_header='gen_ar_eventhandler.hpp',
- protos=protos)
-
- return 0
+ if not os.path.exists(proto_private_filename):
+ print('Error: Could not find private proto file %s' % proto_private_filename, file=sys.stderr)
+ return 1
+
+ final_output_dir = output_dir
+ MakeDir(final_output_dir)
+ output_dir = MakeTmpDir('_codegen')
+
+ protos = {}
+ protos['events'] = {} # event dictionary containing events with their fields
+ protos['event_names'] = [] # needed to keep events in order parsed. dict is not ordered.
+ protos['enums'] = {}
+ protos['enum_names'] = []
+
+ parse_protos(protos, proto_filename)
+ parse_protos(protos, proto_private_filename)
+
+ rval = 0
+
+ try:
+ # Generate event header
+ if args.gen_event_hpp:
+ curdir = os.path.dirname(os.path.abspath(__file__))
+ template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.hpp'])
+ output_fullpath = os.sep.join([output_dir, output_filename])
+
+ MakoTemplateWriter.to_file(template_file, output_fullpath,
+ cmdline=sys.argv,
+ filename=output_filename,
+ protos=protos)
+
+ # Generate event implementation
+ if args.gen_event_cpp:
+ curdir = os.path.dirname(os.path.abspath(__file__))
+ template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.cpp'])
+ output_fullpath = os.sep.join([output_dir, output_filename])
+
+ MakoTemplateWriter.to_file(template_file, output_fullpath,
+ cmdline=sys.argv,
+ filename=output_filename,
+ protos=protos)
+
+ # Generate event handler header
+ if args.gen_eventhandler_hpp:
+ curdir = os.path.dirname(os.path.abspath(__file__))
+ template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandler.hpp'])
+ output_fullpath = os.sep.join([output_dir, output_filename])
+
+ MakoTemplateWriter.to_file(template_file, output_fullpath,
+ cmdline=sys.argv,
+ filename=output_filename,
+ event_header='gen_ar_event.hpp',
+ protos=protos)
+
+ # Generate event handler header
+ if args.gen_eventhandlerfile_hpp:
+ curdir = os.path.dirname(os.path.abspath(__file__))
+ template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandlerfile.hpp'])
+ output_fullpath = os.sep.join([output_dir, output_filename])
+
+ MakoTemplateWriter.to_file(template_file, output_fullpath,
+ cmdline=sys.argv,
+ filename=output_filename,
+ event_header='gen_ar_eventhandler.hpp',
+ protos=protos)
+
+ rval = CopyDirFilesIfDifferent(output_dir, final_output_dir)
+
+ except:
+ rval = 1
+
+ finally:
+ DeleteDirTree(output_dir)
+
+ return rval
if __name__ == '__main__':
sys.exit(main())