js_interface_generator.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from code import Code
  9. from js_util import JsUtil
  10. from model import *
  11. from schema_util import *
  12. import os
  13. import sys
  14. import re
  15. class JsInterfaceGenerator(object):
  16. def Generate(self, namespace):
  17. return _Generator(namespace).Generate()
  18. class _Generator(object):
  19. def __init__(self, namespace):
  20. self._namespace = namespace
  21. first = namespace.name[0].upper()
  22. rest = namespace.name[1:]
  23. self._interface = first + rest
  24. self._js_util = JsUtil()
  25. def Generate(self):
  26. """Generates a Code object with the schema for the entire namespace.
  27. """
  28. c = Code()
  29. (c.Append(self._GetHeader(sys.argv[0], self._namespace.name))
  30. .Append())
  31. self._AppendInterfaceObject(c)
  32. c.Append()
  33. c.Sblock('%s.prototype = {' % self._interface)
  34. for function in self._namespace.functions.values():
  35. self._AppendFunction(c, function)
  36. c.TrimTrailingNewlines()
  37. c.Eblock('};')
  38. c.Append()
  39. for event in self._namespace.events.values():
  40. self._AppendEvent(c, event)
  41. c.TrimTrailingNewlines()
  42. return c
  43. def _GetHeader(self, tool, namespace):
  44. """Returns the file header text.
  45. """
  46. return (
  47. self._js_util.GetLicense() + '\n' + self._js_util.GetInfo(tool) + '\n' +
  48. ('/** @fileoverview Interface for %s that can be overriden. */' %
  49. namespace))
  50. def _AppendInterfaceObject(self, c):
  51. """Appends the code creating the interface object.
  52. For example:
  53. /** @interface */
  54. function SettingsPrivate() {}
  55. """
  56. (c.Append('/** @interface */')
  57. .Append('function %s() {}' % self._interface))
  58. def _AppendFunction(self, c, function):
  59. """Appends the inteface for a function, including a JSDoc comment.
  60. """
  61. if function.deprecated:
  62. return
  63. def getParamNames(f):
  64. names = []
  65. for param in f.params:
  66. names.append(param.name)
  67. # TODO(https://crbug.com/1142991) Update this to represent promises
  68. # better, rather than just appended as a callback param.
  69. if f.returns_async:
  70. names.append(f.returns_async.name)
  71. return names
  72. self._js_util.AppendFunctionJsDoc(c, self._namespace.name, function)
  73. c.Append('%s: function(%s) {},' % (function.name, ', '.join(
  74. getParamNames(function))))
  75. c.Append()
  76. def _AppendEvent(self, c, event):
  77. """Appends the interface for an event.
  78. """
  79. c.Sblock(line='/**', line_prefix=' * ')
  80. if (event.description):
  81. c.Comment(event.description, comment_prefix='')
  82. c.Append('@type {!ChromeEvent}')
  83. self._js_util.AppendSeeLink(c, self._namespace.name, 'event', event.name)
  84. c.Eblock(' */')
  85. c.Append('%s.prototype.%s;' % (self._interface, event.name))
  86. c.Append()