92
92
plot_basedir
93
93
Base directory, to which ``plot::`` file names are relative
94
94
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.)
96
96
97
97
plot_formats
98
98
File formats to generate. List of tuples or strings::
111
111
112
112
plot_apply_rcparams
113
113
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
115
115
and applies rcParams before each plot.
116
116
117
117
plot_working_directory
123
123
helper modules for all code are located.
124
124
125
125
plot_template
126
- Provide a customized template for preparing resturctured text.
127
-
128
-
126
+ Provide a customized template for preparing restructured text.
129
127
"""
130
128
from __future__ import (absolute_import , division , print_function ,
131
129
unicode_literals )
132
130
133
131
import six
134
132
from six .moves import xrange
135
133
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
137
136
import traceback
138
137
139
138
if not six .PY3 :
140
139
import cStringIO
141
140
142
141
from docutils .parsers .rst import directives
143
- from docutils import nodes
144
142
from docutils .parsers .rst .directives .images import Image
145
143
align = Image .align
146
144
import sphinx
@@ -169,67 +167,6 @@ def format_template(template, **kw):
169
167
170
168
__version__ = 2
171
169
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
-
233
170
#------------------------------------------------------------------------------
234
171
# Registration hook
235
172
#------------------------------------------------------------------------------
@@ -239,6 +176,7 @@ def plot_directive(name, arguments, options, content, lineno,
239
176
return run (arguments , content , options , state_machine , state , lineno )
240
177
plot_directive .__doc__ = __doc__
241
178
179
+
242
180
def _option_boolean (arg ):
243
181
if not arg or not arg .strip ():
244
182
# no argument given, assume used as a flag
@@ -267,6 +205,7 @@ def _option_align(arg):
267
205
return directives .choice (arg , ("top" , "middle" , "bottom" , "left" , "center" ,
268
206
"right" ))
269
207
208
+
270
209
def mark_plot_labels (app , document ):
271
210
"""
272
211
To make plots referenceable, we need to move the reference from
@@ -297,6 +236,7 @@ def mark_plot_labels(app, document):
297
236
document .settings .env .docname , labelid , sectname
298
237
break
299
238
239
+
300
240
def setup (app ):
301
241
setup .app = app
302
242
setup .config = app .config
@@ -344,6 +284,7 @@ def contains_doctest(text):
344
284
m = r .search (text )
345
285
return bool (m )
346
286
287
+
347
288
def unescape_doctest (text ):
348
289
"""
349
290
Extract code from a piece of text, which contains either Python code
@@ -364,6 +305,7 @@ def unescape_doctest(text):
364
305
code += "\n "
365
306
return code
366
307
308
+
367
309
def split_code_at_show (text ):
368
310
"""
369
311
Split code at plt.show()
@@ -386,6 +328,7 @@ def split_code_at_show(text):
386
328
parts .append ("\n " .join (part ))
387
329
return parts
388
330
331
+
389
332
def remove_coding (text ):
390
333
"""
391
334
Remove the coding comment, which six.exec_ doesn't like.
@@ -403,9 +346,9 @@ def remove_coding(text):
403
346
404
347
{{ only_html }}
405
348
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) %}
407
350
(
408
- {%- if source_link and html_show_source_link -%}
351
+ {%- if source_link -%}
409
352
`Source code <{{ source_link }}>`__
410
353
{%- endif -%}
411
354
{%- if html_show_formats and not multi_image -%}
@@ -480,6 +423,7 @@ def filename(self, format):
480
423
def filenames (self ):
481
424
return [self .filename (fmt ) for fmt in self .formats ]
482
425
426
+
483
427
def out_of_date (original , derived ):
484
428
"""
485
429
Returns True if derivative is out-of-date wrt original,
@@ -489,9 +433,11 @@ def out_of_date(original, derived):
489
433
(os .path .exists (original ) and
490
434
os .stat (derived ).st_mtime < os .stat (original ).st_mtime ))
491
435
436
+
492
437
class PlotError (RuntimeError ):
493
438
pass
494
439
440
+
495
441
def run_code (code , code_path , ns = None , function_name = None ):
496
442
"""
497
443
Import a Python module from a path, and run the function given by
@@ -565,12 +511,14 @@ def _dummy_print(*arg, **kwarg):
565
511
sys .stdout = stdout
566
512
return ns
567
513
514
+
568
515
def clear_state (plot_rcparams , close = True ):
569
516
if close :
570
517
plt .close ('all' )
571
518
matplotlib .rc_file_defaults ()
572
519
matplotlib .rcParams .update (plot_rcparams )
573
520
521
+
574
522
def render_figures (code , code_path , output_dir , output_base , context ,
575
523
function_name , config , context_reset = False ):
576
524
"""
@@ -681,6 +629,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
681
629
682
630
return results
683
631
632
+
684
633
def run (arguments , content , options , state_machine , state , lineno ):
685
634
# The user may provide a filename *or* Python code content, but not both
686
635
if arguments and content :
@@ -818,7 +767,9 @@ def run(arguments, content, options, state_machine, state, lineno):
818
767
only_latex = ".. only:: latex"
819
768
only_texinfo = ".. only:: texinfo"
820
769
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 :
822
773
src_link = source_link
823
774
else :
824
775
src_link = None
@@ -828,15 +779,14 @@ def run(arguments, content, options, state_machine, state, lineno):
828
779
dest_dir = dest_dir_link ,
829
780
build_dir = build_dir_link ,
830
781
source_link = src_link ,
831
- html_show_source_link = config .plot_html_show_source_link ,
832
782
multi_image = len (images ) > 1 ,
833
783
only_html = only_html ,
834
784
only_latex = only_latex ,
835
785
only_texinfo = only_texinfo ,
836
786
options = opts ,
837
787
images = images ,
838
788
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 ,
840
790
caption = caption )
841
791
842
792
total_lines .extend (result .split ("\n " ))
0 commit comments