|
44 | 44 |
|
45 | 45 | The set of file formats to generate can be specified with the |
46 | 46 | `plot_formats` configuration variable. |
| 47 | +
|
| 48 | +
|
| 49 | +Error handling: |
| 50 | +
|
| 51 | +Any errors generated during the running of the code are emitted as warnings |
| 52 | +using the Python `warnings` module, using a custom category called |
| 53 | +`PlotWarning`. To turn the warnings into fatal errors that stop the |
| 54 | +documentation build, after adjusting your `sys.path` in your `conf.py` Sphinx |
| 55 | +configuration file, use:: |
| 56 | +
|
| 57 | + import plot_directive |
| 58 | + warnings.simplefilter('error', plot_directive.PlotWarning) |
47 | 59 | """ |
48 | 60 |
|
49 | 61 | import sys, os, shutil, imp, warnings, cStringIO, re |
|
76 | 88 | from matplotlib import _pylab_helpers |
77 | 89 | from matplotlib.sphinxext import only_directives |
78 | 90 |
|
| 91 | + |
| 92 | +class PlotWarning(Warning): |
| 93 | + """Warning category for all warnings generated by this directive. |
| 94 | +
|
| 95 | + By printing our warnings with this category, it becomes possible to turn |
| 96 | + them into errors by using in your conf.py:: |
| 97 | +
|
| 98 | + warnings.simplefilter('error', plot_directive.PlotWarning) |
| 99 | +
|
| 100 | + This way, you can ensure that your docs only build if all your examples |
| 101 | + actually run successfully. |
| 102 | + """ |
| 103 | + pass |
| 104 | + |
| 105 | + |
79 | 106 | # os.path.relpath is new in Python 2.6 |
80 | 107 | if hasattr(os.path, 'relpath'): |
81 | 108 | relpath = os.path.relpath |
@@ -208,7 +235,7 @@ def run_savefig(plot_path, basename, tmpdir, destdir, formats): |
208 | 235 | figman.canvas.figure.savefig(outpath, dpi=dpi) |
209 | 236 | except: |
210 | 237 | s = cbook.exception_to_str("Exception saving plot %s" % plot_path) |
211 | | - warnings.warn(s) |
| 238 | + warnings.warn(s, PlotWarning) |
212 | 239 | return 0 |
213 | 240 | if j > 0: |
214 | 241 | shutil.copyfile(outpath, os.path.join(destdir, outname)) |
@@ -270,7 +297,7 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir, |
270 | 297 | run_code(plot_path, function_name, plot_code) |
271 | 298 | except: |
272 | 299 | s = cbook.exception_to_str("Exception running plot %s" % plot_path) |
273 | | - warnings.warn(s) |
| 300 | + warnings.warn(s, PlotWarning) |
274 | 301 | return 0 |
275 | 302 |
|
276 | 303 | num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats) |
|
0 commit comments