forked from mildsunrise/protobuf-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·38 lines (32 loc) · 1.1 KB
/
main.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
#!/usr/bin/env python3
from sys import stdin, argv
from os.path import ismount, exists, join
from runpy import run_path
from lib.types import StandardParser
# Parse arguments
root_type = "root"
if len(argv) >= 2: root_type = argv[1]
# Load the config
config = {}
directory = "."
while not ismount(directory):
filename = join(directory, "protobuf_config.py")
if exists(filename):
config = run_path(filename)
break
directory = join(directory, "..")
# Create and initialize parser with config
parser = StandardParser()
if "types" in config:
for type, value in config["types"].items():
assert(type not in parser.types)
parser.types[type] = value
if "native_types" in config:
for type, value in config["native_types"].items():
parser.native_types[type] = value
# Make sure root type is defined and not compactable
if root_type not in parser.types: parser.types[root_type] = {}
parser.types[root_type]["compact"] = False
# PARSE!
print(parser.safe_call(parser.match_handler("message"), stdin.buffer, root_type) + "\n")
exit(1 if len(parser.errors_produced) else 0)