@@ -153,37 +153,70 @@ def out_of_date(original, derived):
153153 (os .path .exists (original ) and
154154 os .stat (derived ).st_mtime < os .stat (original ).st_mtime ))
155155
156- def import_file (plot_path , function_name ):
156+ def run_code (plot_path , function_name , plot_code ):
157157 """
158158 Import a Python module from a path, and run the function given by
159159 name, if function_name is not None.
160160 """
161161 # Change the working directory to the directory of the example, so
162162 # it can get at its data files, if any. Add its path to sys.path
163163 # so it can import any helper modules sitting beside it.
164- pwd = os .getcwd ()
165- path , fname = os .path .split (plot_path )
166- sys .path .insert (0 , os .path .abspath (path ))
167- stdout = sys .stdout
168- sys .stdout = cStringIO .StringIO ()
169- os .chdir (path )
170- try :
171- fd = open (fname )
172- module = imp .load_module (
173- "__main__" , fd , fname , ('py' , 'r' , imp .PY_SOURCE ))
174- finally :
175- del sys .path [0 ]
176- os .chdir (pwd )
177- sys .stdout = stdout
178- fd .close ()
179-
180- if function_name is not None :
181- print "function_name" , function_name
182- getattr (module , function_name )()
183-
184- return module
185-
186- def render_figures (plot_path , function_name , plot_code , outdir , formats ):
164+ if plot_code is not None :
165+ exec (plot_code )
166+ else :
167+ pwd = os .getcwd ()
168+ path , fname = os .path .split (plot_path )
169+ sys .path .insert (0 , os .path .abspath (path ))
170+ stdout = sys .stdout
171+ sys .stdout = cStringIO .StringIO ()
172+ os .chdir (path )
173+ try :
174+ fd = open (fname )
175+ module = imp .load_module (
176+ "__plot__" , fd , fname , ('py' , 'r' , imp .PY_SOURCE ))
177+ finally :
178+ del sys .path [0 ]
179+ os .chdir (pwd )
180+ sys .stdout = stdout
181+ fd .close ()
182+
183+ if function_name is not None :
184+ getattr (module , function_name )()
185+
186+ def run_savefig (plot_path , basename , tmpdir , destdir , formats ):
187+ """
188+ Once a plot script has been imported, this function runs savefig
189+ on all of the figures in all of the desired formats.
190+ """
191+ fig_managers = _pylab_helpers .Gcf .get_all_fig_managers ()
192+ for i , figman in enumerate (fig_managers ):
193+ for j , (format , dpi ) in enumerate (formats ):
194+ if len (fig_managers ) == 1 :
195+ outname = basename
196+ else :
197+ outname = "%s_%02d" % (basename , i )
198+ outname = outname + "." + format
199+ outpath = os .path .join (tmpdir , outname )
200+ try :
201+ figman .canvas .figure .savefig (outpath , dpi = dpi )
202+ except :
203+ s = cbook .exception_to_str ("Exception saving plot %s" % plot_path )
204+ warnings .warn (s )
205+ return 0
206+ if j > 0 :
207+ shutil .copyfile (outpath , os .path .join (destdir , outname ))
208+
209+ return len (fig_managers )
210+
211+ def clear_state ():
212+ plt .close ('all' )
213+ matplotlib .rcdefaults ()
214+ # Set a default figure size that doesn't overflow typical browser
215+ # windows. The script is free to override it if necessary.
216+ matplotlib .rcParams ['figure.figsize' ] = (5.5 , 4.5 )
217+
218+ def render_figures (plot_path , function_name , plot_code , tmpdir , destdir ,
219+ formats ):
187220 """
188221 Run a pyplot script and save the low and high res PNGs and a PDF
189222 in outdir.
@@ -196,7 +229,7 @@ def render_figures(plot_path, function_name, plot_code, outdir, formats):
196229
197230 # Look for single-figure output files first
198231 for format , dpi in formats :
199- outname = os .path .join (outdir , '%s.%s' % (basename , format ))
232+ outname = os .path .join (tmpdir , '%s.%s' % (basename , format ))
200233 if out_of_date (plot_path , outname ):
201234 all_exists = False
202235 break
@@ -211,7 +244,7 @@ def render_figures(plot_path, function_name, plot_code, outdir, formats):
211244 all_exists = True
212245 for format , dpi in formats :
213246 outname = os .path .join (
214- outdir , '%s_%02d.%s' % (basename , i , format ))
247+ tmpdir , '%s_%02d.%s' % (basename , i , format ))
215248 if out_of_date (plot_path , outname ):
216249 all_exists = False
217250 break
@@ -225,39 +258,20 @@ def render_figures(plot_path, function_name, plot_code, outdir, formats):
225258
226259 # We didn't find the files, so build them
227260
228- # Clear any existing figures
229- plt .close ('all' )
230- matplotlib .rcdefaults ()
231- # Set a default figure size that doesn't overflow typical browser
232- # windows. The script is free to override it if necessary.
233- matplotlib .rcParams ['figure.figsize' ] = (5.5 , 4.5 )
261+ clear_state ()
262+ try :
263+ run_code (plot_path , function_name , plot_code )
264+ except :
265+ s = cbook .exception_to_str ("Exception running plot %s" % plot_path )
266+ warnings .warn (s )
267+ return 0
234268
235- if plot_code is not None :
236- exec (plot_code )
237- else :
238- try :
239- import_file (plot_path , function_name )
240- except :
241- s = cbook .exception_to_str ("Exception running plot %s" % plot_path )
242- warnings .warn (s )
243- return 0
269+ num_figs = run_savefig (plot_path , basename , tmpdir , destdir , formats )
244270
245- fig_managers = _pylab_helpers .Gcf .get_all_fig_managers ()
246- for i , figman in enumerate (fig_managers ):
247- for format , dpi in formats :
248- if len (fig_managers ) == 1 :
249- outname = basename
250- else :
251- outname = "%s_%02d" % (basename , i )
252- outpath = os .path .join (outdir , '%s.%s' % (outname , format ))
253- try :
254- figman .canvas .figure .savefig (outpath , dpi = dpi )
255- except :
256- s = cbook .exception_to_str ("Exception running plot %s" % plot_path )
257- warnings .warn (s )
258- return 0
271+ if '__plot__' in sys .modules :
272+ del sys .modules ['__plot__' ]
259273
260- return len ( fig_managers )
274+ return num_figs
261275
262276def _plot_directive (plot_path , basedir , function_name , plot_code , caption ,
263277 options , state_machine ):
@@ -309,7 +323,7 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
309323
310324 # Generate the figures, and return the number of them
311325 num_figs = render_figures (plot_path , function_name , plot_code , tmpdir ,
312- formats )
326+ destdir , formats )
313327
314328 # Now start generating the lines of output
315329 lines = []
@@ -346,8 +360,6 @@ def _plot_directive(plot_path, basedir, function_name, plot_code, caption,
346360 if plot_code is None :
347361 links .append ('`source code <%(linkdir)s/%(basename)s.py>`__' )
348362 for format , dpi in formats [1 :]:
349- shutil .copyfile (os .path .join (tmpdir , outname + "." + format ),
350- os .path .join (destdir , outname + "." + format ))
351363 links .append ('`%s <%s/%s.%s>`__' % (format , linkdir , outname , format ))
352364 if len (links ):
353365 links = '[%s]' % (', ' .join (links ) % locals ())
0 commit comments