forked from visionegg/visionegg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_docstrings.py
executable file
·256 lines (231 loc) · 8.74 KB
/
update_docstrings.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
import os
import sys
import md5
import re
import VisionEgg
import VisionEgg.Core
import VisionEgg.Daq
import VisionEgg.DaqKeyboard
import VisionEgg.Dots
import VisionEgg.FlowControl
import VisionEgg.Gratings
import VisionEgg.MoreStimuli
import VisionEgg.ResponseControl
import VisionEgg.SphereMap
import VisionEgg.Text
import VisionEgg.Textures
mods = [VisionEgg,
VisionEgg.Core,
VisionEgg.Daq,
VisionEgg.DaqKeyboard,
VisionEgg.Dots,
VisionEgg.FlowControl,
VisionEgg.Gratings,
VisionEgg.MoreStimuli,
VisionEgg.ResponseControl,
VisionEgg.SphereMap,
VisionEgg.Text,
VisionEgg.Textures,
]
mod_name_to_file = {}
file_buffers = {}
file_hashes = {}
for mod in mods:
orig_file = mod.__file__
if orig_file.endswith('pyc'):
orig_file = orig_file[:-1]
cur_file = os.path.join('VisionEgg',os.path.split(orig_file)[1])
mod_name_to_file[mod.__name__] = cur_file
def get_str(xx,const=0):
class_by_key = {}
done_parameters_and_defaults = []
classes = VisionEgg.recursive_base_class_finder( xx )
# Fill self.parameters with parameter names and set to default values
for klass in classes:
if klass == object:
continue # base class of new style classes - ignore
if const:
pd = klass.constant_parameters_and_defaults
else:
pd = klass.parameters_and_defaults
if pd not in done_parameters_and_defaults:
for parameter_name in pd.keys():
class_by_key[parameter_name] = klass
done_parameters_and_defaults.append(pd)
ks = class_by_key.keys()
ks.sort()
if len(ks):
out_strs = []
if not const:
out_strs.append('Parameters\n')
out_strs.append('==========\n')
else:
out_strs.append('Constant Parameters\n')
out_strs.append('===================\n')
# pass 1:
max_len = 0
for param_name in ks:
max_len = max(max_len, len(param_name))
for param_name in ks:
klass = class_by_key[param_name]
if const:
p = klass.constant_parameters_and_defaults
else:
p = klass.parameters_and_defaults
if len(p[param_name]) > 3:
if p[param_name][3] == VisionEgg.ParameterDefinition.DEPRECATED:
continue
type_str = param_name
default = p[param_name][0]
ve_type = p[param_name][1]
if len(p[param_name]) > 2:
description = p[param_name][2] + ' '
else:
description = ''
out_strs.append( '%s -- %s(%s)\n'%(param_name.ljust(max_len), description, str(ve_type)) )
if xx != klass:
if klass.__module__ != xx.__module__:
mod_name = '%s.'%klass.__module__
else:
mod_name = ''
out_strs.append( ' '*(max_len+4)+'Inherited from %s%s\n'%(mod_name,klass.__name__,))
tmp = str(default).split('\n')
if default is None:
tmp = ['(determined at runtime)']
if len(p[param_name]) > 3:
if p[param_name][3] == VisionEgg.ParameterDefinition.OPENGL_ENUM:
if default is None:
gl_name = '(GL enum determined at runtime)'
else:
gl_name = str(default)
tmp = [gl_name]
out_strs.append( ' '*(max_len+4)+'Default: '+tmp[0]+'\n')
if len(tmp) > 1:
for i in range(1,len(tmp)):
out_strs.append( ' '*(max_len+13)+tmp[i]+'\n')
return out_strs
xl = []
for mod in mods:
orig_file = mod.__file__
if orig_file.endswith('pyc'):
orig_file = orig_file[:-1]
orig_fd = file(orig_file,"r")
orig_hash = md5.new()
orig_hash.update( orig_fd.read() )
fname = mod_name_to_file[ mod.__name__ ]
cur_fd = file(fname,"r")
cur_hash = md5.new()
cur_hash.update( cur_fd.read() )
digest = cur_hash.digest()
if orig_hash.digest() != digest:
raise RuntimeError('%s is different in VisionEgg and site-packages'%fname)
for x in mod.__dict__.keys():
xx = getattr(mod,x)
xl.append(xx)
file_hashes[fname] = digest
for xx in xl:
found=0
if type(xx) == type(VisionEgg.ClassWithParameters):
if issubclass(xx,VisionEgg.ClassWithParameters):
if not xx.__module__ in mod_name_to_file.keys():
print xx,'not in modules -- skipping'
continue
fname = mod_name_to_file[xx.__module__]
if file_buffers.has_key(fname):
buf = file_buffers[fname]
else:
fd = file(fname,'r')
buf = fd.readlines()
fd.close()
del fd
search_str = re.compile( r'^class %s\W'%xx.__name__ )
print 'searching for %s in %s'%(xx.__name__,fname)
for line_no, line in enumerate(buf):
if search_str.match(line):
#print xx.__name__,fname,line_no+1,line
found=1
break
if not found:
if xx.__name__ in ['StrokeText','GlutTextBase','BitmapText']:
print 'skipping %s - not found, probably because no glut'%xx.__name__
continue
raise RuntimeError("didn't find source for %s"%xx.__name__)
doc_find = re.compile(r'"""')
doc_start = line_no+1
if not doc_find.search(buf[doc_start]):
print xx.__name__,fname,doc_start,": not doc string"
continue
doc_one_liner_find = re.compile(r'""".*"""')
if doc_one_liner_find.search(buf[doc_start]):
doc_stop = doc_start
del_doc_stop = doc_stop+1
doc_lines = buf[doc_start:del_doc_stop]
del buf[doc_start:del_doc_stop]
else:
doc_stop = doc_start+1
doc_found = 0
while not doc_found:
if doc_find.search(buf[doc_stop]):
doc_found=1
doc_stop += 1
del_doc_stop = doc_stop
doc_lines = buf[doc_start:del_doc_stop-1]
del buf[doc_start:del_doc_stop]
trimmed_doc_lines = []
for doc_line in doc_lines:
doc_line = doc_line.replace('"""','')
if doc_line.startswith(' '):
trimmed_doc_lines.append(doc_line[4:])
else:
trimmed_doc_lines.append(doc_line)
doc_lines = trimmed_doc_lines
# trim old parameter definitions from docstring
idx = len(doc_lines)-1
while idx >= 0:
if doc_lines[idx] == 'Constant Parameters\n':
doc_lines = doc_lines[:idx]
break
idx -= 1
idx = len(doc_lines)-1
while idx >= 0:
if doc_lines[idx] == 'Parameters\n':
doc_lines = doc_lines[:idx]
break
idx -= 1
# insert new parameter defintion
## params_lines = get_str(xx.parameters_and_defaults,const=0)
## const_lines = get_str(xx.constant_parameters_and_defaults,const=1)
params_lines = get_str(xx,const=0)
const_lines = get_str(xx,const=1)
new_lines = doc_lines#['"""'+''.join(doc_lines)]
new_lines[0] = '"""'+new_lines[0]
if params_lines is not None:
if new_lines[-1].strip() != '':
new_lines.append('\n')
new_lines.extend(params_lines)
if const_lines is not None:
if new_lines[-1].strip() != '':
new_lines.append('\n')
new_lines.extend(const_lines)
new_lines.append('"""\n')
final_new_lines = []
for new_line in new_lines:
tmp = new_line.strip()
if tmp == '':
final_new_lines.append('\n')
else:
final_new_lines.append( ' '+new_line )
new_lines = final_new_lines
buf[line_no+1:line_no+1] = new_lines # insert new docstring
file_buffers[fname] = buf # reassign new buffer
for fname, buf in file_buffers.iteritems():
buf = ''.join(buf)
new_hash = md5.new()
new_hash.update(buf)
if new_hash.digest() != file_hashes[fname]:
print 'saving',fname
fd = file(fname,'w')
fd.write(buf)
fd.close()
print 'done'