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

Skip to content

Commit 522fd3b

Browse files
authored
Merge pull request #13936 from anntzer/pathlibanimation
MNT: Pathlibify animation.
2 parents e8c0b5e + d59aa00 commit 522fd3b

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

lib/matplotlib/animation.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
from io import BytesIO, TextIOWrapper
2323
import itertools
2424
import logging
25-
import os
2625
from pathlib import Path
27-
import platform
2826
import shutil
2927
import subprocess
3028
import sys
@@ -44,7 +42,7 @@
4442
# Process creation flag for subprocess to prevent it raising a terminal
4543
# window. See for example:
4644
# https://stackoverflow.com/questions/24130623/using-python-subprocess-popen-cant-prevent-exe-stopped-working-prompt
47-
if platform.system() == 'Windows':
45+
if sys.platform == 'win32':
4846
subprocess_creation_flags = CREATE_NO_WINDOW = 0x08000000
4947
else:
5048
# Apparently None won't work here
@@ -469,7 +467,7 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
469467
self.clear_temp = clear_temp
470468
self.temp_prefix = frame_prefix
471469
self._frame_counter = 0 # used for generating sequential file names
472-
self._temp_names = list()
470+
self._temp_paths = list()
473471
self.fname_format_str = '%s%%07d.%s'
474472

475473
@property
@@ -495,17 +493,17 @@ def _base_temp_name(self):
495493
def _frame_sink(self):
496494
# Creates a filename for saving using the basename and the current
497495
# counter.
498-
fname = self._base_temp_name() % self._frame_counter
496+
path = Path(self._base_temp_name() % self._frame_counter)
499497

500498
# Save the filename so we can delete it later if necessary
501-
self._temp_names.append(fname)
502-
_log.debug('FileMovieWriter.frame_sink: saving frame %d to fname=%s',
503-
self._frame_counter, fname)
499+
self._temp_paths.append(path)
500+
_log.debug('FileMovieWriter.frame_sink: saving frame %d to path=%s',
501+
self._frame_counter, path)
504502
self._frame_counter += 1 # Ensures each created name is 'unique'
505503

506504
# This file returned here will be closed once it's used by savefig()
507505
# because it will no longer be referenced and will be gc-ed.
508-
return open(fname, 'wb')
506+
return open(path, 'wb')
509507

510508
def grab_frame(self, **savefig_kwargs):
511509
'''
@@ -532,10 +530,10 @@ def cleanup(self):
532530

533531
# Delete temporary files
534532
if self.clear_temp:
535-
_log.debug('MovieWriter: clearing temporary fnames=%s',
536-
self._temp_names)
537-
for fname in self._temp_names:
538-
os.remove(fname)
533+
_log.debug('MovieWriter: clearing temporary paths=%s',
534+
self._temp_paths)
535+
for path in self._temp_paths:
536+
path.unlink()
539537

540538

541539
@writers.register('pillow')
@@ -770,10 +768,10 @@ def _args(self):
770768

771769
# Taken directly from jakevdp's JSAnimation package at
772770
# http://github.com/jakevdp/JSAnimation
773-
def _included_frames(frame_list, frame_format):
774-
"""frame_list should be a list of filenames"""
775-
return INCLUDED_FRAMES.format(Nframes=len(frame_list),
776-
frame_dir=os.path.dirname(frame_list[0]),
771+
def _included_frames(paths, frame_format):
772+
"""paths should be a list of Paths"""
773+
return INCLUDED_FRAMES.format(Nframes=len(paths),
774+
frame_dir=paths[0].parent,
777775
frame_format=frame_format)
778776

779777

@@ -816,8 +814,8 @@ def __init__(self, fps=30, codec=None, bitrate=None, extra_args=None,
816814
super().__init__(fps, codec, bitrate, extra_args, metadata)
817815

818816
def setup(self, fig, outfile, dpi, frame_dir=None):
819-
root, ext = os.path.splitext(outfile)
820-
if ext not in ['.html', '.htm']:
817+
outfile = Path(outfile)
818+
if outfile.suffix not in ['.html', '.htm']:
821819
raise ValueError("outfile must be *.htm or *.html")
822820

823821
self._saved_frames = []
@@ -826,10 +824,9 @@ def setup(self, fig, outfile, dpi, frame_dir=None):
826824

827825
if not self.embed_frames:
828826
if frame_dir is None:
829-
frame_dir = root + '_frames'
830-
if not os.path.exists(frame_dir):
831-
os.makedirs(frame_dir)
832-
frame_prefix = os.path.join(frame_dir, 'frame')
827+
frame_dir = outfile.with_name(outfile.stem + '_frames')
828+
frame_dir.mkdir(parents=True, exist_ok=True)
829+
frame_prefix = frame_dir / 'frame'
833830
else:
834831
frame_prefix = None
835832

@@ -866,9 +863,8 @@ def finish(self):
866863
Nframes = len(self._saved_frames)
867864
else:
868865
# temp names is filled by FileMovieWriter
869-
fill_frames = _included_frames(self._temp_names,
870-
self.frame_format)
871-
Nframes = len(self._temp_names)
866+
fill_frames = _included_frames(self._temp_paths, self.frame_format)
867+
Nframes = len(self._temp_paths)
872868
mode_dict = dict(once_checked='',
873869
loop_checked='',
874870
reflect_checked='')

0 commit comments

Comments
 (0)