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