o
    :a2                  
   @   s  d dl Z d dlmZ d dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Zd dl
mZ d dl
mZ e	 dd ZG dd dejZd	d
 Ze	jjdeedgdgddd Zdd ZejdG dd deZg dZdd Ze	jde e	jjdeedgdgddd Ze	jde	jde	jj ej!"  ddde	jde	jj ej#"  d ddge	jd!g d"e	jjdeedgdgdd#d$ Z$e	jjdee%e&d%d&gdgdd'd( Z'd)d* Z(e	jd+e	jd,e	jj ej)ej*d-  d.ddd/ge	jjded0d&gdgdd1d2 Z+e	jd+e	jd,e	jj ej)ej*d-  d.ddd/ge	jjded0d&gdgdd3d4 Z,e	jj e j-d5kd6dd7d8 Z.e	jd9d:d;gd<d= Z/e	jd>g d?d@dA Z0dBdC Z1dDdE Z2dS )F    N)Path)pyplot)	animationc                    s   t  \}}|g g \ |dd |dd  fdd} fdd}tt| d	i }|d
tj	}d|vr<d|d< |d|||d|S )z)Create a simple animation (with options).r   
      c                           g g   fS Nset_data liner   A/usr/lib/python3/dist-packages/matplotlib/tests/test_animation.pyinit      zanim.<locals>.initc                    s.   t ddd}t ||  } ||  fS )Nr   r   d   )nplinspaceZsinr   )ixyr   r   r   animate   s   zanim.<locals>.animateparamklassframes   )figfunc	init_funcNr   )
pltsubplotsplotZset_xlimZset_ylimdictgetattrpopr   FuncAnimation)Zrequestr   axr   r   kwargsr   r   r   r   anim   s   r)   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	NullMovieWritera  
    A minimal MovieWriter.  It doesn't actually write anything.
    It just saves the arguments that were given to the setup() and
    grab_frame() methods as attributes, and counts how many times
    grab_frame() is called.

    This class doesn't have an __init__ method with the appropriate
    signature, and it doesn't define an isAvailable() method, so
    it cannot be added to the 'writers' registry.
    c                 G   s"   || _ || _|| _|| _d| _d S )Nr   )r   outfiledpiargs_count)selfr   r+   r,   r-   r   r   r   setup7   s
   
zNullMovieWriter.setupc                 K   s   || _ |  jd7  _d S )Nr   )savefig_kwargsr.   )r/   r1   r   r   r   
grab_frame>   s   zNullMovieWriter.grab_framec                 C      d S r	   r   r/   r   r   r   finishB      zNullMovieWriter.finishN)__name__
__module____qualname____doc__r0   r2   r5   r   r   r   r   r*   +   s
    r*   c                 C   s   d}d}t dd}t }| j||||d |jtdksJ |j|ks&J |j|ks-J |jdks4J |j	|ks;J |j
| jksCJ d S )Nunused.null2   r   )Zfoo)r,   writerr1   r   r   )r#   r*   saver   r    figurer+   r,   r-   r1   r.   Z
save_count)r)   filenamer,   r1   r=   r   r   r   test_null_movie_writerF   s   

rA   )r   )Zindirectc                 C   sf   t  dkrtj  tjdi | } tjt	dd ~ tj  W d    d S 1 s,w   Y  d S )NPyPyzAnimation was deletedmatchr   )
platformpython_implementationr   testingbreak_cyclesr   r&   pytestwarnsWarningr)   r   r   r   test_animation_deleteX   s   
"rM   c                  C   s^   G dd dt j} t }d}d}d}d}dg}| ||||}||| |j|jks-J d S )Nc                   @   s   e Zd Zdd ZdS )z7test_movie_writer_dpi_default.<locals>.DummyMovieWriterc                 S   r3   r	   r   r4   r   r   r   _rung   r6   z<test_movie_writer_dpi_default.<locals>.DummyMovieWriter._runN)r7   r8   r9   rN   r   r   r   r   DummyMovieWriterf   s    rO   r;   r   Zunusedr   )r   ZMovieWriterr    r?   r0   r,   )rO   r   r@   fpscodecbitrate
extra_argsr=   r   r   r   test_movie_writer_dpi_defaulte   s   rT   nullc                   @   s&   e Zd Z		dddZedd ZdS )RegisteredNullMovieWriterNc                 C   r3   r	   r   )r/   rP   rQ   rR   rS   Zmetadatar   r   r   __init__      z"RegisteredNullMovieWriter.__init__c                 C   s   dS )NTr   )clsr   r   r   isAvailable   rX   z%RegisteredNullMovieWriter.isAvailable)NNNNN)r7   r8   r9   rW   classmethodrZ   r   r   r   r   rV   x   s    	
rV   )	)ffmpeg	movie.mp4)Zffmpeg_filer]   )Zavconvr]   )Zavconv_filer]   )imagemagick	movie.gif)Zimagemagick_filer_   )pillowr_   )htmlz
movie.html)rU   z
movie.nullc                  c   s    t D ]J\} }tj| s0tjd|  d}tj| d ||gdV  tj| d t||gdV  qtj|  }t	|dd gD ]}| ||fV  | |t|fV  q<qd S )Nzwriter 'z' not available on this systemZmarksZsupported_formats)
WRITER_OUTPUTr   writersis_availablerI   markskipr   r   r$   )r=   outputrf   Zwriter_classframe_formatr   r   r   gen_writers   s   

rj   zwriter, frame_format, outputc              	   C   s   |d ur	|t jd< tjd	i |}d }d }|dkr#|jd d}d}|   |j|d|d||d W d    ~d S 1 s?w   Y  ~d S )
Nzanimation.frame_formatr\   )g33333%@gQk"@g      Y@Zh264   i  )rP   r=   rR   r,   rQ   r   )r    rcParamsr   r&   Z_figZset_size_inchesas_cwdr>   )tmpdirr=   ri   rh   r)   r,   rQ   r   r   r   test_save_animation_smoketest   s"   


ro   r=   r\   Requires FFMpeg)reasonrb   r^   zRequires ImageMagickz
html, want))ZnoneN)html5z<video width)Zjshtmlz<script c                 C   s   t  dkrtj  | dkr|dkrtj st	d tj
di |}t| |d | }W d    n1 s<w   Y  |d u ri|d u sKJ tt ~tj  W d    d S 1 sbw   Y  d S ||v soJ d S )NrB   r^   rr   rp   )animation.writerzanimation.htmlr   )rE   rF   r   rG   rH   r   FFMpegWriterrZ   rI   rg   r&   r    
rc_contextZ_repr_html_rJ   UserWarning)r=   ra   Zwantr)   r   r   r   test_animation_repr_html   s&   



"rw   r   )r   c                 C   s   | j dt d d S )Nr;   r=   )r>   r*   rL   r   r   r   test_no_length_frames   s   ry   c                  C   s^   t tjjdks
J dtjd< tjdrJ tjdkrdnd} | tjd< tjds-J d S )Nr   Znot_available_ever_xxxxzanimation.ffmpeg_pathr\   win32truewhere)	lenr   rd   Z_registeredmplrl   re   sysrE   )binr   r   r   test_movie_writer_registry   s   

r   method_nameZto_html5_videors   zanimation writer not installedZ	to_jshtmlr   c              	   C   s   | d | % tddi t||   W d    n1 s"w   Y  W d    n1 s1w   Y  t|jdks?J |j\}|jdkrM|jdksOJ d S )NZWARNINGzanimation.embed_limitgư>r   zmatplotlib.animation)	Z	set_levelrm   r~   ru   r$   r}   ZrecordsnameZ	levelname)r   Zcaplogrn   r)   recordr   r   r   test_embed_limit   s   



r   c                 C   sV   |   t||   ttt| g ksJ W d    d S 1 s$w   Y  d S r	   )rm   r$   listr   strZiterdir)r   rn   r)   r   r   r   test_cleanup_temporaries  s   
"r   posixzrequires a POSIX OSc              	   C   s   |   K |ddtjd   tt| d}|d tt|d t	t
j |d W d   n1 s:w   Y  W d   dS W d   dS 1 sRw   Y  dS )a!  
    Test that we correctly raise a CalledProcessError when ffmpeg fails.

    To do so, mock ffmpeg using a simple executable shell script that
    succeeds when called with no arguments (so that it gets registered by
    `isAvailable`), but fails otherwise, and add it to the $PATH.
    PATHz.:r\   z#!/bin/sh
[[ $@ -eq 0 ]]
i  z	test.mpegN)rm   Zsetenvosenvironr   r   Z
write_textchmodrI   raises
subprocessZCalledProcessErrorr>   )rn   Zmonkeypatchr)   Zexe_pathr   r   r   test_failing_ffmpeg  s   
	
"r   cache_frame_dataFTc           	         s   t  \}}|g g \G dd dt fdd}fdd}g  fdd}tj||||| d	}t }|jd
|d tdksFJ t	j
  D ]}| d u | ksXJ qMd S )Nc                   @   s   e Zd ZdS )z2test_funcanimation_cache_frame_data.<locals>.FrameN)r7   r8   r9   r   r   r   r   Frame0  s    r   c                      r   r	   r
   r   r   r   r   r   4  r   z1test_funcanimation_cache_frame_data.<locals>.initc                    s     | d | d   fS )Nr   r   r
   framer   r   r   r   8  s   z4test_funcanimation_cache_frame_data.<locals>.animatec                  3   sP    t dD ] } tddd}tjd} ||d}t| |V  qd S )Nr   r   r   r   )r   r   )ranger   r   ZrandomZrandappendweakrefref)_r   r   r   )r   frames_generatedr   r   frames_generator>  s   z=test_funcanimation_cache_frame_data.<locals>.frames_generator)r   r   r   r;   rx   r   )r    r!   r"   r#   r   r&   r*   r>   r}   r   rG   rH   )	r   r   r'   r   r   r   r)   r=   fr   )r   r   r   r   #test_funcanimation_cache_frame_data+  s$   

r   return_value)Nstringr   )r   artistc                    sf   t  \}}|g \  fdd}tt tj||dd W d    d S 1 s,w   Y  d S )Nc                    s$     ddgd| g dkr S S )Nr   r   r   r
   )r   r   r   r   r   r   k  s   z test_draw_frame.<locals>.animateT)Zblit)r    r!   r"   rI   r   RuntimeErrorr   r&   )r   r   r'   r   r   r   r   test_draw_frameY  s   	"r   c                 C   s   t  \}}dd }tj||ttdddd}|   |jddd W d    n1 s.w   Y  tj	t
d	d
 |  W d    d S 1 sJw   Y  d S )Nc                 S      g S r	   r   r   r   r   r   update{  r6   z(test_exhausted_animation.<locals>.updater   Fr   repeatr   ztest.gifr`   rx   	exhaustedrC   )r    r!   r   r&   iterr   rm   r>   rI   rJ   rv   _startrn   r   r'   r   r)   r   r   r   test_exhausted_animationx  s   

"r   c                 C   sd   t  \}}dd }tj||g ddd}tjtdd |  W d    d S 1 s+w   Y  d S )Nc                 S   r   r	   r   r   r   r   r   r     r6   z%test_no_frame_warning.<locals>.updateFr   r   rC   )r    r!   r   r&   rI   rJ   rv   r   r   r   r   r   test_no_frame_warning  s   
"r   )3r   Zpathlibr   rE   r   r   r   Znumpyr   rI   Z
matplotlibr~   r   r    r   Zfixturer)   ZAbstractMovieWriterr*   rA   rf   Zparametrizer#   rM   rT   rd   registerrV   rc   rj   ro   r   Zskipifrt   rZ   ZImageMagickWriterrw   r   r   ry   r   re   rl   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s    









-
