##// END OF EJS Templates
Include command line aliases in config docs
Thomas Kluyver -
Show More
@@ -5,6 +5,7 b' from os.path import join, dirname, abspath'
5 from IPython.terminal.ipapp import TerminalIPythonApp
5 from IPython.terminal.ipapp import TerminalIPythonApp
6 from ipykernel.kernelapp import IPKernelApp
6 from ipykernel.kernelapp import IPKernelApp
7 from traitlets import Undefined
7 from traitlets import Undefined
8 from collections import defaultdict
8
9
9 here = abspath(dirname(__file__))
10 here = abspath(dirname(__file__))
10 options = join(here, 'source', 'config', 'options')
11 options = join(here, 'source', 'config', 'options')
@@ -19,7 +20,14 b' def interesting_default_value(dv):'
19 return bool(dv)
20 return bool(dv)
20 return True
21 return True
21
22
22 def class_config_rst_doc(cls):
23 def format_aliases(aliases):
24 fmted = []
25 for a in aliases:
26 dashes = '-' if len(a) == 1 else '--'
27 fmted.append('``%s%s``' % (dashes, a))
28 return ', '.join(fmted)
29
30 def class_config_rst_doc(cls, trait_aliases):
23 """Generate rST documentation for this class' config options.
31 """Generate rST documentation for this class' config options.
24
32
25 Excludes traits defined on parent classes.
33 Excludes traits defined on parent classes.
@@ -29,7 +37,8 b' def class_config_rst_doc(cls):'
29 for k, trait in sorted(cls.class_traits(config=True).items()):
37 for k, trait in sorted(cls.class_traits(config=True).items()):
30 ttype = trait.__class__.__name__
38 ttype = trait.__class__.__name__
31
39
32 lines += ['.. configtrait:: ' + classname + '.' + trait.name,
40 fullname = classname + '.' + trait.name
41 lines += ['.. configtrait:: ' + fullname,
33 ''
42 ''
34 ]
43 ]
35
44
@@ -58,13 +67,38 b' def class_config_rst_doc(cls):'
58 dvr = dvr.replace('\\n', '\\\\n')
67 dvr = dvr.replace('\\n', '\\\\n')
59 lines.append(indent(':default: ``%s``' % dvr, 4))
68 lines.append(indent(':default: ``%s``' % dvr, 4))
60
69
70 # Command line aliases
71 if trait_aliases[fullname]:
72 fmt_aliases = format_aliases(trait_aliases[fullname])
73 lines.append(indent(':CLI option: ' + fmt_aliases, 4))
74
61 # Blank line
75 # Blank line
62 lines.append('')
76 lines.append('')
63
77
64 return '\n'.join(lines)
78 return '\n'.join(lines)
65
79
80 def reverse_aliases(app):
81 """Produce a mapping of trait names to lists of command line aliases.
82 """
83 res = defaultdict(list)
84 for alias, trait in app.aliases.items():
85 res[trait].append(alias)
86
87 # Flags also often act as aliases for a boolean trait.
88 # Treat flags which set one trait to True as aliases.
89 for flag, (cfg, _) in app.flags.items():
90 if len(cfg) == 1:
91 classname = list(cfg)[0]
92 cls_cfg = cfg[classname]
93 if len(cls_cfg) == 1:
94 traitname = list(cls_cfg)[0]
95 if cls_cfg[traitname] is True:
96 res[classname+'.'+traitname].append(flag)
97
98 return res
66
99
67 def write_doc(name, title, app, preamble=None):
100 def write_doc(name, title, app, preamble=None):
101 trait_aliases = reverse_aliases(app)
68 filename = join(options, name+'.rst')
102 filename = join(options, name+'.rst')
69 with open(filename, 'w') as f:
103 with open(filename, 'w') as f:
70 f.write(title + '\n')
104 f.write(title + '\n')
@@ -75,7 +109,7 b' def write_doc(name, title, app, preamble=None):'
75 #f.write(app.document_config_options())
109 #f.write(app.document_config_options())
76
110
77 for c in app._classes_inc_parents():
111 for c in app._classes_inc_parents():
78 f.write(class_config_rst_doc(c))
112 f.write(class_config_rst_doc(c, trait_aliases))
79 f.write('\n')
113 f.write('\n')
80
114
81
115
General Comments 0
You need to be logged in to leave comments. Login now