make_ari_stubs.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # Asterisk -- An open source telephony toolkit.
  3. #
  4. # Copyright (C) 2013, Digium, Inc.
  5. #
  6. # David M. Lee, II <dlee@digium.com>
  7. #
  8. # See http://www.asterisk.org for more information about
  9. # the Asterisk project. Please do not directly contact
  10. # any of the maintainers of this project for assistance;
  11. # the project provides a web site, mailing lists and IRC
  12. # channels for your use.
  13. #
  14. # This program is free software, distributed under the terms of
  15. # the GNU General Public License Version 2. See the LICENSE file
  16. # at the top of the source tree.
  17. #
  18. from __future__ import print_function
  19. import sys
  20. try:
  21. import pystache
  22. except ImportError:
  23. print("Pystache required. Please sudo pip install pystache.", file=sys.stderr)
  24. sys.exit(1)
  25. import os.path
  26. from asterisk_processor import AsteriskProcessor
  27. from optparse import OptionParser
  28. from swagger_model import ResourceListing
  29. from transform import Transform
  30. TOPDIR = os.path.dirname(os.path.abspath(__file__))
  31. def rel(file):
  32. """Helper to get a file relative to the script's directory
  33. @parm file: Relative file path.
  34. """
  35. return os.path.join(TOPDIR, file)
  36. WIKI_PREFIX = 'Asterisk 13'
  37. API_TRANSFORMS = [
  38. Transform(rel('api.wiki.mustache'),
  39. 'doc/rest-api/%s {{name_title}} REST API.wiki' % WIKI_PREFIX),
  40. Transform(rel('res_ari_resource.c.mustache'),
  41. 'res/res_ari_{{c_name}}.c'),
  42. Transform(rel('ari_resource.h.mustache'),
  43. 'res/ari/resource_{{c_name}}.h'),
  44. Transform(rel('ari_resource.c.mustache'),
  45. 'res/ari/resource_{{c_name}}.c', overwrite=False),
  46. ]
  47. RESOURCES_TRANSFORMS = [
  48. Transform(rel('models.wiki.mustache'),
  49. 'doc/rest-api/%s REST Data Models.wiki' % WIKI_PREFIX),
  50. Transform(rel('ari.make.mustache'), 'res/ari.make'),
  51. Transform(rel('ari_model_validators.h.mustache'),
  52. 'res/ari/ari_model_validators.h'),
  53. Transform(rel('ari_model_validators.c.mustache'),
  54. 'res/ari/ari_model_validators.c'),
  55. ]
  56. def main(argv):
  57. parser = OptionParser(usage="Usage %prog [resources.json] [destdir]")
  58. (options, args) = parser.parse_args(argv)
  59. if len(args) != 3:
  60. parser.error("Wrong number of arguments")
  61. source = args[1]
  62. dest_dir = args[2]
  63. renderer = pystache.Renderer(search_dirs=[TOPDIR], missing_tags='strict')
  64. processor = AsteriskProcessor(wiki_prefix=WIKI_PREFIX)
  65. # Build the models
  66. base_dir = os.path.dirname(source)
  67. resources = ResourceListing().load_file(source, processor)
  68. for api in resources.apis:
  69. api.load_api_declaration(base_dir, processor)
  70. # Render the templates
  71. for api in resources.apis:
  72. for transform in API_TRANSFORMS:
  73. transform.render(renderer, api, dest_dir)
  74. for transform in RESOURCES_TRANSFORMS:
  75. transform.render(renderer, resources, dest_dir)
  76. if __name__ == "__main__":
  77. sys.exit(main(sys.argv) or 0)