##// END OF EJS Templates
fix unpacking in docs/autogen_shortcuts.py
Snir Broshi -
Show More
@@ -1,84 +1,89 b''
1 1 from pathlib import Path
2 2
3 3 from IPython.terminal.shortcuts import create_ipython_shortcuts
4 4
5 5 def name(c):
6 6 s = c.__class__.__name__
7 7 if s == '_Invert':
8 8 return '(Not: %s)' % name(c.filter)
9 9 if s in log_filters.keys():
10 10 return '(%s: %s)' % (log_filters[s], ', '.join(name(x) for x in c.filters))
11 11 return log_filters[s] if s in log_filters.keys() else s
12 12
13 13
14 14 def sentencize(s):
15 15 """Extract first sentence
16 16 """
17 17 s = s.replace('\n', ' ').strip().split('.')
18 18 s = s[0] if len(s) else s
19 19 try:
20 20 return " ".join(s.split())
21 21 except AttributeError:
22 22 return s
23 23
24 24
25 25 def most_common(lst, n=3):
26 26 """Most common elements occurring more then `n` times
27 27 """
28 28 from collections import Counter
29 29
30 30 c = Counter(lst)
31 31 return [k for (k, v) in c.items() if k and v > n]
32 32
33 33
34 34 def multi_filter_str(flt):
35 35 """Yield readable conditional filter
36 36 """
37 37 assert hasattr(flt, 'filters'), 'Conditional filter required'
38 38 yield name(flt)
39 39
40 40
41 41 log_filters = {'_AndList': 'And', '_OrList': 'Or'}
42 42 log_invert = {'_Invert'}
43 43
44 44 class _DummyTerminal(object):
45 45 """Used as a buffer to get prompt_toolkit bindings
46 46 """
47 47 handle_return = None
48 48 input_transformer_manager = None
49 49 display_completions = None
50 50
51 51 ipy_bindings = create_ipython_shortcuts(_DummyTerminal()).bindings
52 52
53 53 dummy_docs = [] # ignore bindings without proper documentation
54 54
55 55 common_docs = most_common([kb.handler.__doc__ for kb in ipy_bindings])
56 56 if common_docs:
57 57 dummy_docs.extend(common_docs)
58 58
59 59 dummy_docs = list(set(dummy_docs))
60 60
61 61 single_filter = {}
62 62 multi_filter = {}
63 63 for kb in ipy_bindings:
64 64 doc = kb.handler.__doc__
65 65 if not doc or doc in dummy_docs:
66 66 continue
67 67
68 68 shortcut = ' '.join([k if isinstance(k, str) else k.name for k in kb.keys])
69 69 shortcut += shortcut.endswith('\\') and '\\' or ''
70 70 if hasattr(kb.filter, 'filters'):
71 71 flt = ' '.join(multi_filter_str(kb.filter))
72 72 multi_filter[(shortcut, flt)] = sentencize(doc)
73 73 else:
74 74 single_filter[(shortcut, name(kb.filter))] = sentencize(doc)
75 75
76 76
77 77 if __name__ == '__main__':
78 78 here = Path(__file__).parent
79 79 dest = here / 'source' / 'config' / 'shortcuts'
80 80
81 def sort_key(item):
82 k, v = item
83 shortcut, flt = k
84 return (str(shortcut), str(flt))
85
81 86 for filters, output_filename in [(single_filter, 'single_filtered'), (multi_filter, 'multi_filtered')]:
82 87 with (dest / '{}.csv'.format(output_filename)).open('w') as csv:
83 for (shortcut, flt), v in sorted(filters.items(), key=lambda ((shortcut, flt), v): (str(shortcut), str(flt))):
88 for (shortcut, flt), v in sorted(filters.items(), key=sort_key):
84 89 csv.write(':kbd:`{}`\t{}\t{}\n'.format(shortcut, flt, v))
General Comments 0
You need to be logged in to leave comments. Login now