make_ari_stubs.py 2.8 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. import sys
  19. try:
  20. import pystache
  21. except ImportError:
  22. print >> sys.stderr, "Pystache required. Please sudo pip install pystache."
  23. sys.exit(1)
  24. import os.path
  25. from asterisk_processor import AsteriskProcessor
  26. from optparse import OptionParser
  27. from swagger_model import *
  28. from transform import Transform
  29. TOPDIR = os.path.dirname(os.path.abspath(__file__))
  30. def rel(file):
  31. """Helper to get a file relative to the script's directory
  32. @parm file: Relative file path.
  33. """
  34. return os.path.join(TOPDIR, file)
  35. WIKI_PREFIX = 'Asterisk 13'
  36. API_TRANSFORMS = [
  37. Transform(rel('api.wiki.mustache'),
  38. 'doc/rest-api/%s {{name_title}} REST API.wiki' % WIKI_PREFIX),
  39. Transform(rel('res_ari_resource.c.mustache'),
  40. 'res/res_ari_{{c_name}}.c'),
  41. Transform(rel('ari_resource.h.mustache'),
  42. 'res/ari/resource_{{c_name}}.h'),
  43. Transform(rel('ari_resource.c.mustache'),
  44. 'res/ari/resource_{{c_name}}.c', overwrite=False),
  45. ]
  46. RESOURCES_TRANSFORMS = [
  47. Transform(rel('models.wiki.mustache'),
  48. 'doc/rest-api/%s REST Data Models.wiki' % WIKI_PREFIX),
  49. Transform(rel('ari.make.mustache'), 'res/ari.make'),
  50. Transform(rel('ari_model_validators.h.mustache'),
  51. 'res/ari/ari_model_validators.h'),
  52. Transform(rel('ari_model_validators.c.mustache'),
  53. 'res/ari/ari_model_validators.c'),
  54. ]
  55. def main(argv):
  56. parser = OptionParser(usage="Usage %prog [resources.json] [destdir]")
  57. (options, args) = parser.parse_args(argv)
  58. if len(args) != 3:
  59. parser.error("Wrong number of arguments")
  60. source = args[1]
  61. dest_dir = args[2]
  62. renderer = pystache.Renderer(search_dirs=[TOPDIR], missing_tags='strict')
  63. processor = AsteriskProcessor(wiki_prefix=WIKI_PREFIX)
  64. # Build the models
  65. base_dir = os.path.dirname(source)
  66. resources = ResourceListing().load_file(source, processor)
  67. for api in resources.apis:
  68. api.load_api_declaration(base_dir, processor)
  69. # Render the templates
  70. for api in resources.apis:
  71. for transform in API_TRANSFORMS:
  72. transform.render(renderer, api, dest_dir)
  73. for transform in RESOURCES_TRANSFORMS:
  74. transform.render(renderer, resources, dest_dir)
  75. if __name__ == "__main__":
  76. sys.exit(main(sys.argv) or 0)