blob: c6841b687e6dbc47bef9157b02d5e29c4b6083fe [file] [log] [blame]
Zsolt Haraszti46c72002016-10-10 09:55:30 -07001#!/usr/bin/env python
2#
Zsolt Harasztiaccad4a2017-01-03 21:56:48 -08003# Copyright 2017 the original author or authors.
Zsolt Haraszti46c72002016-10-10 09:55:30 -07004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""
19Load a protobuf description file or protoc CodeGeneratorRequest an make
20sense of it
21"""
22
23import os
24import inspect
25from collections import OrderedDict
26
27import sys
28
29from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest
30from google.protobuf.descriptor import FieldDescriptor, Descriptor
31from google.protobuf.descriptor_pb2 import FileDescriptorProto, MethodOptions
32from google.protobuf.message import Message, DecodeError
33from simplejson import dumps
34
35from google.protobuf import descriptor_pb2
36
37
38class InvalidDescriptorError(Exception): pass
39
40
41class DescriptorParser(object):
42
43 def __init__(self, ignore_empty_source_code_info=True):
44 self.ignore_empty_source_code_info = ignore_empty_source_code_info
45 self.catalog = {}
46 self.meta, blob = self.load_root_descriptor()
47 self.load_descriptor(blob)
48
49 def load_root_descriptor(self):
50 """Load descriptor.desc to make things more data driven"""
51 with open('descriptor.desc', 'r') as f:
52 blob = f.read()
53 proto = descriptor_pb2.FileDescriptorSet()
54 proto.ParseFromString(blob)
55 assert len(proto.file) == 1
56 fdp = proto.file[0]
57
58 # for i, (fd, v) in enumerate(fdp.ListFields()):
59 # assert isinstance(fd, FieldDescriptor)
60 # print fd.name, fd.full_name, fd.number, fd.type, fd.label, fd.message_type, type(v)
61
62 return fdp, blob
63
64 def get_catalog(self):
65 return self.catalog
66
67 def load_descriptor(self, descriptor_blob,
68 fold_comments=True,
69 type_tag_name='_type'):
70
71 # decode file descriptor set or if that is not possible,
72 # try plugin request
73 try:
74 message = descriptor_pb2.FileDescriptorSet()
75 message.ParseFromString(descriptor_blob)
76 except DecodeError:
77 message = CodeGeneratorRequest()
78 message.ParseFromString(descriptor_blob)
79
80 d = self.parse(message, type_tag_name=type_tag_name)
81 print d.keys()
82 for _file in d.get('file', None) or d['proto_file']:
83 if fold_comments:
84 self.fold_comments_in(_file)
85 self.catalog[_file['package']] = _file
86
87 def parse_message(self, m, type_tag_name=None):
88 assert isinstance(m, Message)
89 d = OrderedDict()
90 for fd, v in m.ListFields():
91 assert isinstance(fd, FieldDescriptor)
92 if fd.label in (1, 2):
93 d[fd.name] = self.parse(v, type_tag_name)
94 elif fd.label == 3:
95 d[fd.name] = [self.parse(x, type_tag_name) for x in v]
96 else:
97 raise InvalidDescriptorError()
98
99 if type_tag_name is not None:
100 d[type_tag_name] = m.DESCRIPTOR.full_name
101
102 return d
103
104 parser_table = {
105 unicode: lambda x: x,
106 int: lambda x: x,
107 bool: lambda x: x,
108 }
109
110 def parse(self, o, type_tag_name=None):
111 if isinstance(o, Message):
112 return self.parse_message(o, type_tag_name)
113 else:
114 return self.parser_table[type(o)](o)
115
116 def fold_comments_in(self, descriptor):
117 assert isinstance(descriptor, dict)
118
119 locations = descriptor.get('source_code_info', {}).get('location', [])
120 for location in locations:
121 path = location.get('path', [])
122 comments = ''.join([
123 location.get('leading_comments', '').strip(' '),
124 location.get('trailing_comments', '').strip(' '),
125 ''.join(block.strip(' ') for block
126 in location.get('leading_detached_comments', ''))
127 ]).strip()
128
129 # ignore locations with no comments
130 if not comments:
131 continue
132
133 # we ignore path with odd number of entries, since these do
134 # not address our schema nodes, but rather the meta schema
135 if (len(path) % 2 == 0):
136 node = self.find_node_by_path(
137 path, self.meta.DESCRIPTOR, descriptor)
138 assert isinstance(node, dict)
139 node['_description'] = comments
140
141 # remove source_code_info
142 del descriptor['source_code_info']
143
144 def find_node_by_path(self, path, meta, o):
145
146 # stop recursion when path is empty
147 if not path:
148 return o
149
150 # sanity check
151 assert len(path) >= 2
152 assert isinstance(meta, Descriptor)
153 assert isinstance(o, dict)
154
155 # find field name, then actual field
156 field_number = path.pop(0)
157 field_def = meta.fields_by_number[field_number]
158 field = o[field_def.name]
159
160 # field must be a list, extract entry with given index
161 assert isinstance(field, list) # expected to be a list field
162 index = path.pop(0)
163 child_o = field[index]
164
165 child_meta = field_def.message_type
166 return self.find_node_by_path(path, child_meta, child_o)
167
168
169if __name__ == '__main__':
170
171 # try loading voltha descriptor and turn it into JSON data as a preparation
172 # for generating JSON Schema / swagger file (to be done later)
173 if len(sys.argv) >= 2:
174 desc_file = sys.argv[1]
175 else:
176 desc_dir = os.path.dirname(inspect.getfile(voltha_pb2))
177 desc_file = os.path.join(desc_dir, 'voltha.desc')
178
179 from voltha.protos import voltha_pb2
180 with open(desc_file, 'rb') as f:
181 descriptor_blob = f.read()
182
183 parser = DescriptorParser()
184 parser.save_file_desc = '/tmp/grpc_introspection.out'
185
186 parser.load_descriptor(descriptor_blob)
187 print dumps(parser.get_catalog(), indent=4)
188 sys.exit(0)
189
190 # try to see if we can decode binary data into JSON automatically
191 from random import seed, randint
192 seed(0)
193
194 def make_mc(name, n_children=0):
195 mc = voltha_pb2.MoreComplex(
196 name=name,
197 foo_counter=randint(0, 10000),
198 health=voltha_pb2.HealthStatus(
199 state=voltha_pb2.HealthStatus.OVERLOADED
200 ),
201 address=voltha_pb2.Address(
202 street='1383 N McDowell Blvd',
203 city='Petaluma',
204 zip=94954,
205 state='CA'
206 ),
207 children=[make_mc('child%d' % (i + 1)) for i in xrange(n_children)]
208 )
209 return mc
210
211 mc = make_mc('root', 3)
212 blob = mc.SerializeToString()
213 print len(blob), 'bytes'
214 mc2 = voltha_pb2.MoreComplex()
215 mc2.ParseFromString(blob)
216 assert mc == mc2
217
218 print dumps(parser.parse(mc, type_tag_name='_type'), indent=4)