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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
"""
Generate configuration details docpage
"""
from __future__ import unicode_literals
import argparse
import io
import os
import pprint
import re
import sys
import tempfile
from cmakelang import configuration
from cmakelang import config_util
def parse_sourcefile(infile):
ruler = re.compile(r"[=\-\^~]+")
lines = infile.read().split("\n")
out = {}
stack = [out]
sepmap = {
"=": 0,
"-": 1,
"^": 2,
"~": 3
}
varname = None
buf = []
lineiter = enumerate(lines)
for idx, line in lineiter:
if ruler.match(line):
if lines[idx + 2] == line:
# Section heading
if varname:
stack[-1][varname] = "\n".join(buf)
varname = None
buf = []
title = lines[idx + 1]
newdict = {}
parent_idx = sepmap[line[0]]
stack[parent_idx][title] = newdict
stack = stack[:parent_idx + 1]
stack.append(newdict)
next(lineiter)
next(lineiter)
elif line and ruler.match(lines[idx + 1]):
# Variable heading
if varname:
stack[-1][varname] = "\n".join(buf[:-1])
varname = None
buf = []
varname = line
next(lineiter)
else:
buf.append(line)
if varname:
stack[-1][varname] = "\n".join(buf[:-1])
buf = []
varname = None
return out
SEPARATORS = ["=", "-", "^", "~"]
def get_command_line(descr):
argparser = argparse.ArgumentParser(usage="")
descr.add_to_argparse(argparser)
sys.stdout.flush()
stdout_copy = os.dup(sys.stdout.fileno())
with tempfile.NamedTemporaryFile(delete=False) as outfile:
tmppath = outfile.name
os.dup2(outfile.fileno(), sys.stdout.fileno())
try:
argparser.parse_args(["--help"])
except SystemExit:
pass
sys.stdout.flush()
os.dup2(stdout_copy, sys.stdout.fileno())
with io.open(tmppath, "r", encoding="utf-8") as infile:
content = infile.read()
os.unlink(tmppath)
return "\n".join(content.split("\n")[4:])
ROOT_CONFIG = None
def get_config_example(root, obj, descr):
default_value = descr.__get__(obj, None)
descr.__set__(obj, default_value)
buf = io.StringIO()
root.dump(buf, with_defaults=False)
descr.unset(obj)
return buf.getvalue()
def write_outfile(outfile, data, obj, name=None, depth=0, root=None):
if root is None:
root = obj
helptext = obj.__class__.__doc__.strip()
title, helptext = (helptext + "\n").split("\n", 1)
if not title and name:
title = name
outfile.write(SEPARATORS[depth] * len(title))
outfile.write("\n")
outfile.write(title)
outfile.write("\n")
outfile.write(SEPARATORS[depth] * len(title))
outfile.write("\n\n")
if helptext.strip():
outfile.write("\n")
outfile.write(helptext)
ppr = pprint.PrettyPrinter(indent=2, width=72)
for descr in obj._field_registry: # pylint: disable=protected-access
if isinstance(descr, config_util.SubtreeDescriptor):
subobj = descr.__get__(obj, type(obj))
write_outfile(
outfile, data.get(descr.name, {}), subobj, descr.name, depth + 1,
root)
elif isinstance(descr, config_util.FieldDescriptor):
outfile.write(".. _{}:\n\n".format(descr.name))
outfile.write(descr.name)
outfile.write("\n")
outfile.write("=" * len(descr.name))
outfile.write("\n\n")
if descr.helptext:
outfile.write(descr.helptext.strip())
outfile.write("\n\n")
heading = "default value:"
outfile.write("{}\n{}\n\n".format(heading, "-" * len(heading)))
outfile.write(".. code::\n\n")
for line in ppr.pformat(descr.default_value).split("\n"):
outfile.write(" {}\n".format(line))
outfile.write("\n")
if descr.name in data:
heading = "detailed description:"
outfile.write("{}\n{}\n\n".format(heading, "-" * len(heading)))
outfile.write(data[descr.name].strip())
outfile.write("\n\n")
cmdline = get_command_line(descr)
if cmdline:
heading = "command-line option:"
outfile.write("{}\n{}\n\n".format(heading, "-" * len(heading)))
outfile.write(".. code:: \n\n")
for line in cmdline.split("\n"):
outfile.write(" {}\n".format(line))
outfile.write("\n")
config = get_config_example(root, obj, descr)
if config:
heading = "config-file entry:"
outfile.write("{}\n{}\n\n".format(heading, "-" * len(heading)))
outfile.write(".. code:: \n\n")
for line in config.split("\n"):
outfile.write(" {}\n".format(line))
outfile.write("\n")
else:
raise RuntimeError("Field registry contains unknown descriptor")
def main():
rootdir = os.sep.join(os.path.realpath(__file__).split(os.sep)[:-3])
argparser = argparse.ArgumentParser(description=__doc__)
argparser.add_argument(
"--sourcefile",
default=os.path.join(rootdir, "cmakelang/doc/configopts.rst"))
argparser.add_argument(
"outfile", nargs="?", default="-")
args = argparser.parse_args()
with io.open(args.sourcefile, "r", encoding="utf-8") as infile:
data = parse_sourcefile(infile)
if args.outfile == "-":
args.outfile = os.dup(sys.stdout.fileno())
with io.open(args.outfile, "w", encoding="utf-8") as outfile:
outfile.write(".. _configopts:\n\n")
write_outfile(
outfile, data["global"], configuration.Configuration(), "global")
if __name__ == "__main__":
main()
|