Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions osi3trace/osi2read.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
"""
This program converts serialized osi trace files into a human readable txth file.
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 output.",
description="Convert a serialized osi trace file to a readable txth or json output.",
prog="osi2read",
)
parser.add_argument(
Expand All @@ -40,6 +47,15 @@ def command_line_arguments():
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()

Expand All @@ -52,12 +68,23 @@ def main():
trace = OSITrace(args.data, args.type)

if not args.output:
path = pathlib.Path(args.data).with_suffix(".txth")
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:
for message in trace:
f.write(str(message))
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()

Expand Down