Path: blob/trunk/common/devtools/convert_protocol_to_json.py
2884 views
#!/usr/bin/env python1# Copyright 2017 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.45import argparse6import json7import os.path8import sys910import pdl111213def main(argv):14parser = argparse.ArgumentParser(15description=("Converts from .pdl to .json by invoking the pdl Python module.")16)17parser.add_argument(18"--map_binary_to_string",19type=bool,20help=(21"If set, binary in the .pdl is mapped to a "22"string in .json. Client code will have to "23"base64 decode the string to get the payload."24),25)26parser.add_argument("pdl_file", help="The .pdl input file to parse.")27parser.add_argument("json_file", help="The .json output file write.")28args = parser.parse_args(argv)29file_name = os.path.normpath(args.pdl_file)30input_file = open(file_name, "r")31pdl_string = input_file.read()32protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string)33input_file.close()3435output_file = open(os.path.normpath(args.json_file), "w")36json.dump(protocol, output_file, indent=4, separators=(",", ": "))37output_file.close()383940if __name__ == "__main__":41sys.exit(main(sys.argv[1:]))424344