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