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