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

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: similarity=25 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.
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 from datetime import datetime
17 import re
18
19 LICENSE = ("""// Copyright %s The Chromium Authors. All rights reserved.
Devlin 2015/12/04 17:44:23 Might almost be worth defining this as a constant
stevenjb 2015/12/04 18:24:07 Done.
20 // Use of this source code is governed by a BSD-style license that can be
21 // found in the LICENSE file.
22 """ % datetime.now().year)
23
24 INFO = """// This file was generated by:
25 // %s.
26 """
27
28 class JsInterfaceGenerator(object):
29 def Generate(self, namespace):
30 return _Generator(namespace).Generate()
31
32 def GetHeader(tool, namespace):
33 """Returns the file header text.
34 """
35 return (LICENSE + '\n' + (INFO % tool) + '\n' +
36 ('/** @fileoverview Interface for %s that can be overriden. */' %
37 namespace))
38 GetHeader = staticmethod(GetHeader)
39
40 class _Generator(object):
41 def __init__(self, namespace):
42 self._namespace = namespace
43 first = namespace.name[0].upper()
44 rest = namespace.name[1:]
45 self._interface = first + rest
46 self._js_util = JsUtil()
47
48 def Generate(self):
49 """Generates a Code object with the schema for the entire namespace.
50 """
51 c = Code()
52 (c.Append(JsInterfaceGenerator.GetHeader(sys.argv[0],
53 self._namespace.name))
54 .Append())
55
56 c.Cblock(self._GenerateInterfaceObject())
57
58 c.Sblock('%s.prototype = {' % self._interface)
59
60 for function in self._namespace.functions.values():
61 c.Cblock(self._GenerateFunction(function))
62
63 for event in self._namespace.events.values():
64 c.Cblock(self._GenerateEvent(event))
65
Devlin 2015/12/04 17:44:23 What about enums?
stevenjb 2015/12/04 18:24:07 We don't need them in the interface, they are alre
Devlin 2015/12/04 18:28:59 Well, the enums are exposed as JS objects on the A
66 c.TrimTrailingNewlines()
67
68 c.Eblock('};')
69
70 return c
71
72 def _GenerateInterfaceObject(self):
73 """Generates the code creating the interface object.
74 For example:
75 /** @interface */
76 function SettingsPrivate() {}
77 """
78 c = Code()
79 (c.Append('/** @interface */')
80 .Append('function %s() {}' % self._interface))
81 return c
82
83 def _GenerateFunction(self, function):
84 """Generates the inteface for a function, including a JSDoc comment.
85 """
86 c = Code()
87 if function.deprecated:
88 return c
89
90 (c.Concat(self._js_util.GenerateFunctionJsDoc(self._namespace.name,
91 function))
92 .Append('%s: assertNotReached,' % (function.name)))
Devlin 2015/12/04 17:44:23 Do we want asssertNotReached over just function()
stevenjb 2015/12/04 18:24:07 asssertNotReached is what michael used in the exis
93
94 return c
95
96 def _GenerateEvent(self, event):
97 """Generates the interface for an event.
98 """
99 c = Code()
100 c.Sblock(line='/**', line_prefix=' * ')
101 if (event.description):
102 c.Comment(event.description, comment_prefix='')
103 c.Append('@type {!ChromeEvent}')
104 c.Append(self._js_util.GenerateSeeLink(self._namespace.name, 'event',
105 event.name))
106 c.Eblock(' */')
107 c.Append('%s: new ChromeEvent(),' % (event.name))
108 return c
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/js_externs_generator_test.py ('k') | tools/json_schema_compiler/js_interface_generator_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698