Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0cef4b

Browse files
committed
Merged revisions 6918 via svnmerge from
https://matplotlib.svn.sf.net/svnroot/matplotlib/branches/v0_98_5_maint ........ r6918 | mdboom | 2009-02-16 10:23:25 -0500 (Mon, 16 Feb 2009) | 2 lines Move plot_directive to installed source tree. Add support for inline code. ........ svn path=/trunk/matplotlib/; revision=6919
2 parents 877cdd9 + c557673 commit d0cef4b

3 files changed

Lines changed: 53 additions & 21 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2009-02-16 Move plot_directive.py to the installed source tree. Add
2+
support for inline code content - MGD
3+
14
2009-02-16 Move mathmpl.py to the installed source tree so it is
25
available to other projects. - MGD
36

doc/conf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
# Add any Sphinx extension module names here, as strings. They can be extensions
2929
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
3030
extensions = ['matplotlib.sphinxext.mathmpl', 'math_symbol_table',
31-
'sphinx.ext.autodoc', 'matplotlib.sphinxext.only_directives',
32-
'plot_directive', 'inheritance_diagram', 'gen_gallery', 'gen_rst']
31+
'sphinx.ext.autodoc', # 'matplotlib.sphinxext.only_directives',
32+
'matplotlib.sphinxext.plot_directive', 'inheritance_diagram',
33+
'gen_gallery', 'gen_rst']
3334

3435
# Add any paths that contain templates here, relative to this directory.
3536
templates_path = ['_templates']

doc/sphinxext/plot_directive.py renamed to lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
plot_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
2024
from docutils.parsers.rst import directives
2125
try:
2226
# docutils 0.4
@@ -34,6 +38,8 @@
3438
import matplotlib.image as image
3539
from matplotlib import _pylab_helpers
3640

41+
import only_directives
42+
3743
if hasattr(os.path, 'relpath'):
3844
relpath = os.path.relpath
3945
else:
@@ -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

Comments
 (0)