Chromium Code Reviews| Index: mojo/public/tools/bindings/generate_type_mappings.py |
| diff --git a/mojo/public/tools/bindings/generate_type_mappings.py b/mojo/public/tools/bindings/generate_type_mappings.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..103410d7f4a9961c754ed2a561945359e5db6cd2 |
| --- /dev/null |
| +++ b/mojo/public/tools/bindings/generate_type_mappings.py |
| @@ -0,0 +1,57 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import argparse |
| +import os |
| +import json |
|
yzshen1
2016/04/08 19:58:25
alphabetically, please.
Sam McNally
2016/04/11 04:40:27
Done.
|
| + |
| + |
|
yzshen1
2016/04/08 19:58:25
please add a comment about what this file does, ex
Sam McNally
2016/04/11 04:40:27
Done.
|
| +def ReadTypemap(path): |
| + with open(path) as f: |
| + return json.load(f)['c++'] |
| + |
| + |
| +def ParseTypemapArgs(args): |
| + typemaps = [s for s in '\n'.join(args).split('--start-typemap\n') if s] |
| + result = {} |
| + for typemap in typemaps: |
| + result.update(ParseTypemap(typemap)) |
| + return result |
| + |
| + |
| +def ParseTypemap(typemap): |
| + values = {'type_mappings': [], 'public_headers': [], 'traits_headers': []} |
| + for line in typemap.split('\n'): |
| + if not line: |
| + continue |
| + key, _, value = line.partition('=') |
| + values[key].append(value.lstrip('/')) |
| + result = {} |
| + for typename in values['type_mappings']: |
| + mojom_type, _, native_type = typename.partition('=') |
| + result[mojom_type] = { |
| + 'typename': native_type, |
| + 'public_headers': values['public_headers'], |
| + 'traits_headers': values['traits_headers'], |
| + } |
| + return result |
| + |
| + |
| +def main(): |
| + parser = argparse.ArgumentParser() |
| + parser.add_argument('--dependency', type=str, action='append', default=[]) |
|
yzshen1
2016/04/08 19:58:25
Please add help for args.
Sam McNally
2016/04/11 04:40:27
Done.
|
| + parser.add_argument('--output', type=str, required=True) |
| + params, typemap_params = parser.parse_known_args() |
| + typemaps = ParseTypemapArgs(typemap_params) |
| + missing = [path for path in params.dependency if not os.path.exists(path)] |
| + if missing: |
| + raise IOError('Missing dependencies: %s' % ', '.join(missing)) |
| + for path in params.dependency: |
| + typemaps.update(ReadTypemap(path)) |
| + with open(params.output, 'w') as f: |
| + json.dump({'c++': typemaps}, f) |
| + |
| + |
| +if __name__ == '__main__': |
| + main() |