-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathosi2read.py
More file actions
93 lines (78 loc) · 2.27 KB
/
osi2read.py
File metadata and controls
93 lines (78 loc) · 2.27 KB
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
"""
This program converts serialized osi trace files into a human readable output.
Supported output formats:
txth - Human readable text (default)
json - JSON format (machine parsable, protobuf compatible)
Example usage:
python3 osi2read.py -d trace.osi -o myreadableosifile
python3 osi2read.py -d trace.osi --format json
"""
from osi3trace.osi_trace import OSITrace
from google.protobuf.json_format import MessageToJson
import argparse
import json
import pathlib
def command_line_arguments():
"""Define and handle command line interface"""
parser = argparse.ArgumentParser(
description="Convert a serialized osi trace file to a readable txth or json output.",
prog="osi2read",
)
parser.add_argument(
"--data",
"-d",
help="Path to the file with serialized data.",
type=str,
required=True,
)
parser.add_argument(
"--type",
"-t",
help="Name of the type used to serialize data.",
choices=OSITrace.message_types(),
default="SensorView",
type=str,
required=False,
)
parser.add_argument(
"--output",
"-o",
help="Output name of the file.",
type=str,
required=False,
)
parser.add_argument(
"--format",
"-f",
help="Output format.",
choices=["txth", "json"],
default="txth",
type=str,
required=False,
)
return parser.parse_args()
def main():
# Handling of command line arguments
args = command_line_arguments()
# Initialize the OSI trace class
trace = OSITrace(args.data, args.type)
if not args.output:
suffix = ".json" if args.format == "json" else ".txth"
path = pathlib.Path(args.data).with_suffix(suffix)
args.output = str(path)
with open(args.output, "wt") as f:
if args.format == "json":
f.write("[\n")
first = True
for message in trace:
if not first:
f.write(",\n")
first = False
f.write(MessageToJson(message))
f.write("\n]\n")
else:
for message in trace:
f.write(str(message))
trace.close()
if __name__ == "__main__":
main()