ó
§‰]c           @   sá   d  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 Z d d l	 m
 Z
 m Z d d l m Z m Z d d l m Z d d l m Z m Z d e j f d „  ƒ  YZ d	 d
 „ Z d S(   s   Base test class for nbconvertiÿÿÿÿN(   t   Popent   PIPE(   t   v4t   write(   t   TemporaryWorkingDirectory(   t   string_typest   bytes_to_strt	   TestsBasec           B   s}   e  Z d  Z e e e e e e e d „ Z d „  Z d
 d „ Z e	 d „  ƒ Z
 d „  Z d d „ Z d „  Z e d
 d	 „ Z RS(   sP   Base tests class.  Contains useful fuzzy comparison and nbconvert
    functions.c
   
      C   s=  |	 s! | j  ƒ  } | j  ƒ  } n  | rN | j d d ƒ } | j d d ƒ } n  | r{ | j d d ƒ } | j d d ƒ } n  | r¨ | j d d ƒ } | j d d ƒ } n  | rÕ | j d d ƒ } | j d d ƒ } n  | r|  j | d d ƒ } |  j | d d ƒ } n  | s)| j ƒ  } | j ƒ  } n  |  j | | ƒ d S(   s  
        Performs a fuzzy comparison of two strings.  A fuzzy comparison is a
        comparison that ignores insignificant differences in the two comparands.
        The significance of certain differences can be specified via the keyword
        parameters of this method.
        s   
t    t    s   	s     N(   t   stript   replacet   recursive_replacet   lowert   assertEqual(
   t   selft   at   bt   newlines_are_spacest   tabs_are_spacest   fuzzy_spacingt   ignore_spacest   ignore_newlinest   case_sensitivet   leave_padding(    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyt   fuzzy_compare   s,    
c         C   s)   x" | | k r$ | j  | | ƒ } q W| S(   sµ  
        Performs a recursive replacement operation.  Replaces all instances
        of a search string in a text string with a replacement string until
        the search string no longer exists.  Recursion is needed because the
        replacement string may generate additional search strings.

        For example:
           Replace "ii" with "i" in the string "Hiiii" yields "Hii"
           Another replacement cds "Hi" (the desired output)

        Parameters
        ----------
        text : string
            Text to replace in.
        search : string
            String to search for within "text"
        replacement : string
            String to replace "search" with
        (   R   (   R   t   textt   searcht   replacement(    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyR   C   s    c         C   s2   t  ƒ  } | d  k	 r. |  j | d | j ƒn  | S(   Nt   dest(   R   t   Nonet   copy_files_tot   name(   R   t   copy_filenamest   temp_dir(    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyt   create_temp_cwd[   s    	c         G   s(   i  } x | D] } | j  | ƒ q W| S(   N(   t   update(   t   clst	   dict_argst   outcomet   d(    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyt   merge_dictse   s    c         C   sA   t  j ƒ  } t j | d d d ƒ } t | | d ƒ Wd  QXd  S(   Nt   wt   encodings   utf-8i   (   R   t   new_notebookt   iot   openR   (   R   t   patht   nbt   f(    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyt   create_empty_notebookm   s    t   .c         C   s©   t  j j | ƒ s" t  j | ƒ n  |  j ƒ  } xt | D]l } t j t  j j | | ƒ ƒ } | se t ‚ x9 | D]1 } t j	 | t  j j | t  j j
 | ƒ ƒ ƒ ql Wq5 Wd S(   s.   Copy test files into the destination directoryN(   t   osR/   t   isdirt   makedirst   _get_files_patht   globt   joint   AssertionErrort   shutilt   copyfilet   basename(   R   R!   R   t
   files_patht   patternt   filest   match(    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyR   r   s    c         C   sN   |  j  j d ƒ d d !} | j d ƒ t j j t j ƒ } t j j | | Œ S(   NR3   i   iÿÿÿÿR@   (	   t
   __module__t   splitt   appendR4   R/   t   dirnamet	   nbconvertt   __file__R9   (   R   t   namesR/   (    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyR7   }   s    c         C   s  t  j d d g } t  j d k r` t | t ƒ rJ d j | ƒ d | } q‹ d j | | ƒ } n+ t | t ƒ r t j | ƒ } n  | | 7} t | d t	 d t	 d t	 ƒ} | j
 d | ƒ \ } } | j d	 k pÓ | së t t | ƒ ƒ ‚ n  | j d
 d ƒ | j d
 d ƒ f S(   s±  
        Run nbconvert as a shell command, listening for both Errors and
        non-zero return codes. Returns the tuple (stdout, stderr) of
        output produced during the nbconvert run.

        Parameters
        ----------
        parameters : str, list(str)
            List of parameters to pass to IPython.
        ignore_return_code : optional bool (default False)
            Throw an OSError if the return code
        s   -mRF   t   win32R	   t   stdoutt   stderrt   stdint   inputi    t   utf8R   (   t   syst
   executablet   platformt
   isinstanceR   R9   t   shlexRC   R    R   t   communicatet
   returncodet   OSErrorR   t   decode(   R   t
   parameterst   ignore_return_codeRL   t   cmdt   pRJ   RK   (    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyRF   ˆ   s    
N(   t   __name__RB   t   __doc__t   Truet   FalseR   R   R   R#   t   classmethodR)   R2   R   R7   RF   (    (    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyR      s   &	
		iP   c         C   s  xg t  d t |  ƒ | ƒ D]M } |  | | | !} | | | | !} | | k s t d | | | f ƒ ‚ q Wt |  ƒ t | ƒ k r´ t d t |  ƒ t | ƒ |  t | ƒ f ƒ ‚ nJ t |  ƒ t | ƒ k  rþ t d t |  ƒ t | ƒ |  t | ƒ f ƒ ‚ n  d S(   sš   assert that large strings are equal

    Zooms in on first chunk that differs,
    to give better info than vanilla assertEqual for large text blobs.
    i    s   [offset: %i]
%r != 
%rs.   Length doesn't match (%i > %i). Extra text:
%rs.   Length doesn't match (%i < %i). Extra text:
%rN(   t   ranget   lenR:   (   R   R   t
   chunk_sizet   it   chunk_at   chunk_b(    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyt   assert_big_text_equal¦   s    ),(   R]   R-   R4   R8   RS   R;   RO   t   unittestRF   t
   subprocessR    R   t   nbformatR   R   t   testpath.tempdirR   t   ipython_genutils.py3compatR   R   t   TestCaseR   Rg   (    (    (    s3   lib/python2.7/site-packages/nbconvert/tests/base.pyt   <module>   s   