19
19
from sphinx .util .console import red
20
20
from . import sphinx_compatibility , glr_path_static , __version__ as _sg_version
21
21
from .gen_rst import generate_dir_rst , SPHX_GLR_SIG
22
+ from .scrapers import _scraper_dict , _reset_dict
22
23
from .docs_resolv import embed_code_links
23
24
from .downloads import generate_zipfiles
24
25
from .sorting import NumberOfCodeLinesSortKey
25
- from .binder import copy_binder_files , check_binder_conf
26
+ from .binder import copy_binder_files
26
27
27
28
try :
28
29
FileNotFoundError
29
30
except NameError :
30
31
# Python2
31
32
FileNotFoundError = IOError
32
33
34
+ try :
35
+ basestring
36
+ except NameError :
37
+ basestring = str
38
+ unicode = str
39
+
33
40
DEFAULT_GALLERY_CONF = {
34
41
'filename_pattern' : re .escape (os .sep ) + 'plot' ,
35
42
'ignore_pattern' : '__init__\.py' ,
53
60
'thumbnail_size' : (400 , 280 ), # Default CSS does 0.4 scaling (160, 112)
54
61
'min_reported_time' : 0 ,
55
62
'binder' : {},
63
+ 'image_scrapers' : ('matplotlib' ,),
64
+ 'reset_modules' : ('matplotlib' , 'seaborn' ),
56
65
}
57
66
58
67
logger = sphinx_compatibility .getLogger ('sphinx-gallery' )
@@ -89,13 +98,31 @@ def parse_config(app):
89
98
plot_gallery = eval (app .builder .config .plot_gallery )
90
99
except TypeError :
91
100
plot_gallery = bool (app .builder .config .plot_gallery )
101
+ src_dir = app .builder .srcdir
102
+ abort_on_example_error = app .builder .config .abort_on_example_error
103
+ gallery_conf = _complete_gallery_conf (
104
+ app .config .sphinx_gallery_conf , src_dir , plot_gallery ,
105
+ abort_on_example_error )
106
+ # this assures I can call the config in other places
107
+ app .config .sphinx_gallery_conf = gallery_conf
108
+ app .config .html_static_path .append (glr_path_static ())
109
+ return gallery_conf
92
110
111
+
112
+ def _complete_gallery_conf (sphinx_gallery_conf , src_dir , plot_gallery ,
113
+ abort_on_example_error ):
93
114
gallery_conf = copy .deepcopy (DEFAULT_GALLERY_CONF )
94
- gallery_conf .update (app .config .sphinx_gallery_conf )
115
+ gallery_conf .update (sphinx_gallery_conf )
116
+ if sphinx_gallery_conf .get ('find_mayavi_figures' , False ):
117
+ logger .warning (
118
+ "Deprecated image scraping variable `find_mayavi_figures`\n "
119
+ "detected, use `image_scrapers` instead as:\n \n "
120
+ " image_scrapers=('matplotlib', 'mayavi')" ,
121
+ type = DeprecationWarning )
122
+ gallery_conf ['image_scrapers' ] += ('mayavi' ,)
95
123
gallery_conf .update (plot_gallery = plot_gallery )
96
- gallery_conf .update (
97
- abort_on_example_error = app .builder .config .abort_on_example_error )
98
- gallery_conf ['src_dir' ] = app .builder .srcdir
124
+ gallery_conf .update (abort_on_example_error = abort_on_example_error )
125
+ gallery_conf ['src_dir' ] = src_dir
99
126
100
127
if gallery_conf .get ("mod_example_dir" , False ):
101
128
backreferences_warning = """\n ========
@@ -105,15 +132,43 @@ def parse_config(app):
105
132
version of Sphinx-Gallery. For more details, see the backreferences
106
133
documentation:
107
134
108
- https://sphinx-gallery.readthedocs.io/en/latest/advanced_configuration.html#references-to-examples"""
135
+ https://sphinx-gallery.readthedocs.io/en/latest/advanced_configuration.html#references-to-examples""" # noqa: E501
109
136
gallery_conf ['backreferences_dir' ] = gallery_conf ['mod_example_dir' ]
110
137
logger .warning (
111
138
backreferences_warning ,
112
139
type = DeprecationWarning )
113
140
114
- # this assures I can call the config in other places
115
- app .config .sphinx_gallery_conf = gallery_conf
116
- app .config .html_static_path .append (glr_path_static ())
141
+ # deal with scrapers
142
+ scrapers = gallery_conf ['image_scrapers' ]
143
+ if not isinstance (scrapers , (tuple , list )):
144
+ scrapers = [scrapers ]
145
+ scrapers = list (scrapers )
146
+ for si , scraper in enumerate (scrapers ):
147
+ if isinstance (scraper , basestring ):
148
+ if scraper not in _scraper_dict :
149
+ raise ValueError ('Unknown image scraper named %r' % (scraper ,))
150
+ scrapers [si ] = _scraper_dict [scraper ]
151
+ elif not callable (scraper ):
152
+ raise ValueError ('Scraper %r was not callable' % (scraper ,))
153
+ gallery_conf ['image_scrapers' ] = tuple (scrapers )
154
+ del scrapers
155
+
156
+ # deal with resetters
157
+ resetters = gallery_conf ['reset_modules' ]
158
+ if not isinstance (resetters , (tuple , list )):
159
+ resetters = [resetters ]
160
+ resetters = list (resetters )
161
+ for ri , resetter in enumerate (resetters ):
162
+ if isinstance (resetter , basestring ):
163
+ if resetter not in _reset_dict :
164
+ raise ValueError ('Unknown module resetter named %r'
165
+ % (resetter ,))
166
+ resetters [ri ] = _reset_dict [resetter ]
167
+ elif not callable (resetter ):
168
+ raise ValueError ('Module resetter %r was not callable'
169
+ % (resetter ,))
170
+ gallery_conf ['reset_modules' ] = tuple (resetters )
171
+ del resetters
117
172
118
173
return gallery_conf
119
174
@@ -137,7 +192,8 @@ def get_subsections(srcdir, examples_dir, sortkey):
137
192
138
193
"""
139
194
subfolders = [subfolder for subfolder in os .listdir (examples_dir )
140
- if os .path .exists (os .path .join (examples_dir , subfolder , 'README.txt' ))]
195
+ if os .path .exists (os .path .join (
196
+ examples_dir , subfolder , 'README.txt' ))]
141
197
base_examples_dir_path = os .path .relpath (examples_dir , srcdir )
142
198
subfolders_with_path = [os .path .join (base_examples_dir_path , item )
143
199
for item in subfolders ]
@@ -214,11 +270,14 @@ def generate_gallery_rst(app):
214
270
# :orphan: to suppress "not included in TOCTREE" sphinx warnings
215
271
fhindex .write (":orphan:\n \n " + this_fhindex )
216
272
217
- for subsection in get_subsections (app .builder .srcdir , examples_dir , gallery_conf ['subsection_order' ]):
273
+ for subsection in get_subsections (
274
+ app .builder .srcdir , examples_dir ,
275
+ gallery_conf ['subsection_order' ]):
218
276
src_dir = os .path .join (examples_dir , subsection )
219
277
target_dir = os .path .join (gallery_dir , subsection )
220
- this_fhindex , this_computation_times = generate_dir_rst (src_dir , target_dir , gallery_conf ,
221
- seen_backrefs )
278
+ this_fhindex , this_computation_times = \
279
+ generate_dir_rst (src_dir , target_dir , gallery_conf ,
280
+ seen_backrefs )
222
281
fhindex .write (this_fhindex )
223
282
computation_times += this_computation_times
224
283
@@ -258,9 +317,9 @@ def touch_empty_backreferences(app, what, name, obj, options, lines):
258
317
259
318
260
319
def summarize_failing_examples (app , exception ):
261
- """Collects the list of falling examples during build and prints them with the traceback
320
+ """Collects the list of falling examples and prints them with a traceback.
262
321
263
- Raises ValueError if there where failing examples
322
+ Raises ValueError if there where failing examples.
264
323
"""
265
324
if exception is not None :
266
325
return
@@ -271,9 +330,9 @@ def summarize_failing_examples(app, exception):
271
330
272
331
gallery_conf = app .config .sphinx_gallery_conf
273
332
failing_examples = set (gallery_conf ['failing_examples' ].keys ())
274
- expected_failing_examples = set ([ os . path . normpath ( os . path . join ( app . srcdir , path ))
275
- for path in
276
- gallery_conf ['expected_failing_examples' ] ])
333
+ expected_failing_examples = set (
334
+ os . path . normpath ( os . path . join ( app . srcdir , path ))
335
+ for path in gallery_conf ['expected_failing_examples' ])
277
336
278
337
examples_expected_to_fail = failing_examples .intersection (
279
338
expected_failing_examples )
@@ -297,9 +356,9 @@ def summarize_failing_examples(app, exception):
297
356
failing_examples )
298
357
# filter from examples actually run
299
358
filename_pattern = gallery_conf .get ('filename_pattern' )
300
- examples_not_expected_to_pass = [src_file
301
- for src_file in examples_not_expected_to_pass
302
- if re .search (filename_pattern , src_file )]
359
+ examples_not_expected_to_pass = [
360
+ src_file for src_file in examples_not_expected_to_pass
361
+ if re .search (filename_pattern , src_file )]
303
362
if examples_not_expected_to_pass :
304
363
fail_msgs .append (red ("Examples expected to fail, but not failing:\n " ) +
305
364
"Please remove these examples from\n " +
0 commit comments