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