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 def GetHeader(tool, namespace): | |
27 """Returns the file header text. | |
28 """ | |
29 js_util = JsUtil(); | |
30 return (js_util.GetLicense() + '\n' + | |
31 js_util.GetInfo(tool) + '\n' + | |
32 ('/** @fileoverview Interface for %s that can be overriden. */' % | |
33 namespace) + '\n' + | |
34 ASSERT); | |
35 | |
36 GetHeader = staticmethod(GetHeader) | |
37 | |
38 class _Generator(object): | |
39 def __init__(self, namespace): | |
40 self._namespace = namespace | |
41 first = namespace.name[0].upper() | |
42 rest = namespace.name[1:] | |
43 self._interface = first + rest | |
44 self._js_util = JsUtil() | |
45 | |
46 def Generate(self): | |
47 """Generates a Code object with the schema for the entire namespace. | |
48 """ | |
49 c = Code() | |
50 (c.Append(JsInterfaceGenerator.GetHeader(sys.argv[0], | |
51 self._namespace.name)) | |
52 .Append()) | |
53 | |
54 c.Cblock(self._GenerateInterfaceObject()) | |
55 | |
56 c.Sblock('%s.prototype = {' % self._interface) | |
57 | |
58 for function in self._namespace.functions.values(): | |
59 c.Cblock(self._GenerateFunction(function)) | |
60 | |
61 for event in self._namespace.events.values(): | |
62 c.Cblock(self._GenerateEvent(event)) | |
63 | |
64 c.TrimTrailingNewlines() | |
65 | |
66 c.Eblock('};') | |
67 | |
68 return c | |
69 | |
70 def _GenerateInterfaceObject(self): | |
71 """Generates the code creating the interface object. | |
72 For example: | |
73 /** @interface */ | |
74 function SettingsPrivate() {} | |
75 """ | |
76 c = Code() | |
77 (c.Append('/** @interface */') | |
78 .Append('function %s() {}' % self._interface)) | |
79 return c | |
80 | |
81 def _GenerateFunction(self, function): | |
82 """Generates the inteface for a function, including a JSDoc comment. | |
83 """ | |
84 c = Code() | |
85 if function.deprecated: | |
86 return c | |
87 | |
88 (c.Concat(self._js_util.GenerateFunctionJsDoc(self._namespace.name, | |
89 function)) | |
90 .Append('%s: assertNotReached,' % (function.name))) | |
Devlin
2015/12/07 18:55:27
nit: be consistent in indentation (here and l50 di
stevenjb
2015/12/07 19:49:32
Line 50 might be "right-er" (but I think maybe not
| |
91 | |
92 return c | |
93 | |
94 def _GenerateEvent(self, event): | |
95 """Generates the interface for an event. | |
96 """ | |
97 c = Code() | |
98 c.Sblock(line='/**', line_prefix=' * ') | |
99 if (event.description): | |
100 c.Comment(event.description, comment_prefix='') | |
101 c.Append('@type {!ChromeEvent}') | |
102 c.Append(self._js_util.GenerateSeeLink(self._namespace.name, 'event', | |
103 event.name)) | |
104 c.Eblock(' */') | |
105 c.Append('%s: new ChromeEvent(),' % (event.name)) | |
106 return c | |
OLD | NEW |