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

Skip to content

Commit 88fc024

Browse files
dmcdougalltacaswell
authored andcommitted
Merge pull request matplotlib#3257 from matthew-brett/refactor-plot-directive
MRG: refactor and bugfixes for plot_directive
1 parent 2ecabc1 commit 88fc024

File tree

1 file changed

+23
-73
lines changed

1 file changed

+23
-73
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 23 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
plot_basedir
9393
Base directory, to which ``plot::`` file names are relative
9494
to. (If None or empty, file names are relative to the
95-
directoly where the file containing the directive is.)
95+
directory where the file containing the directive is.)
9696
9797
plot_formats
9898
File formats to generate. List of tuples or strings::
@@ -111,7 +111,7 @@
111111
112112
plot_apply_rcparams
113113
By default, rcParams are applied when `context` option is not used in
114-
a plot directive. This configuration option overrides this behaviour
114+
a plot directive. This configuration option overrides this behavior
115115
and applies rcParams before each plot.
116116
117117
plot_working_directory
@@ -123,24 +123,22 @@
123123
helper modules for all code are located.
124124
125125
plot_template
126-
Provide a customized template for preparing resturctured text.
127-
128-
126+
Provide a customized template for preparing restructured text.
129127
"""
130128
from __future__ import (absolute_import, division, print_function,
131129
unicode_literals)
132130

133131
import six
134132
from six.moves import xrange
135133

136-
import sys, os, glob, shutil, imp, warnings, io, re, textwrap
134+
import sys, os, shutil, io, re, textwrap
135+
from os.path import relpath
137136
import traceback
138137

139138
if not six.PY3:
140139
import cStringIO
141140

142141
from docutils.parsers.rst import directives
143-
from docutils import nodes
144142
from docutils.parsers.rst.directives.images import Image
145143
align = Image.align
146144
import sphinx
@@ -169,67 +167,6 @@ def format_template(template, **kw):
169167

170168
__version__ = 2
171169

172-
#------------------------------------------------------------------------------
173-
# Relative pathnames
174-
#------------------------------------------------------------------------------
175-
176-
# os.path.relpath is new in Python 2.6
177-
try:
178-
from os.path import relpath
179-
except ImportError:
180-
# Copied from Python 2.7
181-
if 'posix' in sys.builtin_module_names:
182-
def relpath(path, start=os.path.curdir):
183-
"""Return a relative version of a path"""
184-
from os.path import sep, curdir, join, abspath, commonprefix, \
185-
pardir
186-
187-
if not path:
188-
raise ValueError("no path specified")
189-
190-
start_list = abspath(start).split(sep)
191-
path_list = abspath(path).split(sep)
192-
193-
# Work out how much of the filepath is shared by start and path.
194-
i = len(commonprefix([start_list, path_list]))
195-
196-
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
197-
if not rel_list:
198-
return curdir
199-
return join(*rel_list)
200-
elif 'nt' in sys.builtin_module_names:
201-
def relpath(path, start=os.path.curdir):
202-
"""Return a relative version of a path"""
203-
from os.path import sep, curdir, join, abspath, commonprefix, \
204-
pardir, splitunc
205-
206-
if not path:
207-
raise ValueError("no path specified")
208-
start_list = abspath(start).split(sep)
209-
path_list = abspath(path).split(sep)
210-
if start_list[0].lower() != path_list[0].lower():
211-
unc_path, rest = splitunc(path)
212-
unc_start, rest = splitunc(start)
213-
if bool(unc_path) ^ bool(unc_start):
214-
raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
215-
% (path, start))
216-
else:
217-
raise ValueError("path is on drive %s, start on drive %s"
218-
% (path_list[0], start_list[0]))
219-
# Work out how much of the filepath is shared by start and path.
220-
for i in range(min(len(start_list), len(path_list))):
221-
if start_list[i].lower() != path_list[i].lower():
222-
break
223-
else:
224-
i += 1
225-
226-
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
227-
if not rel_list:
228-
return curdir
229-
return join(*rel_list)
230-
else:
231-
raise RuntimeError("Unsupported platform (no relpath available!)")
232-
233170
#------------------------------------------------------------------------------
234171
# Registration hook
235172
#------------------------------------------------------------------------------
@@ -239,6 +176,7 @@ def plot_directive(name, arguments, options, content, lineno,
239176
return run(arguments, content, options, state_machine, state, lineno)
240177
plot_directive.__doc__ = __doc__
241178

179+
242180
def _option_boolean(arg):
243181
if not arg or not arg.strip():
244182
# no argument given, assume used as a flag
@@ -267,6 +205,7 @@ def _option_align(arg):
267205
return directives.choice(arg, ("top", "middle", "bottom", "left", "center",
268206
"right"))
269207

208+
270209
def mark_plot_labels(app, document):
271210
"""
272211
To make plots referenceable, we need to move the reference from
@@ -297,6 +236,7 @@ def mark_plot_labels(app, document):
297236
document.settings.env.docname, labelid, sectname
298237
break
299238

239+
300240
def setup(app):
301241
setup.app = app
302242
setup.config = app.config
@@ -344,6 +284,7 @@ def contains_doctest(text):
344284
m = r.search(text)
345285
return bool(m)
346286

287+
347288
def unescape_doctest(text):
348289
"""
349290
Extract code from a piece of text, which contains either Python code
@@ -364,6 +305,7 @@ def unescape_doctest(text):
364305
code += "\n"
365306
return code
366307

308+
367309
def split_code_at_show(text):
368310
"""
369311
Split code at plt.show()
@@ -386,6 +328,7 @@ def split_code_at_show(text):
386328
parts.append("\n".join(part))
387329
return parts
388330

331+
389332
def remove_coding(text):
390333
"""
391334
Remove the coding comment, which six.exec_ doesn't like.
@@ -403,9 +346,9 @@ def remove_coding(text):
403346
404347
{{ only_html }}
405348
406-
{% if (source_link and html_show_source_link) or (html_show_formats and not multi_image) %}
349+
{% if source_link or (html_show_formats and not multi_image) %}
407350
(
408-
{%- if source_link and html_show_source_link -%}
351+
{%- if source_link -%}
409352
`Source code <{{ source_link }}>`__
410353
{%- endif -%}
411354
{%- if html_show_formats and not multi_image -%}
@@ -480,6 +423,7 @@ def filename(self, format):
480423
def filenames(self):
481424
return [self.filename(fmt) for fmt in self.formats]
482425

426+
483427
def out_of_date(original, derived):
484428
"""
485429
Returns True if derivative is out-of-date wrt original,
@@ -489,9 +433,11 @@ def out_of_date(original, derived):
489433
(os.path.exists(original) and
490434
os.stat(derived).st_mtime < os.stat(original).st_mtime))
491435

436+
492437
class PlotError(RuntimeError):
493438
pass
494439

440+
495441
def run_code(code, code_path, ns=None, function_name=None):
496442
"""
497443
Import a Python module from a path, and run the function given by
@@ -565,12 +511,14 @@ def _dummy_print(*arg, **kwarg):
565511
sys.stdout = stdout
566512
return ns
567513

514+
568515
def clear_state(plot_rcparams, close=True):
569516
if close:
570517
plt.close('all')
571518
matplotlib.rc_file_defaults()
572519
matplotlib.rcParams.update(plot_rcparams)
573520

521+
574522
def render_figures(code, code_path, output_dir, output_base, context,
575523
function_name, config, context_reset=False):
576524
"""
@@ -681,6 +629,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
681629

682630
return results
683631

632+
684633
def run(arguments, content, options, state_machine, state, lineno):
685634
# The user may provide a filename *or* Python code content, but not both
686635
if arguments and content:
@@ -818,7 +767,9 @@ def run(arguments, content, options, state_machine, state, lineno):
818767
only_latex = ".. only:: latex"
819768
only_texinfo = ".. only:: texinfo"
820769

821-
if j == 0:
770+
# Not-None src_link signals the need for a source link in the generated
771+
# html
772+
if j == 0 and config.plot_html_show_source_link:
822773
src_link = source_link
823774
else:
824775
src_link = None
@@ -828,15 +779,14 @@ def run(arguments, content, options, state_machine, state, lineno):
828779
dest_dir=dest_dir_link,
829780
build_dir=build_dir_link,
830781
source_link=src_link,
831-
html_show_source_link=config.plot_html_show_source_link,
832782
multi_image=len(images) > 1,
833783
only_html=only_html,
834784
only_latex=only_latex,
835785
only_texinfo=only_texinfo,
836786
options=opts,
837787
images=images,
838788
source_code=source_code,
839-
html_show_formats=config.plot_html_show_formats,
789+
html_show_formats=config.plot_html_show_formats and not nofigs,
840790
caption=caption)
841791

842792
total_lines.extend(result.split("\n"))

0 commit comments

Comments
 (0)