Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: tools/json_schema_compiler/js_interface_generator.py

Issue 1488773003: Add js_interface_generator for generating extensions interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 externs may require tweaking.
michaelpg 2015/12/04 21:02:19 externs -> interfaces
stevenjb 2015/12/07 18:31:24 Done.
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 class JsInterfaceGenerator(object):
19 def Generate(self, namespace):
20 return _Generator(namespace).Generate()
21
22 def GetHeader(tool, namespace):
23 """Returns the file header text.
24 """
25 js_util = JsUtil();
26 return (js_util.GetLicense() + '\n' +
27 js_util.GetInfo(tool) + '\n' +
28 ('/** @fileoverview Interface for %s that can be overriden. */' %
29 namespace))
michaelpg 2015/12/04 21:02:19 can you add the assertNotReached like here? https:
stevenjb 2015/12/07 18:31:24 Based on Devlin's confusion, yeah, I'll do that. I
30 GetHeader = staticmethod(GetHeader)
31
32 class _Generator(object):
33 def __init__(self, namespace):
34 self._namespace = namespace
35 first = namespace.name[0].upper()
36 rest = namespace.name[1:]
37 self._interface = first + rest
38 self._js_util = JsUtil()
39
40 def Generate(self):
41 """Generates a Code object with the schema for the entire namespace.
42 """
43 c = Code()
44 (c.Append(JsInterfaceGenerator.GetHeader(sys.argv[0],
45 self._namespace.name))
46 .Append())
47
48 c.Cblock(self._GenerateInterfaceObject())
49
50 c.Sblock('%s.prototype = {' % self._interface)
51
52 for function in self._namespace.functions.values():
53 c.Cblock(self._GenerateFunction(function))
54
55 for event in self._namespace.events.values():
56 c.Cblock(self._GenerateEvent(event))
57
58 c.TrimTrailingNewlines()
59
60 c.Eblock('};')
61
62 return c
63
64 def _GenerateInterfaceObject(self):
65 """Generates the code creating the interface object.
66 For example:
67 /** @interface */
68 function SettingsPrivate() {}
69 """
70 c = Code()
71 (c.Append('/** @interface */')
72 .Append('function %s() {}' % self._interface))
73 return c
74
75 def _GenerateFunction(self, function):
76 """Generates the inteface for a function, including a JSDoc comment.
77 """
78 c = Code()
79 if function.deprecated:
80 return c
81
82 (c.Concat(self._js_util.GenerateFunctionJsDoc(self._namespace.name,
83 function))
84 .Append('%s: assertNotReached,' % (function.name)))
85
86 return c
87
88 def _GenerateEvent(self, event):
89 """Generates the interface for an event.
90 """
91 c = Code()
92 c.Sblock(line='/**', line_prefix=' * ')
93 if (event.description):
94 c.Comment(event.description, comment_prefix='')
95 c.Append('@type {!ChromeEvent}')
96 c.Append(self._js_util.GenerateSeeLink(self._namespace.name, 'event',
97 event.name))
98 c.Eblock(' */')
99 c.Append('%s: new ChromeEvent(),' % (event.name))
100 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698