Asterisk - The Open Source Telephony Project GIT-master-3dae2cf
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 <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
19from __future__ import print_function
20import sys
21
22try:
23 import pystache
24except ImportError:
25 print("Pystache required. Please sudo pip install pystache.", file=sys.stderr)
26 sys.exit(1)
27
28import os.path
29
30from asterisk_processor import AsteriskProcessor
31from argparse import ArgumentParser as ArgParser
32from swagger_model import ResourceListing
33from transform import Transform
34
35TOPDIR = os.path.dirname(os.path.abspath(__file__))
36
37
38def 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
45def main(argv):
46 description = (
47 'Command line utility to export ARI documentation to markdown'
48 )
49
50 parser = ArgParser(description=description)
51 parser.add_argument('--resources', type=str, default="rest-api/resources.json",
52 help="resources.json file to process", required=False)
53 parser.add_argument('--source-dir', type=str, default=".",
54 help="Asterisk source directory", required=False)
55 parser.add_argument('--dest-dir', type=str, default="doc/rest-api",
56 help="Destination directory", required=False)
57 parser.add_argument('--docs-prefix', type=str, default="../",
58 help="Prefix to apply to links", required=False)
59
60 args = parser.parse_args()
61 if not args:
62 return
63
64 renderer = pystache.Renderer(search_dirs=[TOPDIR], missing_tags='strict')
65 processor = AsteriskProcessor(wiki_prefix=args.docs_prefix)
66
67 API_TRANSFORMS = [
68 Transform(rel('api.wiki.mustache'),
69 '%s/{{name_title}}_REST_API.md' % args.dest_dir),
70 Transform(rel('res_ari_resource.c.mustache'),
71 'res/res_ari_{{c_name}}.c'),
72 Transform(rel('ari_resource.h.mustache'),
73 'res/ari/resource_{{c_name}}.h'),
74 Transform(rel('ari_resource.c.mustache'),
75 'res/ari/resource_{{c_name}}.c', overwrite=False),
76 ]
77
78 RESOURCES_TRANSFORMS = [
79 Transform(rel('models.wiki.mustache'),
80 '%s/Asterisk_REST_Data_Models.md' % args.dest_dir),
81 Transform(rel('ari.make.mustache'), 'res/ari.make'),
82 Transform(rel('ari_model_validators.h.mustache'),
83 'res/ari/ari_model_validators.h'),
84 Transform(rel('ari_model_validators.c.mustache'),
85 'res/ari/ari_model_validators.c'),
86 ]
87
88 # Build the models
89 base_dir = os.path.dirname(args.resources)
90 resources = ResourceListing().load_file(args.resources, processor)
91 for api in resources.apis:
92 api.load_api_declaration(base_dir, processor)
93
94 # Render the templates
95 for api in resources.apis:
96 for transform in API_TRANSFORMS:
97 transform.render(renderer, api, args.source_dir)
98 for transform in RESOURCES_TRANSFORMS:
99 transform.render(renderer, resources, args.source_dir)
100
101if __name__ == "__main__":
102 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.