Skip to content

Commit 3bf334c

Browse files
committed
Make validate_movie_writer actually check registered writers.
Right now the list in validate_movie_writer is outdated -- pillow is missing from it. Just use the actual list of registered writers instead of trying to maintain it in sync.
1 parent ff9bc1f commit 3bf334c

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

lib/matplotlib/animation.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import matplotlib as mpl
3535
from matplotlib._animation_data import (
3636
DISPLAY_TEMPLATE, INCLUDED_FRAMES, JS_INCLUDE, STYLE_INCLUDE)
37-
from matplotlib import cbook, rcParams, rc_context
37+
from matplotlib import cbook
3838

3939

4040
_log = logging.getLogger(__name__)
@@ -288,17 +288,17 @@ def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
288288
self.frame_format = 'rgba'
289289

290290
if codec is None:
291-
self.codec = rcParams['animation.codec']
291+
self.codec = mpl.rcParams['animation.codec']
292292
else:
293293
self.codec = codec
294294

295295
if bitrate is None:
296-
self.bitrate = rcParams['animation.bitrate']
296+
self.bitrate = mpl.rcParams['animation.bitrate']
297297
else:
298298
self.bitrate = bitrate
299299

300300
if extra_args is None:
301-
self.extra_args = list(rcParams[self.args_key])
301+
self.extra_args = list(mpl.rcParams[self.args_key])
302302
else:
303303
self.extra_args = extra_args
304304

@@ -418,7 +418,7 @@ def bin_path(cls):
418418
subclass. This is a class method so that the tool can be looked for
419419
before making a particular MovieWriter subclass available.
420420
'''
421-
return str(rcParams[cls.exec_key])
421+
return str(mpl.rcParams[cls.exec_key])
422422

423423
@classmethod
424424
def isAvailable(cls):
@@ -435,7 +435,7 @@ class FileMovieWriter(MovieWriter):
435435
'''
436436
def __init__(self, *args, **kwargs):
437437
MovieWriter.__init__(self, *args, **kwargs)
438-
self.frame_format = rcParams['animation.frame_format']
438+
self.frame_format = mpl.rcParams['animation.frame_format']
439439

440440
def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
441441
clear_temp=True):
@@ -802,7 +802,7 @@ def __init__(self, fps=30, codec=None, bitrate=None, extra_args=None,
802802

803803
# Save embed limit, which is given in MB
804804
if embed_limit is None:
805-
self._bytes_limit = rcParams['animation.embed_limit']
805+
self._bytes_limit = mpl.rcParams['animation.embed_limit']
806806
else:
807807
self._bytes_limit = embed_limit
808808

@@ -1046,7 +1046,7 @@ def func(current_frame: int, total_frames: int) -> Any
10461046
# If the writer is None, use the rc param to find the name of the one
10471047
# to use
10481048
if writer is None:
1049-
writer = rcParams['animation.writer']
1049+
writer = mpl.rcParams['animation.writer']
10501050
elif (not isinstance(writer, str) and
10511051
any(arg is not None
10521052
for arg in (fps, codec, bitrate, extra_args, metadata))):
@@ -1074,15 +1074,15 @@ def func(current_frame: int, total_frames: int) -> Any
10741074

10751075
# Re-use the savefig DPI for ours if none is given
10761076
if dpi is None:
1077-
dpi = rcParams['savefig.dpi']
1077+
dpi = mpl.rcParams['savefig.dpi']
10781078
if dpi == 'figure':
10791079
dpi = self._fig.dpi
10801080

10811081
if codec is None:
1082-
codec = rcParams['animation.codec']
1082+
codec = mpl.rcParams['animation.codec']
10831083

10841084
if bitrate is None:
1085-
bitrate = rcParams['animation.bitrate']
1085+
bitrate = mpl.rcParams['animation.bitrate']
10861086

10871087
all_anim = [self]
10881088
if extra_anim is not None:
@@ -1122,12 +1122,12 @@ def func(current_frame: int, total_frames: int) -> Any
11221122
# TODO: Right now, after closing the figure, saving a movie won't work
11231123
# since GUI widgets are gone. Either need to remove extra code to
11241124
# allow for this non-existent use case or find a way to make it work.
1125-
with rc_context():
1126-
if rcParams['savefig.bbox'] == 'tight':
1125+
with mpl.rc_context():
1126+
if mpl.rcParams['savefig.bbox'] == 'tight':
11271127
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
11281128
"frame size to vary, which is inappropriate for "
11291129
"animation.")
1130-
rcParams['savefig.bbox'] = None
1130+
mpl.rcParams['savefig.bbox'] = None
11311131
with writer.saving(self._fig, filename, dpi):
11321132
for anim in all_anim:
11331133
# Clear the initial frame
@@ -1313,7 +1313,7 @@ def to_html5_video(self, embed_limit=None):
13131313
if not hasattr(self, '_base64_video'):
13141314
# Save embed limit, which is given in MB
13151315
if embed_limit is None:
1316-
embed_limit = rcParams['animation.embed_limit']
1316+
embed_limit = mpl.rcParams['animation.embed_limit']
13171317

13181318
# Convert from MB to bytes
13191319
embed_limit *= 1024 * 1024
@@ -1324,9 +1324,9 @@ def to_html5_video(self, embed_limit=None):
13241324
path = Path(tmpdir, "temp.m4v")
13251325
# We create a writer manually so that we can get the
13261326
# appropriate size for the tag
1327-
Writer = writers[rcParams['animation.writer']]
1327+
Writer = writers[mpl.rcParams['animation.writer']]
13281328
writer = Writer(codec='h264',
1329-
bitrate=rcParams['animation.bitrate'],
1329+
bitrate=mpl.rcParams['animation.bitrate'],
13301330
fps=1000. / self._interval)
13311331
self.save(str(path), writer=writer)
13321332
# Now open and base64 encode.
@@ -1385,7 +1385,7 @@ def to_jshtml(self, fps=None, embed_frames=True, default_mode=None):
13851385

13861386
def _repr_html_(self):
13871387
'''IPython display hook for rendering.'''
1388-
fmt = rcParams['animation.html']
1388+
fmt = mpl.rcParams['animation.html']
13891389
if fmt == 'html5':
13901390
return self.to_html5_video()
13911391
elif fmt == 'jshtml':

lib/matplotlib/rcsetup.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import re
2323

2424
import matplotlib as mpl
25-
from matplotlib import cbook
25+
from matplotlib import animation, cbook
2626
from matplotlib.cbook import ls_mapper
2727
from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
2828
from matplotlib.colors import is_color_like
@@ -619,11 +619,16 @@ def validate_hinting(s):
619619
validate_pgf_texsystem = ValidateInStrings('pgf.texsystem',
620620
['xelatex', 'lualatex', 'pdflatex'])
621621

622-
validate_movie_writer = ValidateInStrings('animation.writer',
623-
['ffmpeg', 'ffmpeg_file',
624-
'avconv', 'avconv_file',
625-
'imagemagick', 'imagemagick_file',
626-
'html'])
622+
623+
def validate_movie_writer(s):
624+
# writers.list() would only list actually available writers, but
625+
# FFMpeg.isAvailable is slow and not worth paying for at every import.
626+
if s in animation.writers._registered:
627+
return s
628+
else:
629+
raise ValueError(f"Supported animation writers are "
630+
f"{sorted(animation.writers._registered)}")
631+
627632

628633
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
629634
['png', 'jpeg', 'tiff', 'raw', 'rgba'])

0 commit comments

Comments
 (0)