OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 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 |
| 6 import argparse |
| 7 import base64 |
| 8 import json |
| 9 import os |
| 10 import sys |
| 11 import zlib |
| 12 |
| 13 sys.path.insert(0, |
| 14 os.path.join(os.path.dirname(__file__), os.pardir)) |
| 15 # (/path/to/build/scripts) |
| 16 import common.env |
| 17 common.env.Install() |
| 18 |
| 19 from common import chromium_utils |
| 20 |
| 21 def main(): |
| 22 parser = argparse.ArgumentParser() |
| 23 parser.add_argument('-o', '--out', default=sys.stdout, |
| 24 help='Write the output content here. If omitted, output will be written ' |
| 25 'to STDOUT.') |
| 26 def add_value_arg(p): |
| 27 p.add_argument('value', nargs='?', |
| 28 help='The intput content. If omitted, content will be read from STDIN.') |
| 29 subparsers = parser.add_subparsers() |
| 30 |
| 31 # encode |
| 32 p = subparsers.add_parser('encode') |
| 33 def encode(value, out): |
| 34 obj = json.loads(value) |
| 35 out.write(chromium_utils.b64_gz_json_encode(obj)) |
| 36 p.set_defaults(func=encode) |
| 37 add_value_arg(p) |
| 38 |
| 39 # decode |
| 40 p = subparsers.add_parser('decode') |
| 41 def decode(value, out): |
| 42 obj = chromium_utils.convert_gz_json_type(value) |
| 43 json.dump(obj, out, sort_keys=True, separators=(',', ':')) |
| 44 p.set_defaults(func=decode) |
| 45 add_value_arg(p) |
| 46 |
| 47 opts = parser.parse_args() |
| 48 |
| 49 # Read input value. |
| 50 value = opts.value |
| 51 if not value: |
| 52 value = sys.stdin.read() |
| 53 |
| 54 # Generate output. |
| 55 opts.func(value, opts.out) |
| 56 return 0 |
| 57 |
| 58 |
| 59 if __name__ == '__main__': |
| 60 sys.exit(main()) |
OLD | NEW |