• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2017 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import argparse
7import json
8import os.path
9import sys
10
11import pdl
12
13def main(argv):
14    parser = argparse.ArgumentParser(description=(
15        "Converts from .pdl to .json by invoking the pdl Python module."))
16    parser.add_argument('--map_binary_to_string', type=bool,
17                        help=('If set, binary in the .pdl is mapped to a '
18                              'string in .json. Client code will have to '
19                              'base64 decode the string to get the payload.'))
20    parser.add_argument("pdl_file", help="The .pdl input file to parse.")
21    parser.add_argument("json_file", help="The .json output file write.")
22    args = parser.parse_args(argv)
23    file_name = os.path.normpath(args.pdl_file)
24    with open(file_name, "r") as input_file:
25        pdl_string = input_file.read()
26    protocol = pdl.loads(pdl_string, file_name, args.map_binary_to_string)
27
28    with open(os.path.normpath(args.json_file), 'w') as output_file:
29        json.dump(protocol, output_file, indent=4, separators=(',', ': '))
30
31
32if __name__ == '__main__':
33    sys.exit(main(sys.argv[1:]))
34