##// END OF EJS Templates
Improve grouping of filter
Matthias Bussonnier -
Show More
@@ -1,83 +1,83 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':
9 return '(Not: %s)' % name(c.filter)
10 if s in log_filters.keys():
11 return '(%s: %s)' % (log_filters[s], ', '.join(name(x) for x in c.filters))
8 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
9
13
10
14
11 def sentencize(s):
15 def sentencize(s):
12 """Extract first sentence
16 """Extract first sentence
13 """
17 """
14 s = s.replace('\n', ' ').strip().split('.')
18 s = s.replace('\n', ' ').strip().split('.')
15 s = s[0] if len(s) else s
19 s = s[0] if len(s) else s
16 try:
20 try:
17 return " ".join(s.split())
21 return " ".join(s.split())
18 except AttributeError:
22 except AttributeError:
19 return s
23 return s
20
24
21
25
22 def most_common(lst, n=3):
26 def most_common(lst, n=3):
23 """Most common elements occurring more then `n` times
27 """Most common elements occurring more then `n` times
24 """
28 """
25 from collections import Counter
29 from collections import Counter
26
30
27 c = Counter(lst)
31 c = Counter(lst)
28 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]
29
33
30
34
31 def multi_filter_str(flt):
35 def multi_filter_str(flt):
32 """Yield readable conditional filter
36 """Yield readable conditional filter
33 """
37 """
34 assert hasattr(flt, 'filters'), 'Conditional filter required'
38 assert hasattr(flt, 'filters'), 'Conditional filter required'
35
36 yield name(flt)
39 yield name(flt)
37 for subfilter in flt.filters:
38 yield name(subfilter)
39 if hasattr(subfilter, 'filter'):
40 yield name(subfilter.filter)
41
40
42
41
43 log_filters = dict(_AndList='(And)', _OrList='(Or)', _Invert='(Inv)')
42 log_filters = dict(_AndList='And', _OrList='Or')
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 here = abspath(dirname(__file__))
74 here = abspath(dirname(__file__))
75 dest = join(here, 'source', 'config', 'shortcuts')
75 dest = join(here, 'source', 'config', 'shortcuts')
76
76
77 with open(join(dest, 'single_filtered.csv'), 'w') as csv:
77 with open(join(dest, 'single_filtered.csv'), 'w') as csv:
78 for k, v in sorted(single_filter.items()):
78 for k, v in sorted(single_filter.items()):
79 csv.write(':kbd:`{}`\t{}\t{}\n'.format(k[0], k[1], v))
79 csv.write(':kbd:`{}`\t{}\t{}\n'.format(k[0], k[1], v))
80
80
81 with open(join(dest, 'multi_filtered.csv'), 'w') as csv:
81 with open(join(dest, 'multi_filtered.csv'), 'w') as csv:
82 for k, v in sorted(multi_filter.items()):
82 for k, v in sorted(multi_filter.items()):
83 csv.write(':kbd:`{}`\t{}\t{}\n'.format(k[0], k[1], v))
83 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