OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 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 Generator that produces an interface file for the Closure Compiler. |
| 6 Note: This is a work in progress, and generated interfaces may require tweaking. |
| 7 """ |
| 8 |
| 9 from code import Code |
| 10 from js_util import JsUtil |
| 11 from model import * |
| 12 from schema_util import * |
| 13 |
| 14 import os |
| 15 import sys |
| 16 import re |
| 17 |
| 18 ASSERT = """ |
| 19 assertNotReached('Interface file for Closure Compiler should not be executed.'); |
| 20 """ |
| 21 |
| 22 class JsInterfaceGenerator(object): |
| 23 def Generate(self, namespace): |
| 24 return _Generator(namespace).Generate() |
| 25 |
| 26 class _Generator(object): |
| 27 def __init__(self, namespace): |
| 28 self._namespace = namespace |
| 29 first = namespace.name[0].upper() |
| 30 rest = namespace.name[1:] |
| 31 self._interface = first + rest |
| 32 self._js_util = JsUtil() |
| 33 |
| 34 def Generate(self): |
| 35 """Generates a Code object with the schema for the entire namespace. |
| 36 """ |
| 37 c = Code() |
| 38 (c.Append(self._GetHeader(sys.argv[0], self._namespace.name)) |
| 39 .Append()) |
| 40 |
| 41 c.Cblock(self._GenerateInterfaceObject()) |
| 42 |
| 43 c.Sblock('%s.prototype = {' % self._interface) |
| 44 |
| 45 for function in self._namespace.functions.values(): |
| 46 c.Cblock(self._GenerateFunction(function)) |
| 47 |
| 48 for event in self._namespace.events.values(): |
| 49 c.Cblock(self._GenerateEvent(event)) |
| 50 |
| 51 c.TrimTrailingNewlines() |
| 52 |
| 53 c.Eblock('};') |
| 54 |
| 55 return c |
| 56 |
| 57 def _GetHeader(self, tool, namespace): |
| 58 """Returns the file header text. |
| 59 """ |
| 60 return (self._js_util.GetLicense() + '\n' + |
| 61 self._js_util.GetInfo(tool) + '\n' + |
| 62 ('/** @fileoverview Interface for %s that can be overriden. */' % |
| 63 namespace) + '\n' + |
| 64 ASSERT); |
| 65 |
| 66 def _GenerateInterfaceObject(self): |
| 67 """Generates the code creating the interface object. |
| 68 For example: |
| 69 /** @interface */ |
| 70 function SettingsPrivate() {} |
| 71 """ |
| 72 c = Code() |
| 73 (c.Append('/** @interface */') |
| 74 .Append('function %s() {}' % self._interface)) |
| 75 return c |
| 76 |
| 77 def _GenerateFunction(self, function): |
| 78 """Generates the inteface for a function, including a JSDoc comment. |
| 79 """ |
| 80 c = Code() |
| 81 if function.deprecated: |
| 82 return c |
| 83 |
| 84 (c.Concat(self._js_util.GenerateFunctionJsDoc(self._namespace.name, |
| 85 function)) |
| 86 .Append('%s: assertNotReached,' % (function.name))) |
| 87 |
| 88 return c |
| 89 |
| 90 def _GenerateEvent(self, event): |
| 91 """Generates the interface for an event. |
| 92 """ |
| 93 c = Code() |
| 94 c.Sblock(line='/**', line_prefix=' * ') |
| 95 if (event.description): |
| 96 c.Comment(event.description, comment_prefix='') |
| 97 c.Append('@type {!ChromeEvent}') |
| 98 c.Append(self._js_util.GenerateSeeLink(self._namespace.name, 'event', |
| 99 event.name)) |
| 100 c.Eblock(' */') |
| 101 c.Append('%s: new ChromeEvent(),' % (event.name)) |
| 102 return c |
OLD | NEW |