22
22
from io import BytesIO , TextIOWrapper
23
23
import itertools
24
24
import logging
25
- import os
26
25
from pathlib import Path
27
- import platform
28
26
import shutil
29
27
import subprocess
30
28
import sys
44
42
# Process creation flag for subprocess to prevent it raising a terminal
45
43
# window. See for example:
46
44
# 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 ' :
48
46
subprocess_creation_flags = CREATE_NO_WINDOW = 0x08000000
49
47
else :
50
48
# Apparently None won't work here
@@ -469,7 +467,7 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
469
467
self .clear_temp = clear_temp
470
468
self .temp_prefix = frame_prefix
471
469
self ._frame_counter = 0 # used for generating sequential file names
472
- self ._temp_names = list ()
470
+ self ._temp_paths = list ()
473
471
self .fname_format_str = '%s%%07d.%s'
474
472
475
473
@property
@@ -495,17 +493,17 @@ def _base_temp_name(self):
495
493
def _frame_sink (self ):
496
494
# Creates a filename for saving using the basename and the current
497
495
# counter.
498
- fname = self ._base_temp_name () % self ._frame_counter
496
+ path = Path ( self ._base_temp_name () % self ._frame_counter )
499
497
500
498
# 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 )
504
502
self ._frame_counter += 1 # Ensures each created name is 'unique'
505
503
506
504
# This file returned here will be closed once it's used by savefig()
507
505
# because it will no longer be referenced and will be gc-ed.
508
- return open (fname , 'wb' )
506
+ return open (path , 'wb' )
509
507
510
508
def grab_frame (self , ** savefig_kwargs ):
511
509
'''
@@ -532,10 +530,10 @@ def cleanup(self):
532
530
533
531
# Delete temporary files
534
532
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 ( )
539
537
540
538
541
539
@writers .register ('pillow' )
@@ -770,10 +768,10 @@ def _args(self):
770
768
771
769
# Taken directly from jakevdp's JSAnimation package at
772
770
# 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 ,
777
775
frame_format = frame_format )
778
776
779
777
@@ -816,8 +814,8 @@ def __init__(self, fps=30, codec=None, bitrate=None, extra_args=None,
816
814
super ().__init__ (fps , codec , bitrate , extra_args , metadata )
817
815
818
816
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' ]:
821
819
raise ValueError ("outfile must be *.htm or *.html" )
822
820
823
821
self ._saved_frames = []
@@ -826,10 +824,9 @@ def setup(self, fig, outfile, dpi, frame_dir=None):
826
824
827
825
if not self .embed_frames :
828
826
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'
833
830
else :
834
831
frame_prefix = None
835
832
@@ -866,9 +863,8 @@ def finish(self):
866
863
Nframes = len (self ._saved_frames )
867
864
else :
868
865
# 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 )
872
868
mode_dict = dict (once_checked = '' ,
873
869
loop_checked = '' ,
874
870
reflect_checked = '' )
0 commit comments