-
Notifications
You must be signed in to change notification settings - Fork 0
/
protodesc2src.py
executable file
·272 lines (221 loc) · 8.29 KB
/
protodesc2src.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
#!/usr/bin/env python3
import argparse
import functools
import os
import sys
from google.protobuf import descriptor_pb2
def remove_prefix(string, prefix):
if string.startswith(prefix):
return string[len(prefix):]
else:
return string
def Indent(fun):
@functools.wraps(fun)
def wrapped(self, *args, **kwargs):
self.indent_level += 1
result = fun(self, *args, **kwargs)
self.indent_level -= 1
return result
return wrapped
class ProtoWriter:
def __init__(self, indent=4):
if type(indent) == int:
self.indent = ' ' * indent
elif type(indent) == str:
self.indent = indent
else:
raise ValueError("Invalid indent value")
def __call__(self, desc, out):
self.out = out
self.indent_level = 0
if not desc.syntax:
desc.syntax = 'proto2'
self.write_stmt(f'syntax = "{desc.syntax}"')
if desc.package:
self.package_name = desc.package
self.write_stmt(f'package {desc.package}')
self.out.write('\n')
if desc.options:
self.write_file_options(desc.options)
self.out.write('\n')
self.write_dependency(desc.dependency)
for enum in desc.enum_type:
self.write_enum_type(enum)
for msg in desc.message_type:
self.write_message_type(msg)
def get_option_value(self, value):
if type(value) == str:
return f'"{value}"'
if type(value) == bool:
return 'true' if value else 'false'
def write_file_options(self, options):
for field, value in options.ListFields():
if field.name == 'optimize_for':
value = descriptor_pb2.FileOptions.OptimizeMode.Name(value)
else:
value = self.get_option_value(value)
self.write_stmt(f'option {field.name} = {value}')
def write_dependency(self, deps):
for dep in deps:
self.write_stmt(f'import "{dep}"')
if len(deps):
self.out.write('\n')
def write_enum_type(self, enum):
self.write_ln(f'enum {enum.name} {{')
self.write_enum_body(enum)
self.write_ln('}')
@Indent
def write_enum_body(self, enum):
for value in enum.value:
self.write_stmt(f'{value.name} = {value.number}')
def write_message_type(self, message):
self.write_ln(f'message {message.name} {{')
self.write_message_body(message)
self.write_ln('}')
@Indent
def write_message_body(self, message):
for field in message.field:
self.write_message_field(message, field)
for nested_type in message.nested_type:
if nested_type.options.map_entry:
continue
self.write_message_type(nested_type)
for enum_type in message.enum_type:
self.write_enum_type(enum_type)
def write_message_field(self, message, field):
self.write_indent()
self.write_field_type(message, field)
self.write(f' {field.name} = {field.number}')
if field.default_value:
raise NotImplementedError("field default_value")
if field.extendee:
raise NotImplementedError("field extendee")
if field.options:
self.write_field_options(field.options)
self.write(';\n')
def write_ln(self, s):
self.write_indent()
self.write(s)
self.write('\n')
def write_stmt(self, s):
self.write_indent()
self.write(s)
self.write(';\n')
def write_indent(self):
self.write(self.indent * self.indent_level)
def write(self, s):
self.out.write(s)
def write_field_type(self, message, field):
type = descriptor_pb2.FieldDescriptorProto.Type.Name(field.type)
type_name = self.get_field_type_name(field)
nested = False
if type == 'TYPE_MESSAGE' or type == 'TYPE_ENUM':
nested = type_name.startswith(message.name + '.')
if nested:
type_name = type_name[len(message.name) + 1:]
if nested and type == 'TYPE_MESSAGE':
nested_type = self.get_nested_type(message, type_name)
if nested_type.options.map_entry:
key_type = self.get_field(nested_type, 'key')
value_type = self.get_field(nested_type, 'value')
type_name = f'map<{self.get_field_type_name(key_type)}, {self.get_field_type_name(value_type)}>'
self.out.write(type_name)
return
if field.label:
self.write_field_label(field.label)
self.write(type_name)
def get_field_type_name(self, field):
name = descriptor_pb2.FieldDescriptorProto.Type.Name(field.type)
name_map = {
'TYPE_BOOL': 'bool',
'TYPE_BYTES': 'bytes',
'TYPE_DOUBLE': 'double',
'TYPE_INT32': 'int32',
'TYPE_INT64': 'int64',
'TYPE_UINT32': 'uint32',
'TYPE_UINT64': 'uint64',
'TYPE_SINT32': 'sint32',
'TYPE_SINT64': 'sint64',
'TYPE_FIXED32': 'fixed32',
'TYPE_FIXED64': 'fixed64',
'TYPE_SFIXED32': 'sfixed32',
'TYPE_SFIXED64': 'sfixed64',
'TYPE_STRING': 'string',
}
if name in name_map:
return name_map[name]
if name == 'TYPE_MESSAGE' or name == 'TYPE_ENUM':
type_name = field.type_name.lstrip('.')
type_name = remove_prefix(type_name, self.package_name + '.')
return type_name
else:
raise NotImplementedError(name)
def get_nested_type(self, message, name):
for nested_type in message.nested_type:
if nested_type.name == name:
return nested_type
raise LookupError("No nested type with name " + name)
def get_field(self, messageOrType, name):
for field in messageOrType.field:
if field.name == name:
return field
def write_field_options(self, options):
for option, value in options.ListFields():
value = self.get_option_value(value)
self.write(f' [{option.name} = {value}]')
def write_field_label(self, label):
raise NotImplementedError('write_field_label')
class Desc2Proto2(ProtoWriter):
def write_field_label(self, label):
label = descriptor_pb2.FieldDescriptorProto.Label.Name(label)
if label == 'LABEL_REPEATED':
self.write('repeated ')
elif label == 'LABEL_OPTIONAL':
self.write('optional ')
elif label == 'LABEL_REQUIRED':
self.write('required ')
else:
raise RuntimeError(f'Unknown label {label}')
class Desc2Proto3(ProtoWriter):
def write_field_label(self, label):
label = descriptor_pb2.FieldDescriptorProto.Label.Name(label)
if label == 'LABEL_REPEATED':
self.write('repeated ')
elif label == 'LABEL_OPTIONAL':
pass
else:
raise RuntimeError(f'Invalid label {label} in proto3 descriptor')
def desc2proto(desc, out):
if desc.syntax and desc.syntax == 'proto3':
conv = Desc2Proto3()
else:
conv = Desc2Proto2()
conv(desc, out)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('file', help='Path to serialized file descriptor')
parser.add_argument('out', nargs='?',
help='Output file name.'
' Omit to write to the filename specified in the file descriptor.'
' Use "-" to write to stdout')
args = parser.parse_args()
with open(args.file, 'rb') as f:
pb = f.read()
desc = descriptor_pb2.FileDescriptorProto()
desc.ParseFromString(pb)
if not desc.name:
sys.exit("Invalid file descriptor: No name")
if not args.out:
out_path = desc.name.split('/')[-1]
elif args.out == '-':
out_path = None
else:
out_path = args.out
if out_path and os.path.exists(out_path):
sys.exit(f"Output path {out_path} exists. Refusing to overwrite.")
if out_path:
with open(out_path, 'w') as f:
desc2proto(desc, f)
print(f"Wrote output to {out_path}")
else:
desc2proto(desc, sys.stdout)