| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import inspect |
| 4 |
from pathlib import Path |
| 5 |
from IPython.terminal.ipapp import TerminalIPythonApp |
| 6 |
from traitlets import Undefined |
| 7 |
from collections import defaultdict |
| 8 |
|
| 9 |
here = (Path(__file__)).parent |
| 10 |
options = here / "source" / "config" / "options" |
| 11 |
generated = options / "config-generated.txt" |
| 12 |
|
| 13 |
import textwrap |
| 14 |
indent = lambda text,n: textwrap.indent(text,n*' ') |
| 15 |
|
| 16 |
|
| 17 |
def interesting_default_value(dv): |
| 18 |
if (dv is None) or (dv is Undefined): |
| 19 |
return False |
| 20 |
if isinstance(dv, (str, list, tuple, dict, set)): |
| 21 |
return bool(dv) |
| 22 |
return True |
| 23 |
|
| 24 |
def format_aliases(aliases): |
| 25 |
fmted = [] |
| 26 |
for a in aliases: |
| 27 |
dashes = '-' if len(a) == 1 else '--' |
| 28 |
fmted.append('``%s%s``' % (dashes, a)) |
| 29 |
return ', '.join(fmted) |
| 30 |
|
| 31 |
def class_config_rst_doc(cls, trait_aliases): |
| 32 |
"""Generate rST documentation for this class' config options. |
| 33 |
|
| 34 |
Excludes traits defined on parent classes. |
| 35 |
""" |
| 36 |
lines = [] |
| 37 |
classname = cls.__name__ |
| 38 |
for k, trait in sorted(cls.class_traits(config=True).items()): |
| 39 |
ttype = trait.__class__.__name__ |
| 40 |
|
| 41 |
fullname = classname + '.' + trait.name |
| 42 |
lines += ['.. configtrait:: ' + fullname, |
| 43 |
'' |
| 44 |
] |
| 45 |
|
| 46 |
help = trait.help.rstrip() or 'No description' |
| 47 |
lines.append(indent(inspect.cleandoc(help), 4) + '\n') |
| 48 |
|
| 49 |
# Choices or type |
| 50 |
if 'Enum' in ttype: |
| 51 |
# include Enum choices |
| 52 |
lines.append(indent( |
| 53 |
':options: ' + ', '.join('``%r``' % x for x in trait.values), 4)) |
| 54 |
else: |
| 55 |
lines.append(indent(':trait type: ' + ttype, 4)) |
| 56 |
|
| 57 |
# Default value |
| 58 |
# Ignore boring default values like None, [] or '' |
| 59 |
if interesting_default_value(trait.default_value): |
| 60 |
try: |
| 61 |
dvr = trait.default_value_repr() |
| 62 |
except Exception: |
| 63 |
dvr = None # ignore defaults we can't construct |
| 64 |
if dvr is not None: |
| 65 |
if len(dvr) > 64: |
| 66 |
dvr = dvr[:61] + '...' |
| 67 |
# Double up backslashes, so they get to the rendered docs |
| 68 |
dvr = dvr.replace('\\n', '\\\\n') |
| 69 |
lines.append(indent(':default: ``%s``' % dvr, 4)) |
| 70 |
|
| 71 |
# Command line aliases |
| 72 |
if trait_aliases[fullname]: |
| 73 |
fmt_aliases = format_aliases(trait_aliases[fullname]) |
| 74 |
lines.append(indent(':CLI option: ' + fmt_aliases, 4)) |
| 75 |
|
| 76 |
# Blank line |
| 77 |
lines.append('') |
| 78 |
|
| 79 |
return '\n'.join(lines) |
| 80 |
|
| 81 |
def reverse_aliases(app): |
| 82 |
"""Produce a mapping of trait names to lists of command line aliases. |
| 83 |
""" |
| 84 |
res = defaultdict(list) |
| 85 |
for alias, trait in app.aliases.items(): |
| 86 |
res[trait].append(alias) |
| 87 |
|
| 88 |
# Flags also often act as aliases for a boolean trait. |
| 89 |
# Treat flags which set one trait to True as aliases. |
| 90 |
for flag, (cfg, _) in app.flags.items(): |
| 91 |
if len(cfg) == 1: |
| 92 |
classname = list(cfg)[0] |
| 93 |
cls_cfg = cfg[classname] |
| 94 |
if len(cls_cfg) == 1: |
| 95 |
traitname = list(cls_cfg)[0] |
| 96 |
if cls_cfg[traitname] is True: |
| 97 |
res[classname+'.'+traitname].append(flag) |
| 98 |
|
| 99 |
return res |
| 100 |
|
| 101 |
def write_doc(name, title, app, preamble=None): |
| 102 |
trait_aliases = reverse_aliases(app) |
| 103 |
filename = options / (name + ".rst") |
| 104 |
with open(filename, "w", encoding="utf-8") as f: |
| 105 |
f.write("\n") |
| 106 |
if preamble is not None: |
| 107 |
f.write(preamble + '\n\n') |
| 108 |
|
| 109 |
for c in app._classes_inc_parents(): |
| 110 |
f.write(class_config_rst_doc(c, trait_aliases)) |
| 111 |
f.write('\n') |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == '__main__': |
| 115 |
# Touch this file for the make target |
| 116 |
Path(generated).write_text("", encoding="utf-8") |
| 117 |
|
| 118 |
write_doc('terminal', 'Terminal IPython options', TerminalIPythonApp()) |