19
19
import os
20
20
import inspect
21
21
import random
22
- import re
23
- import sys
24
22
import types
25
23
26
24
# import the local copy of matplotlib, not the installed one
27
25
#sys.path.insert(0, './lib')
28
26
from matplotlib .axes import Axes
29
- from matplotlib .cbook import dedent
30
27
31
28
32
29
# this is the magic line that must exist in pyplot, after which the boilerplate content will be
37
34
'matplotlib' , 'pyplot.py' )
38
35
39
36
37
+ AUTOGEN_MSG = """
38
+ # This function was autogenerated by boilerplate.py. Do not edit as
39
+ # changes will be lost"""
40
40
41
- def boilerplate_gen ():
42
- """Generator of lines for the automated part of pyplot."""
43
41
44
- _fmtplot = """
45
- # This function was autogenerated by boilerplate.py. Do not edit as
46
- # changes will be lost
42
+ PLOT_TEMPLATE = AUTOGEN_MSG + """
47
43
@autogen_docstring(Axes.%(func)s)
48
44
def %(func)s(%(argspec)s):
49
45
%(ax)s = gca()
@@ -59,17 +55,38 @@ def %(func)s(%(argspec)s):
59
55
%(ax)s.hold(%(washold)s)
60
56
%(mappable)s
61
57
return %(ret)s
62
- """
63
-
64
- _fmtmisc = """
65
- # This function was autogenerated by boilerplate.py. Do not edit as
66
- # changes will be lost
58
+ """
59
+
60
+
61
+ # Used for misc functions such as cla/legend etc.
62
+ MISC_FN_TEMPLATE = AUTOGEN_MSG + """
67
63
@docstring.copy_dedent(Axes.%(func)s)
68
64
def %(func)s(%(argspec)s):
69
65
%(ret)s = gca().%(func)s(%(call)s)
70
66
draw_if_interactive()
71
67
return %(ret)s
72
- """
68
+ """
69
+
70
+
71
+ # Used for colormap functions
72
+ CMAP_TEMPLATE = AUTOGEN_MSG + """
73
+ def {name}():
74
+ '''
75
+ set the default colormap to {name} and apply to current image if any.
76
+ See help(colormaps) for more information
77
+ '''
78
+ rc('image', cmap='{name}')
79
+ im = gci()
80
+
81
+ if im is not None:
82
+ im.set_cmap(cm.{name})
83
+ draw_if_interactive()
84
+
85
+ """
86
+
87
+
88
+ def boilerplate_gen ():
89
+ """Generator of lines for the automated part of pyplot."""
73
90
74
91
# these methods are all simple wrappers of Axes methods by the same
75
92
# name.
@@ -168,17 +185,12 @@ def format_value(value):
168
185
return '=mlab.' + value .func_name
169
186
if value .func_name == 'mean' :
170
187
return '=np.' + value .func_name
171
- raise ValueError , ( 'default value %s unknown to boilerplate.formatvalue'
172
- % value )
188
+ raise ValueError (( 'default value %s unknown to boilerplate.' + \
189
+ 'formatvalue' ) % value )
173
190
return '=' + repr (value )
174
191
175
- def remove_final_whitespace (string ):
176
- """
177
- Return a copy of *string* with final whitespace removed from each line.
178
- """
179
- return '\n ' .join (x .rstrip () for x in string .split ('\n ' ))
180
-
181
- for fmt ,cmdlist in (_fmtplot ,_plotcommands ),(_fmtmisc ,_misccommands ):
192
+ for fmt , cmdlist in [(PLOT_TEMPLATE , _plotcommands ),
193
+ (MISC_FN_TEMPLATE , _misccommands )]:
182
194
for func in cmdlist :
183
195
# For some commands, an additional line is needed to set the
184
196
# color map
@@ -201,13 +213,13 @@ def remove_final_whitespace(string):
201
213
call .append ('**' + varkw )
202
214
call = ', ' .join (call )
203
215
204
- # Add a hold keyword argument if needed (fmt is _fmtplot ) and
216
+ # Add a hold keyword argument if needed (fmt is PLOT_TEMPLATE ) and
205
217
# possible (if *args is used, we can't just add a hold
206
218
# argument in front of it since it would gobble one of the
207
219
# arguments the user means to pass via *args)
208
220
if varargs :
209
221
sethold = "hold = %(varkw)s.pop('hold', None)" % locals ()
210
- elif fmt is _fmtplot :
222
+ elif fmt is PLOT_TEMPLATE :
211
223
args .append ('hold' )
212
224
defaults = defaults + (None ,)
213
225
sethold = ''
@@ -219,7 +231,7 @@ def remove_final_whitespace(string):
219
231
220
232
# A gensym-like facility in case some function takes an
221
233
# argument named washold, ax, or ret
222
- washold ,ret ,ax = 'washold' , 'ret' , 'ax'
234
+ washold , ret , ax = 'washold' , 'ret' , 'ax'
223
235
bad = set (args ) | set ((varargs , varkw ))
224
236
while washold in bad or ret in bad or ax in bad :
225
237
washold = 'washold' + str (random .randrange (10 ** 12 ))
@@ -230,29 +242,11 @@ def remove_final_whitespace(string):
230
242
# bail out if they are used as argument names
231
243
for reserved in ('gca' , 'gci' , 'draw_if_interactive' ):
232
244
if reserved in bad :
233
- raise ValueError , \
234
- 'Axes method %s has kwarg named %s' % (func , reserved )
235
-
236
- yield remove_final_whitespace (fmt % locals ())
237
-
238
- # define the colormap functions
239
- _fmtcmap = """
240
- # This function was autogenerated by boilerplate.py. Do not edit as
241
- # changes will be lost
242
- def %(name)s():
243
- '''
244
- set the default colormap to %(name)s and apply to current image if any.
245
- See help(colormaps) for more information
246
- '''
247
- rc('image', cmap='%(name)s')
248
- im = gci()
249
-
250
- if im is not None:
251
- im.set_cmap(cm.%(name)s)
252
- draw_if_interactive()
253
-
254
- """
245
+ msg = 'Axes method %s has kwarg named %s' % (func , reserved )
246
+ raise ValueError (msg )
255
247
248
+ yield fmt % locals ()
249
+
256
250
cmaps = (
257
251
'autumn' ,
258
252
'bone' ,
@@ -272,22 +266,22 @@ def %(name)s():
272
266
)
273
267
# add all the colormaps (autumn, hsv, ....)
274
268
for name in cmaps :
275
- yield _fmtcmap % locals ( )
269
+ yield CMAP_TEMPLATE . format ( name = name )
276
270
277
271
278
272
def build_pyplot ():
279
273
pyplot_path = os .path .join (os .path .dirname (__file__ ), 'lib' ,
280
274
'matplotlib' , 'pyplot.py' )
281
275
282
- pyplot_orig = file (pyplot_path , 'r' ).readlines ()
276
+ pyplot_orig = open (pyplot_path , 'r' ).readlines ()
283
277
284
278
285
279
try :
286
280
pyplot_orig = pyplot_orig [:pyplot_orig .index (PYPLOT_MAGIC_HEADER )+ 1 ]
287
281
except IndexError :
288
282
raise ValueError ('The pyplot.py file *must* have the exact line: %s' % PYPLOT_MAGIC_HEADER )
289
283
290
- pyplot = file (pyplot_path , 'w' )
284
+ pyplot = open (pyplot_path , 'w' )
291
285
pyplot .writelines (pyplot_orig )
292
286
pyplot .write ('\n ' )
293
287
0 commit comments