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

Skip to content

Commit 59fa974

Browse files
committed
FIX: Broader API
1 parent 811328f commit 59fa974

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

doc/advanced_configuration.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,10 @@ a description of the interface. Here is pseudocode for what this function
554554
and :func:`sphinx_gallery.gen_rst.mayavi_scraper` do under the hood, which
555555
uses :func:`sphinx_gallery.gen_rst.figure_rst` to create standardized RST::
556556

557-
def mod_scraper(image_path_iterator, gallery_conf)
557+
def mod_scraper(block, block_vars, gallery_conf)
558558
import mymod
559559
image_names = list()
560+
image_path_iterator = block_vars['image_path_iterator']
560561
for fig, image_path in zip(mymod.get_figures(), image_path_iterator):
561562
fig.save_png(image_path)
562563
image_names.append(image_path)
@@ -576,10 +577,11 @@ current directory could do, e.g.::
576577
def __init__(self):
577578
self.seen = set()
578579

579-
def __call__(self, image_path_iterator, gallery_conf):
580+
def __call__(self, block, block_vars, gallery_conf):
580581
count = 0
581582
pngs = sorted(glob.glob(os.path.join(os.getcwd(), '*.png'))
582583
image_names = list()
584+
image_path_iterator = block_vars['image_path_iterator']
583585
for png in my_pngs:
584586
if png not in seen:
585587
seen |= set(png)

sphinx_gallery/gen_rst.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,17 @@ def _import_matplotlib():
271271
_import_matplotlib()
272272

273273

274-
def matplotlib_scraper(image_path_iterator, gallery_conf):
274+
def matplotlib_scraper(block, block_vars, gallery_conf):
275275
"""Scrape Matplotlib images.
276276
277277
Parameters
278278
----------
279-
image_path_iterator : iterable of str
280-
Iterator to get destination paths for images.
279+
block : tuple
280+
A tuple containing the (label, content, line_number) of the block.
281+
block_vars : dict
282+
Dict of block variables.
281283
gallery_conf : dict
282-
The gallery configuration values.
284+
Contains the configuration of Sphinx-Gallery
283285
284286
Returns
285287
-------
@@ -289,6 +291,7 @@ def matplotlib_scraper(image_path_iterator, gallery_conf):
289291
:func:`sphinx_gallery.gen_rst.figure_rst`.
290292
"""
291293
matplotlib, plt = _import_matplotlib()
294+
image_path_iterator = block_vars['image_path_iterator']
292295
image_paths = list()
293296
for fig_num, image_path in zip(plt.get_fignums(), image_path_iterator):
294297
# Set the fig_num figure as the current figure as we can't
@@ -307,15 +310,17 @@ def matplotlib_scraper(image_path_iterator, gallery_conf):
307310
return figure_rst(image_paths, gallery_conf['src_dir'])
308311

309312

310-
def mayavi_scraper(image_path_iterator, gallery_conf):
313+
def mayavi_scraper(block, block_vars, gallery_conf):
311314
"""Scrape Mayavi images.
312315
313316
Parameters
314317
----------
315-
image_path_iterator : iterable of str
316-
Iterator to get destination paths for images.
318+
block : tuple
319+
A tuple containing the (label, content, line_number) of the block.
320+
block_vars : dict
321+
Dict of block variables.
317322
gallery_conf : dict
318-
The gallery configuration values.
323+
Contains the configuration of Sphinx-Gallery
319324
320325
Returns
321326
-------
@@ -325,6 +330,7 @@ def mayavi_scraper(image_path_iterator, gallery_conf):
325330
:func:`sphinx_gallery.gen_rst.figure_rst`.
326331
"""
327332
from mayavi import mlab
333+
image_path_iterator = block_vars['image_path_iterator']
328334
image_paths = list()
329335
e = mlab.get_engine()
330336
for scene, image_path in zip(e.scenes, image_path_iterator):
@@ -378,13 +384,15 @@ def __next__(self):
378384
return path
379385

380386

381-
def save_figures(image_path_iterator, gallery_conf):
387+
def save_figures(block, block_vars, gallery_conf):
382388
"""Save all open figures of the example code-block.
383389
384390
Parameters
385391
----------
386-
image_path_iterator : iterable of str
387-
Iterator to get destination paths for images.
392+
block : tuple
393+
A tuple containing the (label, content, line_number) of the block.
394+
block_vars : dict
395+
Dict of block variables.
388396
gallery_conf : dict
389397
Contains the configuration of Sphinx-Gallery
390398
@@ -395,10 +403,11 @@ def save_figures(image_path_iterator, gallery_conf):
395403
fig_num : int
396404
number of figures saved
397405
"""
406+
image_path_iterator = block_vars['image_path_iterator']
398407
all_rst = u''
399408
prev_count = len(image_path_iterator)
400409
for scraper in gallery_conf['image_scrapers']:
401-
rst = scraper(image_path_iterator, gallery_conf)
410+
rst = scraper(block, block_vars, gallery_conf)
402411
if not isinstance(rst, basestring):
403412
raise TypeError('rst from scraper %r was not a string, '
404413
'got type %s:\n%r'
@@ -659,7 +668,7 @@ def execute_code_block(compiler, block, example_globals,
659668
code_output = u"\n{0}\n\n\n\n".format(except_rst)
660669
# still call this even though we won't use the images so that
661670
# figures are closed
662-
save_figures(block_vars['image_path_iterator'], gallery_conf)
671+
save_figures(block, block_vars, gallery_conf)
663672
else:
664673
sys.stdout.flush()
665674
sys.stdout = orig_stdout
@@ -670,8 +679,7 @@ def execute_code_block(compiler, block, example_globals,
670679
stdout = CODE_OUTPUT.format(indent(my_stdout, u' ' * 4))
671680
else:
672681
stdout = ''
673-
images_rst = save_figures(
674-
block_vars['image_path_iterator'], gallery_conf)
682+
images_rst = save_figures(block, block_vars, gallery_conf)
675683
code_output = u"\n{0}\n\n{1}\n\n".format(images_rst, stdout)
676684

677685
finally:

sphinx_gallery/tests/test_gen_rst.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ def test_save_matplotlib_figures(gallery_conf):
282282
plt.plot(1, 1)
283283
fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png')
284284
image_path_iterator = sg.ImagePathIterator(fname_template)
285-
image_rst = sg.save_figures(image_path_iterator, gallery_conf)
285+
block = ('',) * 3
286+
block_vars = dict(image_path_iterator=image_path_iterator)
287+
image_rst = sg.save_figures(block, block_vars, gallery_conf)
286288
assert len(image_path_iterator) == 1
287289
assert '/image1.png' in image_rst
288290

@@ -292,7 +294,7 @@ def test_save_matplotlib_figures(gallery_conf):
292294
plt.plot(1, 1)
293295
plt.figure()
294296
plt.plot(1, 1)
295-
image_rst = sg.save_figures(image_path_iterator, gallery_conf)
297+
image_rst = sg.save_figures(block, block_vars, gallery_conf)
296298
assert len(image_path_iterator) == 5
297299
assert '/image4.png' in image_rst
298300
assert '/image5.png' in image_rst
@@ -310,11 +312,13 @@ def test_save_mayavi_figures(gallery_conf):
310312
image_scrapers=(sg.matplotlib_scraper, sg.mayavi_scraper))
311313
fname_template = os.path.join(gallery_conf['gallery_dir'], 'image{0}.png')
312314
image_path_iterator = sg.ImagePathIterator(fname_template)
315+
block = ('',) * 3
316+
block_vars = dict(image_path_iterator=image_path_iterator)
313317

314318
plt.axes([-0.1, -0.1, 1.2, 1.2])
315319
plt.pcolor([[0]], cmap='Greens')
316320
mlab.test_plot3d()
317-
image_rst = sg.save_figures(image_path_iterator, gallery_conf)
321+
image_rst = sg.save_figures(block, block_vars, gallery_conf)
318322
assert len(plt.get_fignums()) == 0
319323
assert len(image_path_iterator) == 2
320324
assert '/image0.png' not in image_rst
@@ -334,7 +338,7 @@ def test_save_mayavi_figures(gallery_conf):
334338
mlab.test_plot3d()
335339
plt.axes([-0.1, -0.1, 1.2, 1.2])
336340
plt.pcolor([[0]], cmap='Reds')
337-
image_rst = sg.save_figures(image_path_iterator, gallery_conf)
341+
image_rst = sg.save_figures(block, block_vars, gallery_conf)
338342
assert len(plt.get_fignums()) == 0
339343
assert len(image_path_iterator) == 3
340344
assert '/image1.png' not in image_rst
@@ -350,21 +354,22 @@ def test_save_mayavi_figures(gallery_conf):
350354
assert (pixels == [255, 245, 240]).all()
351355

352356
# custom finders
353-
gallery_conf.update(image_scrapers=[lambda x, y: ''])
354-
image_rst = sg.save_figures(image_path_iterator, gallery_conf)
357+
gallery_conf.update(image_scrapers=[lambda x, y, z: ''])
358+
image_rst = sg.save_figures(block, block_vars, gallery_conf)
355359
assert len(image_path_iterator) == 3
356360

357361
# degenerate
358362
gallery_conf.update(image_scrapers=['foo'])
359363
with pytest.raises(ValueError, match='Unknown image scraper'):
360364
_complete_gallery_conf(
361365
gallery_conf, gallery_conf['gallery_dir'], True, False)
362-
gallery_conf.update(image_scrapers=[lambda x, y: x.next()])
366+
gallery_conf.update(
367+
image_scrapers=[lambda x, y, z: y['image_path_iterator'].next()])
363368
with pytest.raises(RuntimeError, match='did not produce expected image'):
364-
sg.save_figures(image_path_iterator, gallery_conf)
365-
gallery_conf.update(image_scrapers=[lambda x, y: 1.])
369+
sg.save_figures(block, block_vars, gallery_conf)
370+
gallery_conf.update(image_scrapers=[lambda x, y, z: 1.])
366371
with pytest.raises(TypeError, match='was not a string'):
367-
sg.save_figures(image_path_iterator, gallery_conf)
372+
sg.save_figures(block, block_vars, gallery_conf)
368373

369374

370375
def test_zip_notebooks(gallery_conf):

0 commit comments

Comments
 (0)