8000 Deprecate setup, finish and cleanup; saving takes kwargs. · matplotlib/matplotlib@1d010d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d010d3

Browse files
committed
Deprecate setup, finish and cleanup; saving takes kwargs.
The old setup, finish and cleanup methods interact poorly with the context-manager approach anyways so the saving context-manager now calls _setup, _finish and _cleanup. The saving context-manager now supports keyword-arguments, e.g. clear_temp. Do not overload the _temp_names attribute anymore. Minor layout changes.
1 parent f10180a commit 1d010d3

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

lib/matplotlib/animation.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import shutil
3030
import sys
3131
import tempfile
32-
from matplotlib.cbook import iterable, is_string_like
32+
import warnings
33+
from matplotlib.cbook import iterable, is_string_like, mplDeprecation
3334
from matplotlib.compat import subprocess
3435
from matplotlib import verbose
3536
from matplotlib import rcParams
@@ -77,11 +78,10 @@ def __getitem__(self, name):
7778

7879
class MovieWriter(object):
7980
'''
80-
Base class for writing movies. Fundamentally, what a MovieWriter does
81-
is provide is a way to grab frames by calling grab_frame(). setup()
82-
is called to start the process and finish() is called afterwards.
83-
This class is set up to provide for writing movie frame data to a pipe.
84-
saving() is provided as a context manager to facilitate this process as::
81+
Base class for writing movies. Fundamentally, what a MovieWriter does is
82+
provide is a way to grab frames by calling grab_frame(). This class is set
83+
up to provide for writing movie frame data to a pipe. saving() is provided
84+
as a context manager to facilitate this process as::
8585
8686
with moviewriter.saving('myfile.mp4'):
8787
# Iterate over frames
@@ -147,7 +147,7 @@ def frame_size(self):
147147
width_inches, height_inches = self.fig.get_size_inches()
148148
return width_inches * self.dpi, height_inches * self.dpi
149149

150-
def setup(self, fig, outfile, dpi, *args):
150+
def setup(self, *args, **kwargs):
151151
'''
152152
Perform setup for writing the movie file.
153153
@@ -159,28 +159,33 @@ def setup(self, fig, outfile, dpi, *args):
159159
The DPI (or resolution) for the file. This controls the size
160160
in pixels of the resulting movie file.
161161
'''
162+
warnings.warn('setup interacts poorly with the saving context-manager',
163+
mplDeprecation)
164+
self._setup(*args, **kwargs)
165+
166+
def _setup(self, outfile, dpi):
162167
self.outfile = outfile
163168
self.fig = fig
164169
self.dpi = dpi
165-
166170
# Run here so that grab_frame() can write the data to a pipe. This
167171
# eliminates the need for temp files.
168172
self._run()
169173

170174
@contextlib.contextmanager
171-
def saving(self, *args):
175+
def saving(self, *args, **kwargs):
172176
'''
173177
Context manager to facilitate writing the movie file.
174178
175-
``*args`` are any parameters that should be passed to `setup`.
179+
``*args`` and ``**kwargs`` are any parameters that should be passed to
180+
`setup`.
176181
'''
177182
# This particular sequence is what contextlib.contextmanager wants
178-
self.setup(*args)
183+
self._setup(*args, **kwargs)
179184
try:
180185
yield
181-
self.finish()
186+
self._finish()
182187
finally:
183-
self.cleanup()
188+
self._cleanup()
184189

185190
def _run(self):
186191
# Uses subprocess to call the program for assembling frames into a
@@ -199,6 +204,12 @@ def _run(self):
199204

200205
def finish(self):
201206
'Finish any processing for writing the movie.'
207+
warnings.warn('finish interacts poorly with the saving context-manager',
208+
mplDeprecation)
209+
self.cleanup()
210+
211+
def _finish(self):
212+
'Finish any processing for writing the movie.'
202213

203214
def grab_frame(self, **savefig_kwargs):
204215
'''
@@ -229,6 +240,12 @@ def _args(self):
229240
return NotImplementedError("args needs to be implemented by subclass.")
230241

231242
def cleanup(self):
243+
'Clean-up and collect the process used to write the movie file.'
244+
warnings.warn('cleanup interacts poorly with the saving context-manager',
245+
mplDeprecation)
246+
self._cleanup()
247+
248+
def _cleanup(self):
232249
'Clean-up and collect the process used to write the movie file.'
233250
if hasattr(self, "_proc"):
234251
out, err = self._proc.communicate()
@@ -269,8 +286,8 @@ def __init__(self, *args, **kwargs):
269286
MovieWriter.__init__(self, *args, **kwargs)
270287
self.frame_format = rcParams['animation.frame_format']
271288

272-
def setup(self, fig, outfile, dpi, frame_prefix='_tmp', clear_temp=True,
273-
tmpdir=None):
289+
def _setup(self, fig, outfile, dpi, frame_prefix='_tmp', clear_temp=True,
290+
tmpdir=None):
274291
'''
275292
Perform setup for writing the movie file.
276293
@@ -297,12 +314,13 @@ def setup(self, fig, outfile, dpi, frame_prefix='_tmp', clear_temp=True,
297314
self.temp_prefix = frame_prefix
298315
self._frame_counter = 0 # used for generating sequential file names
299316
if tmpdir is None:
300-
self._tmpdir = self._temp_names = tempfile.mkdtemp()
317+
self._tmpdir = tempfile.mkdtemp()
318+
self._temp_names = None
301319
else:
302320
self._tmpdir = tmpdir
303321
self._temp_names = list()
304-
self.fname_format_str = os.path.join(
305-
self._tmpdir.replace('%', '%%'), '%s%%07d.%s')
322+
self.fname_format_str = os.path.join(self._tmpdir.replace('%', '%%'),
323+
'%s%%07d.%s')
306324

307325
@property
308326
def frame_format(self):
@@ -366,11 +384,11 @@ def grab_frame(self, **savefig_kwargs):
366384
err), level='helpful')
367385
raise
368386

369-
def finish(self):
387+
def _finish(self):
370388
# Call run here now that all frame grabbing is done. All temp files
371389
# are available to be assembled.
372390
self._run()
373-
MovieWriter.finish(self)
391+
MovieWriter._finish(self)
374392

375393
# Check error code for creating file here, since we just run
376394
# the process here, rather than having an open pipe.
@@ -379,20 +397,23 @@ def finish(self):
379397
+ str(self._proc.returncode)
380398
+ ' Try running with --verbose-debug')
381399

382-
def cleanup(self):
383-
MovieWriter.cleanup(self)
400+
def _cleanup(self):
401+
MovieWriter._cleanup(self)
384402

385403
#Delete temporary files
386404
if self.clear_temp:
387-
verbose.report(
388-
'MovieWriter: clearing temporary fnames=%s' %
389-
str(self._temp_names),
405+
if self._temp_names is None: # tmpdir created with mkdtemp
406+
verbose.report(
407+
'MovieWriter: clearing temporary fnames=%s' % self._tmpdir,
408+
level='debug')
409+
shutil.rmtree(self._tmpdir)
410+
else:
411+
verbose.report(
412+
'MovieWriter: clearing temporary fnames=%s' %
413+
self._temp_names,
390414
level='debug')
391-
if isinstance(self._temp_names, list):
392415
for fname in self._temp_names:
393416
os.remove(fname)
394-
else:
395-
shutil.rmtree(self._temp_names)
396417

397418

398419
# Base class of ffmpeg information. Has the config keys and the common set

0 commit comments

Comments
 (0)
0