ó
,BSc           @   sÁ  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d d d d d	 d
 g Z y
 e Z	 Wn e
 k
 rŠ e e f Z	 n Xe j d d k Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j Z d „  Z d „  Z e j d e d e ƒ Z d „  Z e j d ƒ Z e j d ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d „ Z  e e! d „ Z" d e# f d „  ƒ  YZ$ d „  Z% i d d 6Z& e' d k r½d d l Z e j( ƒ  n  d S(   sÑ  
lxml-based doctest output comparison.

Note: normally, you should just import the `lxml.usedoctest` and
`lxml.html.usedoctest` modules from within a doctest, instead of this
one::

    >>> import lxml.usedoctest # for XML output

    >>> import lxml.html.usedoctest # for HTML output

To use this module directly, you must call ``lxmldoctest.install()``,
which will cause doctest to use this in all subsequent calls.

This changes the way output is checked and comparisons are made for
XML or HTML-like content.

XML or HTML content is noticed because the example starts with ``<``
(it's HTML if it starts with ``<html``).  You can also use the
``PARSE_HTML`` and ``PARSE_XML`` flags to force parsing.

Some rough wildcard-like things are allowed.  Whitespace is generally
ignored (except in attributes).  In text (attributes and text in the
body) you can use ``...`` as a wildcard.  In an example it also
matches any trailing tags in the element, though it does not match
leading tags.  You may create a tag ``<any>`` or include an ``any``
attribute in the tag.  An ``any`` tag matches any tag, while the
attribute matches any and all attributes.

When a match fails, the reformatted example and gotten text is
displayed (indented), and a rough diff-like output is given.  Anything
marked with ``-`` is in the output but wasn't supposed to be, and
similarly ``+`` means its in the example but wasn't in the output.

You can disable parsing on one line with ``# doctest:+NOPARSE_MARKUP``
iÿÿÿÿ(   t   etreeNt
   PARSE_HTMLt	   PARSE_XMLt   NOPARSE_MARKUPt   LXMLOutputCheckert   LHTMLOutputCheckert   installt   temp_installi    i   c         C   s   |  d  k r d  S|  j ƒ  Sd  S(   N(   t   Nonet   strip(   t   v(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR	   <   s    c         C   s   t  j d |  ƒ S(   Nt    (   t   _norm_whitespace_ret   sub(   R
   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   norm_whitespaceB   s    t   recovert   remove_blank_textc         C   s   t  j |  t ƒ S(   N(   R    t
   fromstringt   _html_parser(   t   html(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   html_fromstringG   s    s   ^<[^>]+ (at|object) s   [ \t\n][ \t\n]+c        
   B   s³   e  Z d Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 e d „ Z d d „ Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z RS(   t   paramt   imgt   areat   brt   basefontt   inputt   baset   metat   linkt   colc         C   s   t  j S(   N(   R    t   XML(   t   self(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   get_default_parserT   s    c   	      C   sÇ   t  |  d d  ƒ } | d  k	 r0 |  j } | }  n	 t j } |  j | | | ƒ } | sg | |  | | | ƒ Sy | | ƒ } Wn t j k
 rŽ t SXy | | ƒ } Wn t j k
 r¶ t SX|  j	 | | ƒ S(   Nt   _temp_override_self(
   t   getattrR   t   _temp_call_super_check_outputt   OutputCheckert   check_outputt
   get_parserR    t   XMLSyntaxErrort   Falset   compare_docs(	   R    t   wantt   gott   optionflagst   alt_selft   super_methodt   parsert   want_doct   got_doc(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR&   W   s$    			c         C   s§   d  } t | @r d  St | @r' t } n| t | @r= t j } nf | j ƒ  j ƒ  j	 d ƒ rv | j ƒ  j	 d ƒ rv t } n- |  j
 | ƒ r£ |  j
 | ƒ r£ |  j ƒ  } n  | S(   Ns   <html(   R   R   R   R   R   R    R   R	   t   lowert
   startswitht   _looks_like_markupR!   (   R    R+   R,   R-   R0   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR'   l   s    

	
	c         C   s)   | j  ƒ  } | j d ƒ o( t j | ƒ S(   Nt   <(   R	   R4   t   _repr_ret   search(   R    t   s(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR5   |   s    c   
      C   s‹  |  j  | j | j ƒ s t S|  j | j | j t ƒ s; t S|  j | j | j t ƒ sZ t Sd | j k rÞ t | j j	 ƒ  ƒ } t | j j	 ƒ  ƒ } | | k r£ t Sx8 | D]- } |  j | j | | j | t ƒ sª t Sqª Wn  | j d k sù t
 | ƒ r‡t | ƒ } t | ƒ } xs | s | rƒ| s.| r2t S| j d ƒ } | j d ƒ }	 |  j | |	 ƒ sft S| r| j d k rPqqWn  t S(   Nt   anys   ...i    (   t   tag_comparet   tagR)   t   text_comparet   textt   Truet   tailt   attribt   sortedt   keyst   lent   listt   popR*   (
   R    R+   R,   t	   want_keyst   got_keyst   keyt   want_childrent   got_childrent
   want_firstt	   got_first(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR*      s6    #c         C   sˆ   | p	 d } | p d } | rE t  | ƒ j ƒ  } t  | ƒ j ƒ  } n  d t j | ƒ } | j d d ƒ } t j | | ƒ r€ t St Sd  S(   Nt    s   ^%s$s   \.\.\.s   .*(   R   R	   t   ret   escapet   replaceR8   R?   R)   (   R    R+   R,   R	   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR=   ž   s    c         C   s“   | d k r t  St | t ƒ s0 t | t ƒ r: | | k S| pC d } | pO d } | j d ƒ r… | j d ƒ d | j d ƒ d k S| | k Sd  S(   NR:   RN   s   {...}t   }iÿÿÿÿ(   R?   t
   isinstancet   _basestringR4   t   split(   R    R+   R,   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR;   «   s    
$c         C   s±  | j  } |  j | | | ƒ } g  } | d  k	 rÃ y | | ƒ } Wn5 t j k
 rw t j ƒ  d } | j d | ƒ n Xy | | ƒ }	 WqÃ t j k
 r¿ t j ƒ  d } | j d | ƒ qÃ Xn  | d  k sÕ | rt j	 |  | | | ƒ }
 | r| j |
 ƒ d j
 | ƒ S|
 Sn  | t k } g  } | j d ƒ | j |  j | | d ƒ ƒ | j d ƒ | j |  j |	 | d ƒ ƒ | j d ƒ | j |  j | |	 | d ƒ ƒ d j
 | ƒ S(	   Ni   s   In example: %ss   In actual output: %ss   
s	   Expected:i   s   Got:s   Diff:(   R+   R'   R   R    R(   t   syst   exc_infot   appendR%   t   output_differencet   joinR   t
   format_doct   collect_diff(   R    t   exampleR,   R-   R+   R0   t   errorsR1   t   eR2   t   valueR   t
   diff_parts(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRY   ¹   s<    	c         C   s=   | s
 t  S| j |  j k r  t  S| j s5 t | ƒ r9 t  St S(   N(   R)   R<   t
   empty_tagsR>   RD   R?   (   R    t   elR   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   html_empty_tagÚ   s    RN   c         C   s;  g  } t  | ƒ sá | j d | ƒ | j | ƒ | j |  j | ƒ ƒ |  j | | ƒ sœ t | j ƒ rƒ | j |  j | j ƒ ƒ n  | j |  j | ƒ ƒ n  t | j ƒ rÇ | j |  j | j ƒ ƒ n  | j d ƒ d j	 | ƒ S| j d | ƒ | j | ƒ | j |  j | ƒ ƒ |  j | | ƒ så| j d ƒ t | j ƒ r}| j d | ƒ | j |  j | j ƒ ƒ | j d ƒ n  x. | D]& } | j |  j
 | | | d ƒ ƒ q„W| j d | ƒ | j |  j | ƒ ƒ | j d ƒ n  t | j ƒ r.| j d | ƒ | j |  j | j ƒ ƒ | j d ƒ n  d j	 | ƒ S(   NR   s   
RN   i   (   RD   RX   t
   format_tagRd   R	   R>   t   format_textt   format_end_tagR@   RZ   R[   (   R    t   docR   t   indentt   prefixt   partsRc   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR[   ä   s@    $c         C   s5   | d  k r d S| r% | j ƒ  } n  t j | d ƒ S(   NRN   i   (   R   R	   t   cgiRP   (   R    R>   R	   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRf     s
    c         C   s   g  } t  | t j ƒ r d SxF t | j j ƒ  ƒ D]/ \ } } | j d | |  j | t ƒ f ƒ q2 W| sv d | j	 Sd | j	 d j
 | ƒ f S(   Ns   <!--s   %s="%s"s   <%s>s   <%s %s>R   (   RS   R    t   CommentBaseRB   RA   t   itemsRX   Rf   R)   R<   RZ   (   R    Rc   t   attrst   nameR`   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRe     s    "'c         C   s!   t  | t j ƒ r d Sd | j S(   Ns   -->s   </%s>(   RS   R    Rm   R<   (   R    Rc   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRg     s    c         C   s×  g  } t  | ƒ rÐ t  | ƒ rÐ | j d | ƒ | j |  j | | ƒ ƒ |  j | | ƒ s— | j |  j | j | j ƒ ƒ | j |  j | | ƒ ƒ n  | j |  j | j | j ƒ ƒ | j d ƒ d j | ƒ S| j d | ƒ | j |  j | | ƒ ƒ | j d ƒ t	 | j ƒ s%t	 | j ƒ re| j d | ƒ | j |  j | j | j ƒ ƒ | j d ƒ n  t
 | ƒ } t
 | ƒ } xµ | sŒ| r4| sÄ| j |  j | j d ƒ | | d d ƒ ƒ q€n  | sü| j |  j | j d ƒ | | d d ƒ ƒ q€n  | j |  j | j d ƒ | j d ƒ | | d ƒ ƒ q€W| j d | ƒ | j |  j | | ƒ ƒ | j d ƒ t	 | j ƒ sŠt	 | j ƒ rÊ| j d | ƒ | j |  j | j | j ƒ ƒ | j d ƒ n  d j | ƒ S(   NR   s   
RN   i    i   t   -t   +(   RD   RX   t   collect_diff_tagRd   t   collect_diff_textR>   t   collect_diff_end_tagR@   RZ   R	   RE   R[   RF   R\   (   R    R+   R,   R   Ri   Rk   RJ   RK   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR\     sH    ,,-c   	      C   s®  |  j  | j | j ƒ s1 d | j | j f } n	 | j } g  } | j d k p[ d | j k } x¶ t | j j ƒ  ƒ D]Ÿ \ } } | | j k r¼ | r¼ | j d | |  j | t ƒ f ƒ qt | | j k rê |  j | j | | t ƒ } n |  j | t ƒ } | j d | | f ƒ qt W| s~x^ t | j j ƒ  ƒ D]D \ } } | | j k rTq3n  | j d | |  j | t ƒ f ƒ q3Wn  | r d | d j	 | ƒ f } n
 d | } | S(	   Ns   %s (got: %s)R:   s   -%s="%s"s   %s="%s"s   +%s="%s"s   <%s %s>R   s   <%s>(
   R;   R<   RA   RB   Rn   RX   Rf   R)   Rt   RZ   (	   R    R+   R,   R<   Ro   R:   Rp   R`   R>   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRs   E  s*    	"&"*
c         C   s<   | j  | j  k r+ d | j  | j  f } n	 | j  } d | S(   Ns   %s (got: %s)s   </%s>(   R<   (   R    R+   R,   R<   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRu   `  s    	c         C   sO   |  j  | | | ƒ r/ | s d S|  j | | ƒ Sd | | f } |  j | | ƒ S(   NRN   s   %s (got: %s)(   R=   Rf   (   R    R+   R,   R	   R>   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRt   g  s    (
   R   R   R   R   R   s   inputs   baseR   s   linkR   (   t   __name__t
   __module__Rb   R!   R&   R'   R5   R*   R=   R;   RY   R?   Rd   R[   Rf   Re   Rg   R\   Rs   Ru   Rt   (    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR   N   s&    								!
#			&		c           B   s   e  Z d  „  Z RS(   c         C   s   t  S(   N(   R   (   R    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR!   p  s    (   Rv   Rw   R!   (    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR   o  s   c         C   s   |  r t  t _ n	 t t _ d S(   s£   
    Install doctestcompare for all future doctests.

    If html is true, then by default the HTML parser will be used;
    otherwise the XML parser is used.
    N(   R   t   doctestR%   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR   s  s    c   	      C   s­   |  r t  } n t } t ƒ  } | j d } | ƒ  } | j } | | _ t rk | j d j } | j j } n | j d j } | j j } t	 t
 _	 t | | | | | | ƒ d S(   sÁ   
    Use this *inside* a doctest to enable this checker for this
    doctest only.

    If html is true, then by default the HTML parser will be used;
    otherwise the XML parser is used.
    R    t   checkN(   R   R   t   _find_doctest_framet   f_localst   _checkert   _IS_PYTHON_3t   __func__R&   t   im_funcR    Rx   t   _RestoreChecker(	   R   t
   del_modulet   Checkert   framet   dt_selft   checkert   old_checkert
   check_funct   checker_check_func(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR     s"    					
	R€   c           B   sP   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s`   | |  _  | |  _ |  j |  j _ | |  j _ | |  _ | |  _ | |  _ |  j ƒ  |  j	 ƒ  d  S(   N(
   R„   R…   t
   call_superR$   R"   R‡   t
   clone_funcR   t   install_clonet   install_dt_self(   R    R„   R†   t   new_checkerR‡   RŠ   R   (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   __init__§  s    					
c         C   sm   t  r9 |  j j |  _ |  j j |  _ |  j j |  j _ n0 |  j j |  _ |  j j |  _ |  j j |  j _ d  S(   N(   R}   R‡   t   __code__t	   func_codet   __globals__t   func_globalsRŠ   (   R    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR‹   ²  s    c         C   s+   t  r |  j |  j _ n |  j |  j _ d  S(   N(   R}   R   R‡   R   (   R    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   uninstall_clone»  s    c         C   s   |  j  j |  _ |  |  j  _ d  S(   N(   R„   t   _DocTestRunner__record_outcomet	   prev_func(   R    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRŒ   À  s    c         C   s   |  j  |  j _ d  S(   N(   R•   R„   R”   (   R    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   uninstall_dt_selfÃ  s    c         C   sp   |  j  rl d d  l } | j |  j  =d |  j  k rl |  j  j d d ƒ \ } } | j | } t | | ƒ ql n  d  S(   Niÿÿÿÿt   .i   (   R   RV   t   modulest   rsplitt   delattr(   R    RV   t   packaget   modulet   package_mod(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   uninstall_moduleÅ  s    	c         O   sF   |  j  ƒ  |  j ƒ  |  j ` |  j ` |  j | | Ž  } |  j ƒ  | S(   N(   R“   R–   R…   R"   R$   R•   Rž   (   R    t   argst   kwt   result(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   __call__Í  s    

		
c         O   s0   |  j  ƒ  z |  j | | Ž  SWd  |  j ƒ  Xd  S(   N(   R“   R‡   R‹   (   R    RŸ   R    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR‰   Õ  s    
(
   Rv   Rw   RŽ   R‹   R“   RŒ   R–   Rž   R¢   R‰   (    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyR€   ¦  s   								c          C   sZ   d d  l  }  |  j d ƒ } x, | rI | j } d | k r= | S| j } q Wt d ƒ ‚ d  S(   Niÿÿÿÿi   t   BOOMsB   Could not find doctest (only use this function *inside* a doctest)(   RV   t	   _getframeR{   t   f_backt   LookupError(   RV   Rƒ   t   l(    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyRz   Ü  s    		si  
    >>> temp_install()
    >>> print """<xml a="1" b="2">stuff</xml>"""
    <xml b="2" a="1">...</xml>
    >>> print """<xml xmlns="http://example.com"><tag   attr="bar"   /></xml>"""
    <xml xmlns="...">
      <tag attr="..." />
    </xml>
    >>> print """<xml>blahblahblah<foo /></xml>""" # doctest: +NOPARSE_MARKUP, +ELLIPSIS
    <xml>...foo /></xml>
    t   basict   __main__()   t   __doc__t   lxmlR    RV   RO   Rx   Rl   t   __all__t
   basestringRT   t	   NameErrort   strt   bytest   version_infoR}   t   register_optionflagR   R   R   R%   R	   R   t
   HTMLParserR)   R?   R   R   t   compileR7   R   R   R   R   R   R   t   objectR€   Rz   t   __test__Rv   t   testmod(    (    (    s7   /usr/lib/python2.7/dist-packages/lxml/doctestcompare.pyt   <module>$   sD   
				ÿ "'6	
