Debian Codegraph @

docs/update_behave_rst.py

raw
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 """
4 Generates documentation of behave's
5
6 * command-line options
7 * configuration-file parameters
8
9 REQUIRES: Python >= 2.6
10 """
11
12 from __future__ import absolute_import, print_function
13 import re
14 import sys
15 import conf
16 import textwrap
17 from behave import configuration
18 from behave.__main__ import TAG_HELP
19
20 cmdline = []
21 config = []
22 indent = " "
23 cmdline_option_schema = """\
24 .. option:: {cmdline_option}
25
26 {text}
27 """
28 config_param_schema = """\
29 .. index::
30 single: configuration param; {param}
31
32 .. describe:: {param} : {type}
33
34 {text}
35 """
36
37 # -- STEP: Collect information and preprocess it.
38 for fixed, keywords in configuration.options:
39 skip = False
40 if "dest" in keywords:
41 dest = keywords["dest"]
42 else:
43 for opt in fixed:
44 if opt.startswith("--no"):
45 option_case = False
46 skip = True
47 if opt.startswith("--"):
48 dest = opt[2:].replace("-", "_")
49 break
50 else:
51 assert len(opt) == 2
52 dest = opt[1:]
53
54 # -- CASE: command-line option
55 text = re.sub(r"\s+", " ", keywords["help"]).strip()
56 text = text.replace("%%", "%")
57 text = textwrap.fill(text, 70, initial_indent="", subsequent_indent=indent)
58 if fixed:
59 # -- COMMAND-LINE OPTIONS (CONFIGFILE only have empty fixed):
60 # cmdline.append(".. option:: %s\n\n%s\n" % (", ".join(fixed), text))
61 cmdline_option = ", ".join(fixed)
62 cmdline.append(cmdline_option_schema.format(
63 cmdline_option=cmdline_option, text=text))
64
65 if skip or dest in "tags_help lang_list lang_help version".split():
66 continue
67
68 # -- CASE: configuration-file parameter
69 action = keywords.get("action", "store")
70 if action == "store":
71 type = "text"
72 elif action in ("store_true","store_false"):
73 type = "bool"
74 elif action == "append":
75 type = "sequence<text>"
76 else:
77 raise ValueError("unknown action %s" % action)
78
79 if action == "store_false":
80 # -- AVOID: Duplicated descriptions, use only case:true.
81 continue
82
83 text = re.sub(r"\s+", " ", keywords.get("config_help", keywords["help"])).strip()
84 text = text.replace("%%", "%")
85 text = textwrap.fill(text, 70, initial_indent="", subsequent_indent=indent)
86 config.append(config_param_schema.format(param=dest, type=type, text=text))
87
88
89 # -- STEP: Generate documentation.
90 print("Writing behave.rst ...")
91 with open("behave.rst-template") as f:
92 template = f.read()
93
94 values = dict(
95 cmdline="\n".join(cmdline),
96 tag_expression=TAG_HELP,
97 config="\n".join(config),
98 )
99 with open("behave.rst", "w") as f:
100 f.write(template.format(**values))