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