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