forked from damontallen/IPython-quick-ref-sheets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild_dict.py
More file actions
151 lines (142 loc) · 6.81 KB
/
Build_dict.py
File metadata and controls
151 lines (142 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from Parse_flags import *
from collections import OrderedDict as Or_dict
def add_subject(_type_, Quick_ref_dic, lines=[], index=0, subject=''):
"""This function adds a new ordered dict to the Quick_ref_dic with the
line that the index points to (or subject) as the key to the ordered dict.
This key is returned as the current subject. The ordered dict is
initialized with "_type_" being set to the passed in _type_."""
if not subject:
subject = lines[index]
current_dic = Or_dict()
current_dic['_type_'] = _type_
Quick_ref_dic[subject] = current_dic
return subject
def strip_leading_empties(test_list):
if test_list[0].strip()=='':#is the righthand column empty
#print("Empty right!!!")
#print(_right)
#shift the explanations to the top of the example
for index_b, b in enumerate(test_list):
#print(b, index_b)
if b.strip()!='':
#index_b +=1
break
test_list = test_list[index_b:]
return test_list
def add_multiline(current_dict,_multiline_flag, line='', left=[], right=[]):
"""This function starts or adds to an array of multiline text in the
current_dict. The particular arrar is designated by the _multiline_flag."""
if current_dict['_type_']=='Comment':
if _multiline_flag not in current_dict:
current_dict[_multiline_flag] = []
current_dict[_multiline_flag].append(line)
else:
current_dict[_multiline_flag] = list(zip(left, right))
def build_dict(lines, break_at=-1):
"""This function takes an array of strings generated by quickref and turns
it into an ordered dict"""
Quick_ref_dic = Or_dict()
start_up = Or_dict(
[('ipython', 'Open IPython terminal console'),
('juypter console', 'Open terminal console (4.0 and later)'),
('ipython qtconsole', 'Open IPython qtconsole'),
('juptyer qtconsole', 'Open qtconsole (4.0 and later)'),
('ipython notebook', 'Open IPython Notebook (browser interface)'),
('jupyter notebook', 'Open Notebook (browser interface, 4.0 and later)'),
('ipython --help-all', 'Show all IPython start flags')])
in_comment = False
multiline = {'In multiline':False,'column':'Left','Entry':''}
lines.append(None)
for index, line in enumerate(lines):
if index==break_at:
break
if line == None:
break
elif not(line) or '=======' in line: #skip empty lines
in_comment = False
continue
elif title_check(lines, index):
"""Check to see if the current line is the title"""
subject = add_subject('Title', Quick_ref_dic, lines, index)
current_dict = Quick_ref_dic[subject]
for L_entry, R_entry in start_up.items():#Add start_up to the quickref
current_dict[L_entry]=R_entry
subject = add_subject("Heading", Quick_ref_dic, subject='Within IPython:')
#add_multiline(current_dict,_multiline_flag, left=left, right=right)
continue
elif heading_check(lines, index):# or comment_check(lines, index):
"""Check to see if the current line is a new heading"""
subject = add_subject('Heading', Quick_ref_dic, lines, index)
continue
elif comment_check(lines, index, in_comment):
"""Check to see if the current line is a comment instead of an
example"""
#print("Line = {}".format( line))
if not in_comment:
in_comment = True
#break_section = True
cur_com = '_Comment_starts_at_{}_'.format(str(index))
subject = add_subject('Comment', Quick_ref_dic, subject=cur_com)
comment = line
current_dict = Quick_ref_dic[subject]
add_multiline(current_dict,cur_com, line=comment)
continue
else:
try:
next_line = lines(index+1)
except:
next_line = ''
#print("comment? {}".format(in_comment))
#print("line is -- {}".format(line))
m_line_start = multiline_start_check(lines, index, in_comment)
#print(" m_line_start ={}".format( m_line_start))
end_multiline = not right_column_check(next_line)
end_multiline = end_multiline and multiline['column'] == 'Right'
parts = patch_range(line) #break the line up into parts
if m_line_start or multiline['In multiline']:
"""Check to see is the current line is the start of a multilined
example """
if m_line_start and not multiline['In multiline']:
multiline['In multiline']=True
multiline['column']='Left'
multiline['Entry'] = '_Multiline_Flag_{}_'.format(str(index))
_left = []
_right = []
rhc = right_column_check(line)
#print("line parts are: {}\n".format(parts))
if len(parts)==1:
if not rhc:
parts.append('')
else:
multiline['column']=='Right'
parts = [''] + parts
else:
multiline['column'] = 'Right'
_left.append(parts[0].strip())
_right.append(parts[1].strip())
if end_multiline:
#add the multiline explanation to Quick_ref_dic
_right = strip_leading_empties(_right)
_left.reverse()
_left = strip_leading_empties(_left)
_left.reverse()
len_diff = len(_left)-len(_right)
if len_diff>0: #make the two columns the same length
_right += [' ']*len_diff
elif len_diff<0:
_left += [' ']*(-len_diff)
current_dict = Quick_ref_dic[subject]
if len(_right)>1:
M_key = multiline['Entry']
add_multiline(current_dict, M_key, left=_left, right=_right)
else: #if it is not really a multiline entry....
Quick_ref_dic[subject][_left[0]]=_right[0]
#reset multiline values
multiline={'In multiline':False,'column':'Left','Entry':''}
elif (len(parts)>1 or not(line))and not(multiline['In multiline']): #was elif
"""This line contains atleast part of an example and maybe even part
of an explanation """
part_key = parts[0].strip() #######
Quick_ref_dic[subject][part_key]=parts[1] #######
#print("******\n")
return Quick_ref_dic