forked from google/j2objc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_proto.py
executable file
·126 lines (97 loc) · 3.18 KB
/
parse_proto.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
#!/usr/bin/python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Parses metadata from a .proto file
Parses metadata from a .proto file including various options and a list of top
level messages and enums declared within the proto file.
"""
import re
import string
def CamelCase(name):
result = []
cap_next = True
for ch in name:
if cap_next and ch.islower():
result.append(ch.upper())
elif ch.isalnum():
result.append(ch)
cap_next = not ch.isalpha()
return ''.join(result)
class ProtoMetadata:
"""Parses a proto file to extract options and other metadata."""
multiple_files = False
package = ''
java_package = ''
java_api_version = 2
java_alt_api_package = ''
outer_class = ''
optimize_for = 'SPEED'
def __init__(self):
self.messages = []
self.enums = []
def MatchOptions(line, data):
# package
match = re.match(r'package\s*([\w\.]+)\s*;', line)
if match:
data.package = match.group(1)
# java package
match = re.match(r'option\s+java_package\s*=\s*"([^"]+)', line)
if match:
data.java_package = match.group(1)
# outer classname
match = re.match(r'option\s+java_outer_classname\s*=\s*"([^"]+)"', line)
if match:
data.outer_class = match.group(1)
# multiple files?
match = re.match(r'option\s+java_multiple_files\s*=\s*(\S+)\s*;', line)
if match:
data.multiple_files = True if match.group(1).lower() == 'true' else False
match = re.match(r'option\s+optimize_for\s*=\s*(\S+)\s*;', line)
if match:
data.optimize_for = match.group(1)
def MatchTypes(line, data):
# messages and enums
match = re.match(r'\s*(message|enum)\s+(\S+)\s+{$', line)
if match:
if match.group(1) == 'message':
data.messages.append(match.group(2))
else:
data.enums.append(match.group(2))
def MatchGroups(line, data):
match = re.match(
r'\s*(required|optional|repeated)\s+group\s+(\S+)\s+=\s+\d+\s+{$', line)
if match:
data.messages.append(match.group(2))
def ParseProto(filename):
data = ProtoMetadata()
data.outer_class = CamelCase(filename.rsplit('/', 1)[-1].split('.', 1)[0])
with open(filename, 'r') as fh:
brace_depth = 0
inside_extend = False
for line in fh:
line = line.rstrip()
MatchOptions(line, data)
if brace_depth == 0 and re.match(r'\s*extend\s+\S+\s+{$', line):
inside_extend = True
brace_depth += 1
continue
# never emit anything for nested definitions
if brace_depth == 0:
MatchTypes(line, data)
elif brace_depth == 1 and inside_extend:
MatchGroups(line, data)
brace_depth += line.count('{')
brace_depth -= line.count('}')
if brace_depth == 0:
inside_extend = False
return data