Path: blob/trunk/common/devtools/convert_protocol_to_json.py
4500 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(description=("Converts from .pdl to .json by invoking the pdl Python module."))15parser.add_argument(16"--map_binary_to_string",17type=bool,18help=(19"If set, binary in the .pdl is mapped to a "20"string in .json. Client code will have to "21"base64 decode the string to get the payload."22),23)24parser.add_argument("pdl_file", help="The .pdl input file to parse.")25parser.add_argument("json_file", help="The .json output file write.")26args = parser.parse_args(argv)27file_name = os.path.normpath(args.pdl_file)28with open(file_name) as input_file:29pdl_string = input_file.read()3031protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string)3233with open(os.path.normpath(args.json_file), "w") as output_file:34json.dump(protocol, output_file, indent=4, separators=(",", ": "))353637if __name__ == "__main__":38sys.exit(main(sys.argv[1:]))394041