Show More
@@ -1,90 +1,84 b'' | |||||
1 |
from |
|
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(object): |
|
44 | class _DummyTerminal(object): | |
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 |
|
50 | |||
51 | ipy_bindings = create_ipython_shortcuts(_DummyTerminal()).bindings |
|
51 | ipy_bindings = create_ipython_shortcuts(_DummyTerminal()).bindings | |
52 |
|
52 | |||
53 | dummy_docs = [] # ignore bindings without proper documentation |
|
53 | dummy_docs = [] # ignore bindings without proper documentation | |
54 |
|
54 | |||
55 | common_docs = most_common([kb.handler.__doc__ for kb in ipy_bindings]) |
|
55 | common_docs = most_common([kb.handler.__doc__ for kb in ipy_bindings]) | |
56 | if common_docs: |
|
56 | if common_docs: | |
57 | dummy_docs.extend(common_docs) |
|
57 | dummy_docs.extend(common_docs) | |
58 |
|
58 | |||
59 | dummy_docs = list(set(dummy_docs)) |
|
59 | dummy_docs = list(set(dummy_docs)) | |
60 |
|
60 | |||
61 | single_filter = {} |
|
61 | single_filter = {} | |
62 | multi_filter = {} |
|
62 | multi_filter = {} | |
63 | for kb in ipy_bindings: |
|
63 | for kb in ipy_bindings: | |
64 | doc = kb.handler.__doc__ |
|
64 | doc = kb.handler.__doc__ | |
65 | if not doc or doc in dummy_docs: |
|
65 | if not doc or doc in dummy_docs: | |
66 | continue |
|
66 | continue | |
67 |
|
67 | |||
68 | shortcut = ' '.join([k if isinstance(k, str) else k.name for k in kb.keys]) |
|
68 | shortcut = ' '.join([k if isinstance(k, str) else k.name for k in kb.keys]) | |
69 | shortcut += shortcut.endswith('\\') and '\\' or '' |
|
69 | shortcut += shortcut.endswith('\\') and '\\' or '' | |
70 | if hasattr(kb.filter, 'filters'): |
|
70 | if hasattr(kb.filter, 'filters'): | |
71 | flt = ' '.join(multi_filter_str(kb.filter)) |
|
71 | flt = ' '.join(multi_filter_str(kb.filter)) | |
72 | multi_filter[(shortcut, flt)] = sentencize(doc) |
|
72 | multi_filter[(shortcut, flt)] = sentencize(doc) | |
73 | else: |
|
73 | else: | |
74 | single_filter[(shortcut, name(kb.filter))] = sentencize(doc) |
|
74 | single_filter[(shortcut, name(kb.filter))] = sentencize(doc) | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | if __name__ == '__main__': |
|
77 | if __name__ == '__main__': | |
|
78 | here = Path(__file__).parent | |||
|
79 | dest = here / 'source' / 'config' / 'shortcuts' | |||
78 |
|
80 | |||
79 | sort_key = lambda k:(str(k[0][1]),str(k[0][0])) |
|
81 | for filters, output_filename in [(single_filter, 'single_filtered'), (multi_filter, 'multi_filtered')]: | |
80 |
|
82 | with (dest / '{}.csv'.format(output_filename)).open('w') as csv: | ||
81 | here = abspath(dirname(__file__)) |
|
83 | for (shortcut, flt), v in sorted(filters.items(), key=lambda ((shortcut, flt), v): (str(shortcut), str(flt))): | |
82 | dest = join(here, 'source', 'config', 'shortcuts') |
|
84 | csv.write(':kbd:`{}`\t{}\t{}\n'.format(shortcut, flt, v)) | |
83 |
|
||||
84 | with open(join(dest, 'single_filtered.csv'), 'w') as csv: |
|
|||
85 | for k, v in sorted(single_filter.items(), key=sort_key): |
|
|||
86 | csv.write(':kbd:`{}`\t{}\t{}\n'.format(k[0], k[1], v)) |
|
|||
87 |
|
||||
88 | with open(join(dest, 'multi_filtered.csv'), 'w') as csv: |
|
|||
89 | for k, v in sorted(multi_filter.items(), key=sort_key): |
|
|||
90 | csv.write(':kbd:`{}`\t{}\t{}\n'.format(k[0], k[1], v)) |
|
General Comments 0
You need to be logged in to leave comments.
Login now