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