ó
ù¢TQc           @   s‘  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 m Z d  d l m Z d  d l m Z m Z d  d l m Z d  d	 l m Z d  d
 l m Z d  d l m Z d! Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d d „ Z' e( d „ Z) d „  Z* d e f d „  ƒ  YZ+ d „  Z, d „  Z- d  „  Z. d S("   iÿÿÿÿN(   t   parseStringt   Node(   t   settingst   UserSettingsHolder(   t   mail(   t   Templatet   loadert   TemplateDoesNotExist(   t   cached(   t   template_renderedt   setting_changed(   t	   force_str(   t   wraps(   t   six(   t
   deactivatet   Approximatet   ContextListt
   get_runnert   override_settingst   setup_test_environmentt   teardown_test_environmentt!   _original_template_source_loadersc           B   s&   e  Z d  d „ Z d „  Z d „  Z RS(   i   c         C   s   | |  _  | |  _ d  S(   N(   t   valt   places(   t   selfR   R   (    (    s*   ../Django//lib/python/django/test/utils.pyt   __init__   s    	c         C   s   t  |  j ƒ S(   N(   t   reprR   (   R   (    (    s*   ../Django//lib/python/django/test/utils.pyt   __repr__   s    c         C   s6   |  j  | k r t St t |  j  | ƒ |  j ƒ d k S(   Ni    (   R   t   Truet   roundt   absR   (   R   t   other(    (    s*   ../Django//lib/python/django/test/utils.pyt   __eq__    s    (   t   __name__t
   __module__R   R   R    (    (    (    s*   ../Django//lib/python/django/test/utils.pyR      s   	c           B   s    e  Z d  Z d „  Z d „  Z RS(   sk   A wrapper that provides direct key access to context items contained
    in a list of context objects.
    c         C   s`   t  | t j ƒ rF x" |  D] } | | k r | | Sq Wt | ƒ ‚ n t t |  ƒ j | ƒ Sd  S(   N(   t
   isinstanceR   t   string_typest   KeyErrort   superR   t   __getitem__(   R   t   keyt
   subcontext(    (    s*   ../Django//lib/python/django/test/utils.pyR'   *   s    c         C   s%   y |  | Wn t  k
 r  t SXt S(   N(   R%   t   FalseR   (   R   R(   (    (    s*   ../Django//lib/python/django/test/utils.pyt   __contains__3   s
    (   R!   R"   t   __doc__R'   R+   (    (    (    s*   ../Django//lib/python/django/test/utils.pyR   &   s   		c         C   s,   t  j d |  d |  d | ƒ |  j j | ƒ S(   sz   
    An instrumented Template render method, providing a signal
    that can be intercepted by the test system Client
    t   sendert   templatet   context(   R	   t   sendt   nodelistt   render(   R   R/   (    (    s*   ../Django//lib/python/django/test/utils.pyt   instrumented_test_render;   s    c           C   sV   t  j t  _ t t  _ t j t _ d t _ t j t _	 d g t _ g  t _
 t ƒ  d S(   sð   Perform any global pre-test setup. This involves:

        - Installing the instrumented test renderer
        - Set the email backend to the locmem email backend.
        - Setting the active locale to match the LANGUAGE_CODE setting.
    s-   django.core.mail.backends.locmem.EmailBackendt   *N(   R   t   _rendert   original_renderR3   R   t   EMAIL_BACKENDR   t   original_email_backendt   ALLOWED_HOSTSt   _original_allowed_hostst   outboxR   (    (    (    s*   ../Django//lib/python/django/test/utils.pyR   D   s    			c           C   s@   t  j t  _ t  ` t j t _ t ` t j t _ t ` t `	 d S(   s›   Perform any global post-test teardown. This involves:

        - Restoring the original test renderer
        - Restoring the email sending functions

    N(
   R   R6   R5   R   R8   R   R7   R:   R9   R;   (    (    (    s*   ../Django//lib/python/django/test/utils.pyR   Y   s    c           C   s   t  j S(   sG   
    Returns an object containing the state of the warnings module
    (   t   warningst   filters(    (    (    s*   ../Django//lib/python/django/test/utils.pyt   get_warnings_statel   s    c         C   s   |  t  _ d S(   sw   
    Restores the state of the warnings module when passed an object that was
    returned by get_warnings_state()
    N(   R<   R=   (   t   state(    (    s*   ../Django//lib/python/django/test/utils.pyt   restore_warnings_statev   s    c         C   s…   | s |  j  } n  | j d ƒ } t | ƒ d k rI d j | d  ƒ } n d } t | i  i  t | d ƒ ƒ } t | | d ƒ } | S(   Nt   .i   iÿÿÿÿ(   t   TEST_RUNNERt   splitt   lent   joint
   __import__R   t   getattr(   R   t   test_runner_classt	   test_patht   test_module_namet   test_modulet   test_runner(    (    s*   ../Django//lib/python/django/test/utils.pyR   ~   s    c            s   t  t t ƒ r" t d t ƒ ‚ n  d ‡  f d † } | rX t j d ƒ } | f | _ n | } t t t t j	 ƒ | f t _	 | S(   s   
    Changes Django to only find templates from within a dictionary (where each
    key is the template name and each value is the corresponding template
    content to return).

    Use meth:`restore_template_loaders` to restore the original loaders.
    s   loader.%s already existsc            s:   y ˆ  |  d |  f SWn t  k
 r5 t |  ƒ ‚ n Xd S(   s@   A custom template loader that loads templates from a dictionary.s   test:%sN(   R%   R   (   t   template_namet   template_dirs(   t   templates_dict(    s*   ../Django//lib/python/django/test/utils.pyt   test_template_loader˜   s    RP   N(   s   test_template_loader(
   t   hasattrR   t   RESTORE_LOADERS_ATTRt	   Exceptiont   NoneR   t   Loadert   _cached_loaderst   setattrt   template_source_loaders(   RO   t   use_cached_loaderRP   t   template_loader(    (   RO   s*   ../Django//lib/python/django/test/utils.pyt   setup_test_template_loader   s    c           C   s#   t  t t ƒ t _ t t t ƒ d S(   sk   
    Restores the original template loaders after
    :meth:`setup_test_template_loader` has been run.
    N(   RG   R   RR   RX   t   delattr(    (    (    s*   ../Django//lib/python/django/test/utils.pyt   restore_template_loadersª   s    c           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s;  
    Acts as either a decorator, or a context manager. If it's a decorator it
    takes a function and returns a wrapped function. If it's a contextmanager
    it's used with the ``with`` statement. In either event entering/exiting
    are called before and after, respectively, the function/block is executed.
    c         K   s   | |  _  t j |  _ d  S(   N(   t   optionsR   t   _wrappedt   wrapped(   R   t   kwargs(    (    s*   ../Django//lib/python/django/test/utils.pyR   º   s    	c         C   s   |  j  ƒ  d  S(   N(   t   enable(   R   (    (    s*   ../Django//lib/python/django/test/utils.pyt	   __enter__¾   s    c         C   s   |  j  ƒ  d  S(   N(   t   disable(   R   t   exc_typet	   exc_valuet	   traceback(    (    s*   ../Django//lib/python/django/test/utils.pyt   __exit__Á   s    c            s«   d d l  m } t ˆ  t ƒ r‰ t ˆ  | ƒ s= t d ƒ ‚ n  ˆ  j ‰ ˆ  j ‰ ‡ ‡ f d †  } ‡ ‡ f d †  } | ˆ  _ | ˆ  _ ˆ  St ˆ  ƒ ‡  ‡ f d †  ƒ } | S(   Niÿÿÿÿ(   t   SimpleTestCasesP   Only subclasses of Django SimpleTestCase can be decorated with override_settingsc            s   ˆ  j  ƒ  ˆ |  ƒ d  S(   N(   Rb   (   t	   innerself(   R   t   original_pre_setup(    s*   ../Django//lib/python/django/test/utils.pyt
   _pre_setupÎ   s    
c            s   ˆ |  ƒ ˆ  j  ƒ  d  S(   N(   Rd   (   Rj   (   R   t   original_post_teardown(    s*   ../Django//lib/python/django/test/utils.pyt   _post_teardownÒ   s    
c             s   ˆ  ˆ  |  | Ž  SWd  QXd  S(   N(    (   t   argsRa   (   t	   test_funcR   (    s*   ../Django//lib/python/django/test/utils.pyt   innerÙ   s    (	   t   django.testRi   R#   t   typet
   issubclassRS   Rl   Rn   R   (   R   Rp   Ri   Rl   Rn   Rq   (    (   Rp   Rk   R   Rm   s*   ../Django//lib/python/django/test/utils.pyt   __call__Ä   s    				c         C   sŽ   t  t j ƒ } x- |  j j ƒ  D] \ } } t | | | ƒ q W| t _ x? |  j j ƒ  D]. \ } } t j d t j j d | d | ƒ qX Wd  S(   NR-   t   settingt   value(	   R   R   R_   R^   t   itemsRW   R
   R0   t	   __class__(   R   t   overrideR(   t	   new_value(    (    s*   ../Django//lib/python/django/test/utils.pyRb   ß   s    	c         C   sX   |  j  t _ xE |  j D]: } t t | d  ƒ } t j d t j j d | d | ƒ q Wd  S(   NR-   Rv   Rw   (	   R`   R   R_   R^   RG   RT   R
   R0   Ry   (   R   R(   R{   (    (    s*   ../Django//lib/python/django/test/utils.pyRd   è   s
    (	   R!   R"   R,   R   Rc   Rh   Ru   Rb   Rd   (    (    (    s*   ../Django//lib/python/django/test/utils.pyR   ³   s   						c            s  t  j d ƒ ‰ ‡ f d †  ‰ d „  ‰ d „  ‰ ‡ ‡ f d †  ‰ d „  ‰  ‡ ‡  ‡ ‡ f d †  ‰ d „  } t |  | ƒ \ }  } |  j d	 d
 ƒ }  | j d	 d
 ƒ } |  j d ƒ sÑ d } | |  }  | | } n  | t |  ƒ ƒ } | t | ƒ ƒ } ˆ | | ƒ S(   s0  Tries to do a 'xml-comparison' of want and got.  Plain string
    comparison doesn't always work because, for example, attribute
    ordering should not be important. Comment nodes are not considered in the
    comparison.

    Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py
    s   [ \t\n][ \t\n]+c            s   ˆ  j  d |  ƒ S(   Nt    (   t   sub(   t   v(   t   _norm_whitespace_re(    s*   ../Django//lib/python/django/test/utils.pyt   norm_whitespaceù   s    c         S   s8   d j  g  |  j D]! } | j t j k r | j ^ q ƒ S(   Nt    (   RE   t
   childNodest   nodeTypeR   t	   TEXT_NODEt   data(   t   elementt   c(    (    s*   ../Django//lib/python/django/test/utils.pyt
   child_textü   s    c         S   s,   g  |  j  D] } | j t j k r
 | ^ q
 S(   N(   R‚   Rƒ   R   t   ELEMENT_NODE(   R†   R‡   (    (    s*   ../Django//lib/python/django/test/utils.pyt   children   s    c            s   ˆ  ˆ |  ƒ ƒ S(   N(    (   R†   (   R€   Rˆ   (    s*   ../Django//lib/python/django/test/utils.pyt   norm_child_text  s    c         S   s   t  |  j j ƒ  ƒ S(   N(   t   dictt
   attributesRx   (   R†   (    (    s*   ../Django//lib/python/django/test/utils.pyt
   attrs_dict  s    c            s¹   |  j  | j  k r t Sˆ  |  ƒ ˆ  | ƒ k r2 t Sˆ |  ƒ ˆ | ƒ k rN t Sˆ |  ƒ } ˆ | ƒ } t | ƒ t | ƒ k r‚ t Sx0 t | | ƒ D] \ } } ˆ | | ƒ s’ t Sq’ Wt S(   N(   t   tagNameR*   RD   t   zipR   (   t   want_elementt   got_elementt   want_childrent   got_childrent   wantt   got(   R‹   RŽ   t   check_elementRŠ   (    s*   ../Django//lib/python/django/test/utils.pyR—   
  s    c         S   s.   x' |  j  D] } | j t j k r
 | Sq
 Wd  S(   N(   R‚   Rƒ   R   t   COMMENT_NODE(   t   documentt   node(    (    s*   ../Django//lib/python/django/test/utils.pyt
   first_node  s    s   \ns   
s   <?xmls   <root>%s</root>(   t   ret   compilet   strip_quotest   replacet
   startswithR    (   R•   R–   R›   t   wrappert	   want_roott   got_root(    (   RŽ   R   R‹   R€   R—   RŠ   Rˆ   s*   ../Django//lib/python/django/test/utils.pyt   compare_xmlð   s$    				
c         C   sž   d „  } d „  } | |  ƒ rS | | ƒ rS |  j  ƒ  d d !}  | j  ƒ  d d !} nA | |  ƒ r” | | ƒ r” |  j  ƒ  d d !}  | j  ƒ  d d !} n  |  | f S(   s‚   
    Strip quotes of doctests output values:

    >>> strip_quotes("'foo'")
    "foo"
    >>> strip_quotes('"foo"')
    "foo"
    c         S   s@   |  j  ƒ  }  t |  ƒ d k o? |  d |  d k o? |  d d k S(   Ni   i    iÿÿÿÿt   "t   '(   R¥   R¦   (   t   stripRD   (   t   s(    (    s*   ../Django//lib/python/django/test/utils.pyt   is_quoted_string:  s    c         S   sP   |  j  ƒ  }  t |  ƒ d k oO |  d d k oO |  d |  d k oO |  d d k S(	   Ni   i    t   ui   iÿÿÿÿR¥   R¦   (   R¥   R¦   (   R§   RD   (   R¨   (    (    s*   ../Django//lib/python/django/test/utils.pyt   is_quoted_unicode@  s
    i   iÿÿÿÿi   (   R§   (   R•   R–   R©   R«   (    (    s*   ../Django//lib/python/django/test/utils.pyRž   1  s    			c         C   s   |  i t  j r d n d d 6S(   NR   Rª   t   _(   R   t   PY3(   R¨   (    (    s*   ../Django//lib/python/django/test/utils.pyt
   str_prefixO  s    (   s   Approximates   ContextLists
   get_runners   override_settingss   setup_test_environments   teardown_test_environment(/   Rœ   R<   t   xml.dom.minidomR    R   t   django.confR   R   t   django.coreR   t   django.templateR   R   R   t   django.template.loadersR   t   django.test.signalsR	   R
   t   django.utils.encodingR   t   django.utils.functionalR   t   django.utilsR   t   django.utils.translationR   t   __all__RR   t   objectR   t   listR   R3   R   R   R>   R@   RT   R   R*   R[   R]   R   R¤   Rž   R®   (    (    (    s*   ../Django//lib/python/django/test/utils.pyt   <module>   s8    					
			=	A	