Show More
@@ -1,83 +1,85 | |||
|
1 | 1 | from os.path import abspath, dirname, join |
|
2 | 2 | |
|
3 | 3 | from IPython.terminal.interactiveshell import KeyBindingManager |
|
4 | 4 | |
|
5 | 5 | |
|
6 | 6 | def name(c): |
|
7 | 7 | s = c.__class__.__name__ |
|
8 | 8 | if s == '_Invert': |
|
9 | 9 | return '(Not: %s)' % name(c.filter) |
|
10 | 10 | if s in log_filters.keys(): |
|
11 | 11 | return '(%s: %s)' % (log_filters[s], ', '.join(name(x) for x in c.filters)) |
|
12 | 12 | return log_filters[s] if s in log_filters.keys() else s |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | def sentencize(s): |
|
16 | 16 | """Extract first sentence |
|
17 | 17 | """ |
|
18 | 18 | s = s.replace('\n', ' ').strip().split('.') |
|
19 | 19 | s = s[0] if len(s) else s |
|
20 | 20 | try: |
|
21 | 21 | return " ".join(s.split()) |
|
22 | 22 | except AttributeError: |
|
23 | 23 | return s |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | def most_common(lst, n=3): |
|
27 | 27 | """Most common elements occurring more then `n` times |
|
28 | 28 | """ |
|
29 | 29 | from collections import Counter |
|
30 | 30 | |
|
31 | 31 | c = Counter(lst) |
|
32 | 32 | return [k for (k, v) in c.items() if k and v > n] |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | def multi_filter_str(flt): |
|
36 | 36 | """Yield readable conditional filter |
|
37 | 37 | """ |
|
38 | 38 | assert hasattr(flt, 'filters'), 'Conditional filter required' |
|
39 | 39 | yield name(flt) |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | log_filters = dict(_AndList='And', _OrList='Or') |
|
43 | 43 | log_invert = {'_Invert'} |
|
44 | 44 | |
|
45 | 45 | kbm = KeyBindingManager.for_prompt() |
|
46 | 46 | ipy_bindings = kbm.registry.key_bindings |
|
47 | 47 | |
|
48 | 48 | dummy_docs = [] # ignore bindings without proper documentation |
|
49 | 49 | |
|
50 | 50 | common_docs = most_common([kb.handler.__doc__ for kb in ipy_bindings]) |
|
51 | 51 | if common_docs: |
|
52 | 52 | dummy_docs.extend(common_docs) |
|
53 | 53 | |
|
54 | 54 | dummy_docs = list(set(dummy_docs)) |
|
55 | 55 | |
|
56 | 56 | single_filter = dict() |
|
57 | 57 | multi_filter = dict() |
|
58 | 58 | for kb in ipy_bindings: |
|
59 | 59 | doc = kb.handler.__doc__ |
|
60 | 60 | if not doc or doc in dummy_docs: |
|
61 | 61 | continue |
|
62 | 62 | |
|
63 | 63 | shortcut = ' '.join([k if isinstance(k, str) else k.name for k in kb.keys]) |
|
64 | 64 | shortcut += shortcut.endswith('\\') and '\\' or '' |
|
65 | 65 | if hasattr(kb.filter, 'filters'): |
|
66 | 66 | flt = ' '.join(multi_filter_str(kb.filter)) |
|
67 | 67 | multi_filter[(shortcut, flt)] = sentencize(doc) |
|
68 | 68 | else: |
|
69 | 69 | single_filter[(shortcut, name(kb.filter))] = sentencize(doc) |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | if __name__ == '__main__': |
|
73 | 73 | |
|
74 | sort_key = lambda k:(str(k[0][1]),str(k[0][0])) | |
|
75 | ||
|
74 | 76 | here = abspath(dirname(__file__)) |
|
75 | 77 | dest = join(here, 'source', 'config', 'shortcuts') |
|
76 | 78 | |
|
77 | 79 | with open(join(dest, 'single_filtered.csv'), 'w') as csv: |
|
78 | for k, v in sorted(single_filter.items()): | |
|
80 | for k, v in sorted(single_filter.items(), key=sort_key): | |
|
79 | 81 | csv.write(':kbd:`{}`\t{}\t{}\n'.format(k[0], k[1], v)) |
|
80 | 82 | |
|
81 | 83 | with open(join(dest, 'multi_filtered.csv'), 'w') as csv: |
|
82 | for k, v in sorted(multi_filter.items()): | |
|
84 | for k, v in sorted(multi_filter.items(), key=sort_key): | |
|
83 | 85 | 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