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
|
# Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# Python source
from __future__ import print_function
import os
import sys
import re
from gen_common import *
def parse_event_fields(lines, idx, event_dict):
"""
Parses lines from a proto file that contain an event definition and stores it in event_dict
"""
fields = []
end_of_event = False
# record all fields in event definition.
# note: we don't check if there's a leading brace.
while not end_of_event and idx < len(lines):
line = lines[idx].rstrip()
idx += 1
match = re.match(r'(\s*)([\w\*]+)(\s+)(counter\s+)*([\w]+)(\[\d+\])*', line)
if match:
field = {
"type": match.group(2),
"name": match.group(5),
"size": int(match.group(6)[1:-1]) if match.group(6) else 1,
"counter": True if match.group(4) else False
}
fields.append(field)
end_of_event = re.match(r'(\s*)};', line)
event_dict['fields'] = fields
event_dict['num_fields'] = len(fields)
return idx
def parse_enums(lines, idx, event_dict):
"""
Parses lines from a proto file that contain an enum definition and stores it in event_dict
"""
enum_names = []
end_of_enum = False
# record all enum values in enumeration
# note: we don't check if there's a leading brace.
while not end_of_enum and idx < len(lines):
line = lines[idx].rstrip()
idx += 1
preprocessor = re.search(r'#if|#endif', line)
if not preprocessor:
enum = re.match(r'(\s*)(\w+)(\s*)', line)
if enum:
enum_names.append(line)
end_of_enum = re.match(r'(\s*)};', line)
event_dict['names'] = enum_names
return idx
def parse_protos(files, verbose=False):
"""
Parses a proto file and returns a dictionary of event definitions
"""
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['event_map'] = {} # dictionary to map event ids to event names
protos['enums'] = {}
protos['enum_names'] = []
eventId = 0
if type(files) is not list:
files = [files]
for filename in files:
if verbose:
print("Parsing proto file: %s" % os.path.normpath(filename))
with open(filename, 'r') as f:
lines=f.readlines()
idx = 0
raw_text = []
while idx < len(lines):
line = lines[idx].rstrip()
idx += 1
# search for event definitions.
match = re.match(r'(\s*)event(\s*)(\w+)', line)
if match:
eventId += 1
event_name = match.group(3)
protos["event_names"].append(event_name)
protos["events"][event_name] = {}
protos["events"][event_name]["event_id"] = eventId
protos["event_map"][eventId] = event_name
idx = parse_event_fields(lines, idx, protos["events"][event_name])
# search for enums.
match = re.match(r'(\s*)enum(\s*)(\w+)', line)
if match:
enum_name = match.group(3)
protos["enum_names"].append(enum_name)
protos["enums"][enum_name] = {}
idx = parse_enums(lines, idx, protos["enums"][enum_name])
return protos
def main():
# Parse args...
parser = ArgumentParser()
parser.add_argument("--proto", "-p", dest="protos", nargs='+', help="Path to all proto file(s) to process. Accepts one or more paths (i.e. events.proto and events_private.proto)", required=True)
parser.add_argument("--output-dir", help="Output dir (defaults to ./codegen). Will create folder if it does not exist.", required=False, default="codegen")
parser.add_argument("--verbose", "-v", help="Verbose", action="store_true")
args = parser.parse_args()
if not os.path.exists(args.output_dir):
MakeDir(args.output_dir)
for f in args.protos:
if not os.path.exists(f):
print('Error: Could not find proto file %s' % f, file=sys.stderr)
return 1
# Parse each proto file and add to protos container
protos = parse_protos(args.protos, args.verbose)
files = [
["gen_ar_event.hpp", ""],
["gen_ar_event.cpp", ""],
["gen_ar_eventhandler.hpp", "gen_ar_event.hpp"],
["gen_ar_eventhandlerfile.hpp", "gen_ar_eventhandler.hpp"]
]
rval = 0
try:
# Delete existing files
for f in files:
filename = f[0]
output_fullpath = os.path.join(args.output_dir, filename)
if os.path.exists(output_fullpath):
if args.verbose:
print("Deleting existing file: %s" % output_fullpath)
os.remove(output_fullpath)
# Generate files from templates
print("Generating c++ from proto files...")
for f in files:
filename = f[0]
event_header = f[1]
curdir = os.path.dirname(os.path.abspath(__file__))
template_file = os.path.join(curdir, 'templates', filename)
output_fullpath = os.path.join(args.output_dir, filename)
if args.verbose:
print("Generating: %s" % output_fullpath)
MakoTemplateWriter.to_file(template_file, output_fullpath,
cmdline=sys.argv,
filename=filename,
protos=protos,
event_header=event_header)
except Exception as e:
print(e)
rval = 1
return rval
if __name__ == '__main__':
sys.exit(main())
|