
    Ib2                         d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
mZ d dlmZ d dlmZmZmZmZ d dlmZ d Z G d	 d
      Z ed      j/                  d      Z G d de
      Zy)    N)join)url_path_join)NotebookTestBaseassert_http_error)write)new_notebooknew_markdown_cellnew_code_cell
new_output)encodebytesc                 0    t        j                  |       yy)zCheck is a command exists.FT)shutilwhich)cmds    R/usr/lib/python3/dist-packages/notebook/nbconvert/tests/test_nbconvert_handlers.py
cmd_existsr      s    ||C     c                   2    e Zd ZdZd ZddZd	dZd Zd Zy)
NbconvertAPIz Wrapper for nbconvert API calls.c                     || _         y )N)request)selfr   s     r   __init__zNbconvertAPI.__init__   s	    r   Nc                 d    | j                  |t        d|      ||      }|j                          |S )N	nbconvert)dataparams)r   r   raise_for_status)r   verbpathbodyr   responses         r   _reqzNbconvertAPI._req   s9    <<k40&   
 	!!#r   c                 D    | j                  dt        |||      d|i      S )NGETdownload)r   )r#   r   )r   formatr    namer&   s        r   	from_filezNbconvertAPI.from_file%   s-    yyfdD A!+H 5  7 	7r   c                 R    t        j                  |      }| j                  d||      S )NPOST)jsondumpsr#   )r   r'   nbmodelr!   s       r   	from_postzNbconvertAPI.from_post)   s#    zz'"yy..r   c                 &    | j                  dd      S )Nr%    )r#   r   s    r   list_formatszNbconvertAPI.list_formats-   s    yy##r   )NN)F)	__name__
__module____qualname____doc__r   r#   r)   r/   r3    r   r   r   r      s    *7/$r   r   sG   PNG

   IHDR     x00   wS   IDATc  \.~d    IENDB`asciic                   
   e Zd Zd Zej
                  j                   ed       d      d        Zej
                  j                   ed       d      d        Z	ej
                  j                   ed       d      d        Z
ej
                  j                   ed       d      d        Zej
                  j                   ed       d      d	        Zej
                  j                   ed       d      d
        Zy)APITestc                    | j                   }t        j                  j                  t	        |d            s5t	        |d      t        j
                         | j                  fd       }t               }|j                  j                  t        d             t        d      }|j                  j                  t        dd             |j                  j                  t        d	d
t        id             |j                  j                  |       t        t	        |dd      dd      5 }t!        ||d       d d d        t#        | j$                        | _        y # 1 sw Y   $xY w)Nfooc                  4    t        j                   d       y )NT)ignore_errors)r   rmtree)subdirs   r   cleanup_dirz"APITest.setUp.<locals>.cleanup_dirA   s    fD9r   u   Created by test ³
print(2*6))sourcestream12)output_typetextexecute_resultz	image/png   )rG   r   execution_counttestnb.ipynbwzutf-8)encoding   )version)notebook_dirosr    isdirpjoinmkdir
addCleanupr   cellsappendr	   r
   outputsr   png_green_pixelopenr   r   r   nbconvert_api)r   nbdirrB   nbcc1frA   s         @r   setUpzAPITest.setUp6   s    !!ww}}U5%015%(FHHV
 __: : ^
)*>?@<0:(FG:2B0
 	 	%un5s%' 	$*+"a#	$ *$,,7		$ 	$s   -EE(pandocz(Pandoc wasn't found. Skipping this test.)reasonc                    | j                   j                  ddd      }| j                  |j                  d       | j	                  d|j
                  d          | j	                  d|j                         | j	                  d|j                         | j                   j                  d	dd      }| j	                  d
|j
                  d          | j	                  d|j                         y )Nhtmlr=   rL      	text/htmlContent-TypeCreated by testprintpythontext/x-pythonrC   )r\   r)   assertEqualstatus_codeassertInheadersrH   r   rs     r   test_from_filezAPITest.test_from_fileV   s    
 ((G,k199^#<='0gqvv&((5.Ioqyy'@AlAFF+r   c                 ~    t        d      5  | j                  j                  ddd       d d d        y # 1 sw Y   y xY w)Ni  re   r=   zthisdoesntexist.ipynb)r   r\   r)   r2   s    r   test_from_file_404zAPITest.test_from_file_404e   s>    
 s# 	Q((8OP	Q 	Q 	Qs   3<c                     | j                   j                  dddd      }|j                  d   }| j                  d|       | j                  d|       y )	Nrk   r=   rL   Tr&   Content-Disposition
attachmentz	testnb.py)r\   r)   rp   ro   )r   rr   content_dispositions      r   test_from_file_downloadzAPITest.test_from_file_downloadm   sR    
 ((5.SW(Xii(=>l$78k#67r   c                     | j                   j                  dddd      }| j                  d|j                  d          | j                  d|j                  d	          y )
Nlatexr=   rL   Trw   application/ziprh   .ziprx   )r\   r)   ro   rp   rq   s     r   test_from_file_zipzAPITest.test_from_file_zipw   sT    
 ((%RV(W'>)BCfaii(=>?r   c                    | j                  dd      j                         }| j                  j                  d|      }| j	                  |j
                  d       | j                  d|j                  d          | j                  d|j                         | j                  d	|j                         | j                  j                  d
|      }| j                  d|j                  d          | j                  d|j                         y )Nr%   api/contents/foo/testnb.ipynbre   r'   r.   rf   rg   rh   ri   rj   rk   rl   rC   )	r   r,   r\   r/   rm   rn   ro   rp   rH   r   r.   rr   s      r   test_from_postzAPITest.test_from_post   s    
 ,,u&EFKKM(((H,k199^#<='0gqvv&(('(Joqyy'@AlAFF+r   c                     | j                  dd      j                         }| j                  j                  d|      }| j	                  d|j
                  d          | j	                  d|j
                  d          y )	Nr%   r   r}   r   r~   rh   r   rx   )r   r,   r\   r/   ro   rp   r   s      r   test_from_post_zipzAPITest.test_from_post_zip   si    
 ,,u&EFKKM(((I'>)BCfaii(=>?r   N)r4   r5   r6   ra   pytestmarkskipifr   rs   ru   r{   r   r   r   r8   r   r   r;   r;   5   sR   8@ [[x  9  	,		, [[x  9  Q	Q [[x  9  8	8 [[x  9  @	@
 [[x  9  ,	, [[x  9  @	@r   r;   )r,   rR   os.pathr   rT   r   r   notebook.utilsr   notebook.tests.launchnotebookr   r   nbformatr   nbformat.v4r   r	   r
   r   base64r   r   r   decoderZ   r;   r8   r   r   <module>r      sl     	 !   ( M   $ $0  T  	&/ 
e@ e@r   