1616plot_formats configuration variable.
1717"""
1818
19- import sys , os , glob , shutil , imp , warnings , cStringIO
19+ import sys , os , glob , shutil , hashlib , imp , warnings , cStringIO
20+ try :
21+ from hashlib import md5
22+ except ImportError :
23+ from md5 import md5
2024from docutils .parsers .rst import directives
2125try :
2226 # docutils 0.4
3438import matplotlib .image as image
3539from matplotlib import _pylab_helpers
3640
41+ import only_directives
42+
3743if hasattr (os .path , 'relpath' ):
3844 relpath = os .path .relpath
3945else :
@@ -134,16 +140,20 @@ def runfile(fullpath):
134140 sys .stdout = stdout
135141 return module
136142
137- def makefig (fullpath , outdir ):
143+ def makefig (fullpath , code , outdir ):
138144 """
139145 run a pyplot script and save the low and high res PNGs and a PDF in _static
140146 """
141147 formats = [('png' , 80 ), ('hires.png' , 200 ), ('pdf' , 50 )]
142148
143149 fullpath = str (fullpath ) # todo, why is unicode breaking this
144-
145150 basedir , fname = os .path .split (fullpath )
146151 basename , ext = os .path .splitext (fname )
152+
153+ if str (basename ) == "None" :
154+ import pdb
155+ pdb .set_trace ()
156+
147157 all_exists = True
148158
149159 # Look for single-figure output files first
@@ -183,12 +193,15 @@ def makefig(fullpath, outdir):
183193 # Set a figure size that doesn't overflow typical browser windows
184194 matplotlib .rcParams ['figure.figsize' ] = (5.5 , 4.5 )
185195
186- try :
187- runfile (fullpath )
188- except :
189- s = cbook .exception_to_str ("Exception running plot %s" % fullpath )
190- warnings .warn (s )
191- return 0
196+ if code is not None :
197+ exec (code )
198+ else :
199+ try :
200+ runfile (fullpath )
201+ except :
202+ s = cbook .exception_to_str ("Exception running plot %s" % fullpath )
203+ warnings .warn (s )
204+ return 0
192205
193206 fig_managers = _pylab_helpers .Gcf .get_all_fig_managers ()
194207 for i , figman in enumerate (fig_managers ):
@@ -218,10 +231,21 @@ def plot_directive(name, arguments, options, content, lineno,
218231 if type (formats ) == str :
219232 formats = eval (formats )
220233
221- reference = directives .uri (arguments [0 ])
222- basedir , fname = os .path .split (reference )
223- basename , ext = os .path .splitext (fname )
224- basedir = relpath (basedir , setup .app .builder .srcdir )
234+ # The user may provide a filename *or* Python code content, but not both
235+ if len (arguments ) == 1 :
236+ reference = directives .uri (arguments [0 ])
237+ basedir , fname = os .path .split (reference )
238+ basename , ext = os .path .splitext (fname )
239+ basedir = relpath (basedir , setup .app .builder .srcdir )
240+ if len (content ):
241+ raise ValueError ("plot directive may not specify both a filename and inline content" )
242+ content = None
243+ else :
244+ basedir = "inline"
245+ content = '\n ' .join (content )
246+ # Since we don't have a filename, use a hash based on the content
247+ reference = basename = md5 (content ).hexdigest ()[- 10 :]
248+ fname = None
225249
226250 # Get the directory of the rst file, and determine the relative
227251 # path from the resulting html file to the plot_directive links
@@ -249,11 +273,12 @@ def plot_directive(name, arguments, options, content, lineno,
249273 cbook .mkdirs (destdir )
250274
251275 # Generate the figures, and return the number of them
252- num_figs = makefig (reference , tmpdir )
276+ num_figs = makefig (reference , content , tmpdir )
253277
254278 if options .has_key ('include-source' ):
255- contents = open (reference , 'r' ).read ()
256- lines = ['::' , '' ] + [' %s' % row .rstrip () for row in contents .split ('\n ' )]
279+ if content is None :
280+ content = open (reference , 'r' ).read ()
281+ lines = ['::' , '' ] + [' %s' % row .rstrip () for row in content .split ('\n ' )]
257282 del options ['include-source' ]
258283 else :
259284 lines = []
@@ -262,7 +287,8 @@ def plot_directive(name, arguments, options, content, lineno,
262287 options = [' :%s: %s' % (key , val ) for key , val in
263288 options .items ()]
264289 options = "\n " .join (options )
265- shutil .copyfile (reference , os .path .join (destdir , fname ))
290+ if fname is not None :
291+ shutil .copyfile (reference , os .path .join (destdir , fname ))
266292
267293 for i in range (num_figs ):
268294 if num_figs == 1 :
@@ -272,7 +298,9 @@ def plot_directive(name, arguments, options, content, lineno,
272298
273299 # Copy the linked-to files to the destination within the build tree,
274300 # and add a link for them
275- links = ['`source code <%(linkdir)s/%(basename)s.py>`__' ]
301+ links = []
302+ if fname is not None :
303+ links .append ('`source code <%(linkdir)s/%(basename)s.py>`__' )
276304 for format in formats [1 :]:
277305 shutil .copyfile (os .path .join (tmpdir , outname + "." + format ),
278306 os .path .join (destdir , outname + "." + format ))
@@ -295,7 +323,7 @@ def setup(app):
295323 setup .config = app .config
296324 setup .confdir = app .confdir
297325
298- app .add_directive ('plot' , plot_directive , False , (1 , 0 , 1 ), ** options )
326+ app .add_directive ('plot' , plot_directive , True , (0 , 1 , 0 ), ** options )
299327 app .add_config_value (
300328 'plot_formats' ,
301329 ['png' , 'hires.png' , 'pdf' ],
0 commit comments