From: <jd...@us...> - 2009-03-27 18:11:35
|
Revision: 7006 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7006&view=rev Author: jdh2358 Date: 2009-03-27 18:11:18 +0000 (Fri, 27 Mar 2009) Log Message: ----------- applied Gael's sphinx patch Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py Modified: trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py =================================================================== --- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-03-25 19:27:28 UTC (rev 7005) +++ trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-03-27 18:11:18 UTC (rev 7006) @@ -17,6 +17,7 @@ """ import sys, os, glob, shutil, hashlib, imp, warnings, cStringIO +import re try: from hashlib import md5 except ImportError: @@ -33,7 +34,10 @@ import sphinx sphinx_version = sphinx.__version__.split(".") -sphinx_version = tuple([int(x) for x in sphinx_version[:2]]) +# The split is necessary for sphinx beta versions where the string is +# '6b1' +sphinx_version = tuple([int(re.split('[a-z]', x)[0]) + for x in sphinx_version[:2]]) import matplotlib import matplotlib.cbook as cbook This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <md...@us...> - 2009-09-16 14:12:35
|
Revision: 7767 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7767&view=rev Author: mdboom Date: 2009-09-16 14:12:27 +0000 (Wed, 16 Sep 2009) Log Message: ----------- Minor fixes to plot_directive Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py Modified: trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py =================================================================== --- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-09-15 20:10:35 UTC (rev 7766) +++ trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-09-16 14:12:27 UTC (rev 7767) @@ -153,7 +153,7 @@ (os.path.exists(original) and os.stat(derived).st_mtime < os.stat(original).st_mtime)) -def import_file(plot_path, function_name): +def run_code(plot_path, function_name, plot_code): """ Import a Python module from a path, and run the function given by name, if function_name is not None. @@ -161,29 +161,62 @@ # Change the working directory to the directory of the example, so # it can get at its data files, if any. Add its path to sys.path # so it can import any helper modules sitting beside it. - pwd = os.getcwd() - path, fname = os.path.split(plot_path) - sys.path.insert(0, os.path.abspath(path)) - stdout = sys.stdout - sys.stdout = cStringIO.StringIO() - os.chdir(path) - try: - fd = open(fname) - module = imp.load_module( - "__main__", fd, fname, ('py', 'r', imp.PY_SOURCE)) - finally: - del sys.path[0] - os.chdir(pwd) - sys.stdout = stdout - fd.close() + if plot_code is not None: + exec(plot_code) + else: + pwd = os.getcwd() + path, fname = os.path.split(plot_path) + sys.path.insert(0, os.path.abspath(path)) + stdout = sys.stdout + sys.stdout = cStringIO.StringIO() + os.chdir(path) + try: + fd = open(fname) + module = imp.load_module( + "__plot__", fd, fname, ('py', 'r', imp.PY_SOURCE)) + finally: + del sys.path[0] + os.chdir(pwd) + sys.stdout = stdout + fd.close() - if function_name is not None: - print "function_name", function_name - getattr(module, function_name)() + if function_name is not None: + getattr(module, function_name)() - return module +def run_savefig(plot_path, basename, tmpdir, destdir, formats): + """ + Once a plot script has been imported, this function runs savefig + on all of the figures in all of the desired formats. + """ + fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() + for i, figman in enumerate(fig_managers): + for j, (format, dpi) in enumerate(formats): + if len(fig_managers) == 1: + outname = basename + else: + outname = "%s_%02d" % (basename, i) + outname = outname + "." + format + outpath = os.path.join(tmpdir, outname) + try: + figman.canvas.figure.savefig(outpath, dpi=dpi) + except: + s = cbook.exception_to_str("Exception saving plot %s" % plot_path) + warnings.warn(s) + return 0 + if j > 0: + shutil.copyfile(outpath, os.path.join(destdir, outname)) -def render_figures(plot_path, function_name, plot_code, outdir, formats): + return len(fig_managers) + +def clear_state(): + plt.close('all') + matplotlib.rcdefaults() + # Set a default figure size that doesn't overflow typical browser + # windows. The script is free to override it if necessary. + matplotlib.rcParams['figure.figsize'] = (5.5, 4.5) + +def render_figures(plot_path, function_name, plot_code, tmpdir, destdir, + formats): """ Run a pyplot script and save the low and high res PNGs and a PDF in outdir. @@ -196,7 +229,7 @@ # Look for single-figure output files first for format, dpi in formats: - outname = os.path.join(outdir, '%s.%s' % (basename, format)) + outname = os.path.join(tmpdir, '%s.%s' % (basename, format)) if out_of_date(plot_path, outname): all_exists = False break @@ -211,7 +244,7 @@ all_exists = True for format, dpi in formats: outname = os.path.join( - outdir, '%s_%02d.%s' % (basename, i, format)) + tmpdir, '%s_%02d.%s' % (basename, i, format)) if out_of_date(plot_path, outname): all_exists = False break @@ -225,39 +258,20 @@ # We didn't find the files, so build them - # Clear any existing figures - plt.close('all') - matplotlib.rcdefaults() - # Set a default figure size that doesn't overflow typical browser - # windows. The script is free to override it if necessary. - matplotlib.rcParams['figure.figsize'] = (5.5, 4.5) + clear_state() + try: + run_code(plot_path, function_name, plot_code) + except: + s = cbook.exception_to_str("Exception running plot %s" % plot_path) + warnings.warn(s) + return 0 - if plot_code is not None: - exec(plot_code) - else: - try: - import_file(plot_path, function_name) - except: - s = cbook.exception_to_str("Exception running plot %s" % plot_path) - warnings.warn(s) - return 0 + num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats) - fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() - for i, figman in enumerate(fig_managers): - for format, dpi in formats: - if len(fig_managers) == 1: - outname = basename - else: - outname = "%s_%02d" % (basename, i) - outpath = os.path.join(outdir, '%s.%s' % (outname, format)) - try: - figman.canvas.figure.savefig(outpath, dpi=dpi) - except: - s = cbook.exception_to_str("Exception running plot %s" % plot_path) - warnings.warn(s) - return 0 + if '__plot__' in sys.modules: + del sys.modules['__plot__'] - return len(fig_managers) + return num_figs def _plot_directive(plot_path, basedir, function_name, plot_code, caption, options, state_machine): @@ -309,7 +323,7 @@ # Generate the figures, and return the number of them num_figs = render_figures(plot_path, function_name, plot_code, tmpdir, - formats) + destdir, formats) # Now start generating the lines of output lines = [] @@ -346,8 +360,6 @@ if plot_code is None: links.append('`source code <%(linkdir)s/%(basename)s.py>`__') for format, dpi in formats[1:]: - shutil.copyfile(os.path.join(tmpdir, outname + "." + format), - os.path.join(destdir, outname + "." + format)) links.append('`%s <%s/%s.%s>`__' % (format, linkdir, outname, format)) if len(links): links = '[%s]' % (', '.join(links) % locals()) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <md...@us...> - 2009-09-21 12:07:46
|
Revision: 7806 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7806&view=rev Author: mdboom Date: 2009-09-21 12:07:39 +0000 (Mon, 21 Sep 2009) Log Message: ----------- Don't crash the plot_directive if the plot itself fails. Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py Modified: trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py =================================================================== --- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-09-21 12:07:15 UTC (rev 7805) +++ trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-09-21 12:07:39 UTC (rev 7806) @@ -170,6 +170,7 @@ stdout = sys.stdout sys.stdout = cStringIO.StringIO() os.chdir(path) + fd = None try: fd = open(fname) module = imp.load_module( @@ -178,7 +179,8 @@ del sys.path[0] os.chdir(pwd) sys.stdout = stdout - fd.close() + if fd is not None: + fd.close() if function_name is not None: getattr(module, function_name)() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <md...@us...> - 2009-11-18 16:30:31
|
Revision: 7973 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7973&view=rev Author: mdboom Date: 2009-11-18 16:30:16 +0000 (Wed, 18 Nov 2009) Log Message: ----------- Fix plot directive so it handles source files in various encodings. Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py Modified: trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py =================================================================== --- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-11-18 16:19:21 UTC (rev 7972) +++ trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-11-18 16:30:16 UTC (rev 7973) @@ -38,7 +38,9 @@ Additionally, if the `:include-source:` option is provided, the literal source will be displayed inline in the text, (as well as a -link to the source in HTML). +link to the source in HTML). If this source file is in a non-UTF8 or +non-ASCII encoding, the encoding must be specified using the +`:encoding:` option. The set of file formats to generate can be specified with the `plot_formats` configuration variable. @@ -331,14 +333,21 @@ # Now start generating the lines of output lines = [] + if plot_code is None: + shutil.copyfile(plot_path, os.path.join(destdir, fname)) + if options.has_key('include-source'): if plot_code is None: - fd = open(plot_path, 'r') - plot_code = fd.read() - fd.close() - lines.extend(['::', '']) - lines.extend([' %s' % row.rstrip() - for row in plot_code.split('\n')]) + lines.extend( + ['.. include:: %s' % os.path.join(setup.app.builder.srcdir, plot_path), + ' :literal:']) + if options.has_key('encoding'): + lines.append(' :encoding: %s' % options['encoding']) + del options['encoding'] + else: + lines.extend(['::', '']) + lines.extend([' %s' % row.rstrip() + for row in plot_code.split('\n')]) lines.append('') del options['include-source'] else: @@ -348,8 +357,6 @@ options = ['%s:%s: %s' % (template_content_indent, key, val) for key, val in options.items()] options = "\n".join(options) - if plot_code is None: - shutil.copyfile(plot_path, os.path.join(destdir, fname)) for i in range(num_figs): if num_figs == 1: @@ -425,7 +432,8 @@ 'scale': directives.nonnegative_int, 'align': align, 'class': directives.class_option, - 'include-source': directives.flag } + 'include-source': directives.flag, + 'encoding': directives.encoding } app.add_directive('plot', plot_directive, True, (0, 2, 0), **options) app.add_config_value( This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <md...@us...> - 2009-12-01 18:58:58
|
Revision: 7997 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7997&view=rev Author: mdboom Date: 2009-12-01 18:58:49 +0000 (Tue, 01 Dec 2009) Log Message: ----------- [2906157] Let sphinx reference matplotlib-created plots Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py Modified: trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py =================================================================== --- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-12-01 15:17:21 UTC (rev 7996) +++ trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2009-12-01 18:58:49 UTC (rev 7997) @@ -308,8 +308,8 @@ # treated as relative to the root of the documentation tree. We # need to support both methods here. tmpdir = os.path.join('build', outdir) + tmpdir = os.path.abspath(tmpdir) if sphinx_version < (0, 6): - tmpdir = os.path.abspath(tmpdir) prefix = '' else: prefix = '/' @@ -421,6 +421,36 @@ return _plot_directive(plot_path, 'inline', None, plot_code, '', options, state_machine) +def mark_plot_labels(app, document): + """ + To make plots referenceable, we need to move the reference from + the "htmlonly" (or "latexonly") node to the actual figure node + itself. + """ + for name, explicit in document.nametypes.iteritems(): + if not explicit: + continue + labelid = document.nameids[name] + if labelid is None: + continue + node = document.ids[labelid] + if node.tagname in ('html_only', 'latex_only'): + for n in node: + if n.tagname == 'figure': + sectname = name + for c in n: + if c.tagname == 'caption': + sectname = c.astext() + break + + node['ids'].remove(labelid) + node['names'].remove(name) + n['ids'].append(labelid) + n['names'].append(name) + document.settings.env.labels[name] = \ + document.settings.env.docname, labelid, sectname + break + def setup(app): setup.app = app setup.config = app.config @@ -441,3 +471,4 @@ [('png', 80), ('hires.png', 200), ('pdf', 50)], True) + app.connect('doctree-read', mark_plot_labels) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jd...@us...> - 2010-06-17 17:45:44
|
Revision: 8440 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8440&view=rev Author: jdh2358 Date: 2010-06-17 17:45:38 +0000 (Thu, 17 Jun 2010) Log Message: ----------- add np and plt to default plot namespace in plot directive Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py Modified: trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py =================================================================== --- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2010-06-16 17:08:43 UTC (rev 8439) +++ trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2010-06-17 17:45:38 UTC (rev 8440) @@ -194,7 +194,8 @@ # it can get at its data files, if any. Add its path to sys.path # so it can import any helper modules sitting beside it. if plot_code is not None: - exec(plot_code) + exec_code = 'import numpy as np; import matplotlib.pyplot as plt\n%s'%plot_code + exec(exec_code) else: pwd = os.getcwd() path, fname = os.path.split(plot_path) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jd...@us...> - 2011-01-05 22:20:15
|
Revision: 8894 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8894&view=rev Author: jdh2358 Date: 2011-01-05 22:20:09 +0000 (Wed, 05 Jan 2011) Log Message: ----------- fix plot directive to use rc file defaults Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py Modified: trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py =================================================================== --- trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2011-01-05 22:04:47 UTC (rev 8893) +++ trunk/matplotlib/lib/matplotlib/sphinxext/plot_directive.py 2011-01-05 22:20:09 UTC (rev 8894) @@ -265,10 +265,7 @@ def clear_state(): plt.close('all') - matplotlib.rcdefaults() - # Set a default figure size that doesn't overflow typical browser - # windows. The script is free to override it if necessary. - matplotlib.rcParams['figure.figsize'] = (5.5, 4.5) + matplotlib.rc_file_defaults() def render_figures(plot_path, function_name, plot_code, tmpdir, destdir, formats, context=False): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |