Add dcae-cli and component-json-schemas projects
[dcaegen2/platform/cli.git] / dcae-cli / docs / source / dcaeclidoctools.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 import pkg_resources
22
23 from docutils.nodes import literal_block
24 from sphinx.domains import Domain
25 from sphinx.util.compat import Directive
26
27 import click
28
29 from dcae_cli.cli import cli as group  # PYTHONPATH dynamically altered in conf.py
30
31
32 def generate_help_texts(command, prefix):
33     ctx = click.Context(command)
34     yield make_block(
35         ' '.join(prefix),
36         command.get_help_option(ctx).opts[0],
37         command.get_help(ctx),
38     )
39
40     if isinstance(command, click.core.Group):
41         for c in command.list_commands(ctx):
42             c = command.resolve_command(ctx, [c])[1]
43             prefix.append(c.name)
44             for h in generate_help_texts(c, prefix):
45                 yield h
46             prefix.pop()
47
48
49 def find_script_callable(name):
50     return list(pkg_resources.iter_entry_points(
51         'console_scripts', name))[0].load()
52
53
54 def make_block(command, opt, content):
55     h = "$ {} {}\n".format(command, opt) + content
56     return literal_block(h, h, language='bash')
57
58
59 class ClickHelpDirective(Directive):
60     has_content = True
61     required_arguments = 1
62
63     def run(self):
64         root_cmd = self.arguments[0]
65         #group = find_script_callable(root_cmd)
66         return list(generate_help_texts(group, [root_cmd]))
67
68
69 class DcaeCliDomain(Domain):
70     name = 'dcae_cli'
71     label = 'DCAE-CLI'
72     directives = {
73         'click-help': ClickHelpDirective,
74     }
75
76
77 def setup(app):
78     app.add_domain(DcaeCliDomain)