-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproto_parser.py
37 lines (30 loc) · 1.06 KB
/
proto_parser.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
"""This module provides a very simple proto parser."""
from sample import addressbook_pb2
class ProtoParser:
"""Class for parsing protos"""
def __init__(self):
self.columns = []
def _decide_and_prepare(self, message_type):
"""Decides whether the proto message is nested or simple.
Args:
message_type: Message type object.
"""
for k in message_type.fields_by_name.values():
if k.type != 11:
self.columns.append(k.full_name)
else:
self._parse_proto(message_type, 'nested')
def _parse_proto(self, desc, type='normal'):
if type == 'normal':
for key, message_type in desc.message_types_by_name.items():
self._decide_and_prepare(message_type)
else:
for key, message_type in desc.nested_types_by_name.items():
self._decide_and_prepare(message_type)
def parse(self, desc):
self._parse_proto(desc)
return self.columns
if __name__ == "__main__":
"""This can be considered as a test for now."""
parserObj = ProtoParser()
print parserObj.parse(addressbook_pb2.DESCRIPTOR)