OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 """ |
| 5 Generates an externs.js file for Chrome extension APIs. |
| 6 """ |
| 7 |
| 8 from code import Code |
| 9 from model import CamelName, Function, PropertyType |
| 10 from schema_util import StripNamespace |
| 11 |
| 12 import os |
| 13 from datetime import datetime |
| 14 |
| 15 LICENSE = ( |
| 16 """// Copyright %s The Chromium Authors. All rights reserved. |
| 17 // Use of this source code is governed by a BSD-style license that can be |
| 18 // found in the LICENSE file.""" % |
| 19 datetime.now().year) |
| 20 |
| 21 PREAMBLE = ( |
| 22 """// Auto generated externs.js; do not edit. |
| 23 """) |
| 24 |
| 25 class ExternsJsGenerator(object): |
| 26 def Generate(self, namespace): |
| 27 self.api_namespace_ = 'chrome.%s' % CamelName(namespace.unix_name) |
| 28 |
| 29 c = Code() |
| 30 c.Append(LICENSE) |
| 31 |
| 32 c.Append() |
| 33 |
| 34 c.Append('/**') |
| 35 c.Append(' * @const') |
| 36 c.Append(' */') |
| 37 c.Append('%s = {};' % self.api_namespace_) |
| 38 |
| 39 # TODO(dtseng): Support namespace.functions, namespace.events. |
| 40 for value in namespace.types.values(): |
| 41 c.Append('/**') |
| 42 if value.description: |
| 43 c.Append(' * %s' % value.description) |
| 44 if value.property_type.name == 'enum': |
| 45 c.Append(' * @enum {string}') |
| 46 c.Append(' */') |
| 47 c.Append('%s.%s = {' % (self.api_namespace_, value.name)) |
| 48 entries = [] |
| 49 for enum_value in value.enum_values: |
| 50 entries.append(' %s: \'%s\'' % (enum_value.name, enum_value.name)) |
| 51 c.Append(',\n'.join(entries)) |
| 52 c.Append('};') |
| 53 elif value.property_type.name == 'object': |
| 54 # TODO(dtseng): Implement fully. |
| 55 c.Append(' * @constructor') |
| 56 c.Append(' */') |
| 57 c.Append('%s.%s = function() {};' % (self.api_namespace_, value.name)) |
| 58 else: |
| 59 raise Exception('Unknown property type: %s' % value.property_type.name) |
| 60 return c |
OLD | NEW |