-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin.py
executable file
·63 lines (44 loc) · 1.68 KB
/
plugin.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
#!/usr/bin/env python
import json
import logging
import sys
from google.protobuf.compiler import plugin_pb2 as plugin
from google.protobuf.descriptor_pb2 import FileDescriptorProto
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
def process_file(
proto_file: FileDescriptorProto, response: plugin.CodeGeneratorResponse
) -> None:
logger.info(f"Processing proto_file: {proto_file.name}")
# Create dict of options
options = str(proto_file.options).strip().replace("\n", ", ").replace('"', "")
options_dict = dict(item.split(": ") for item in options.split(", ") if options)
# Create list of dependencies
dependencies_list = list(proto_file.dependency)
data = {
"package": f"{proto_file.package}",
"filename": f"{proto_file.name}",
"dependencies": dependencies_list,
"options": options_dict,
}
file = response.file.add()
file.name = proto_file.name + ".json"
logger.info(f"Creating new file: {file.name}")
file.content = json.dumps(data, indent=2) + "\r\n"
def process(
request: plugin.CodeGeneratorRequest, response: plugin.CodeGeneratorResponse
) -> None:
for proto_file in request.proto_file:
process_file(proto_file, response)
def main() -> None:
# Load the request from stdin
request = plugin.CodeGeneratorRequest.FromString(sys.stdin.buffer.read())
# Create a response
response = plugin.CodeGeneratorResponse()
process(request, response)
# Serialize response and write to stdout
sys.stdout.buffer.write(response.SerializeToString())
if __name__ == "__main__":
main()