Asterisk - The Open Source Telephony Project  18.5.0
make_ari_stubs.py
Go to the documentation of this file.
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 <[email protected]>
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 
19 from __future__ import print_function
20 import sys
21 
22 try:
23  import pystache
24 except ImportError:
25  print("Pystache required. Please sudo pip install pystache.", file=sys.stderr)
26  sys.exit(1)
27 
28 import os.path
29 
30 from asterisk_processor import AsteriskProcessor
31 from optparse import OptionParser
32 from swagger_model import ResourceListing
33 from transform import Transform
34 
35 TOPDIR = os.path.dirname(os.path.abspath(__file__))
36 
37 
38 def rel(file):
39  """Helper to get a file relative to the script's directory
40 
41  @parm file: Relative file path.
42  """
43  return os.path.join(TOPDIR, file)
44 
45 WIKI_PREFIX = 'Asterisk 18'
46 
47 API_TRANSFORMS = [
48  Transform(rel('api.wiki.mustache'),
49  'doc/rest-api/%s {{name_title}} REST API.wiki' % WIKI_PREFIX),
50  Transform(rel('res_ari_resource.c.mustache'),
51  'res/res_ari_{{c_name}}.c'),
52  Transform(rel('ari_resource.h.mustache'),
53  'res/ari/resource_{{c_name}}.h'),
54  Transform(rel('ari_resource.c.mustache'),
55  'res/ari/resource_{{c_name}}.c', overwrite=False),
56 ]
57 
58 RESOURCES_TRANSFORMS = [
59  Transform(rel('models.wiki.mustache'),
60  'doc/rest-api/%s REST Data Models.wiki' % WIKI_PREFIX),
61  Transform(rel('ari.make.mustache'), 'res/ari.make'),
62  Transform(rel('ari_model_validators.h.mustache'),
63  'res/ari/ari_model_validators.h'),
64  Transform(rel('ari_model_validators.c.mustache'),
65  'res/ari/ari_model_validators.c'),
66 ]
67 
68 
69 def main(argv):
70  parser = OptionParser(usage="Usage %prog [resources.json] [destdir]")
71 
72  (options, args) = parser.parse_args(argv)
73 
74  if len(args) != 3:
75  parser.error("Wrong number of arguments")
76 
77  source = args[1]
78  dest_dir = args[2]
79  renderer = pystache.Renderer(search_dirs=[TOPDIR], missing_tags='strict')
80  processor = AsteriskProcessor(wiki_prefix=WIKI_PREFIX)
81 
82  # Build the models
83  base_dir = os.path.dirname(source)
84  resources = ResourceListing().load_file(source, processor)
85  for api in resources.apis:
86  api.load_api_declaration(base_dir, processor)
87 
88  # Render the templates
89  for api in resources.apis:
90  for transform in API_TRANSFORMS:
91  transform.render(renderer, api, dest_dir)
92  for transform in RESOURCES_TRANSFORMS:
93  transform.render(renderer, resources, dest_dir)
94 
95 if __name__ == "__main__":
96  sys.exit(main(sys.argv) or 0)
def main(argv)
static int load_file(const char *filename, char **ret)
Read a TEXT file into a string and return the length.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)