ó
öÀ„\c           @   sï  d  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 d d l	 m
 Z
 d d l m Z m Z d d l m Z y d d l m Z Wn' e k
 rÉ d	 e f d
 „  ƒ  YZ n Xd d l m Z d d l m Z d d l m Z m Z m Z d d l m Z m Z m Z m  Z  m! Z! m" Z" d d l# m$ Z$ e% r›d d l& m' Z' m( Z( m) Z) m* Z* m+ Z+ m, Z, d d l- m. Z. d d l/ m0 Z0 n  e j1 e2 ƒ Z3 d e j4 f d „  ƒ  YZ5 d „  Z6 d e f d „  ƒ  YZ7 d „  Z8 d S(   sÙ   
    sphinx.builders.linkcheck
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    The CheckExternalLinksBuilder class.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iÿÿÿÿN(   t   path(   t   nodes(   t	   HTTPError(   t   queuet   html_parser(   t   unquote(   t   HTMLParseErrorR   c           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyR      s   (   t   Builder(   t   __(   t
   encode_urit   requestst   logging(   t   purplet   redt	   darkgreent   darkgrayt   darkredt	   turquoise(   t   is_ssl_error(   t   Anyt   Dictt   Listt   Sett   Tuplet   Union(   t   Sphinx(   t   Responset   AnchorCheckParserc           B   s    e  Z d  Z d „  Z d „  Z RS(   s9   Specialized HTML parser that looks for a specific anchor.c         C   s&   t  j j |  ƒ | |  _ t |  _ d  S(   N(   R   t
   HTMLParsert   __init__t   search_anchort   Falset   found(   t   selfR    (    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyR   6   s    	c         C   sC   x< | D]4 \ } } | d k r | |  j  k r t |  _ Pq q Wd  S(   Nt   idt   name(   R$   R%   (   R    t   TrueR"   (   R#   t   tagt   attrst   keyt   value(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   handle_starttag=   s    	(   R   R   t   __doc__R   R+   (    (    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyR   3   s   	c         C   sr   t  | ƒ } yK x: |  j d d d t ƒ D]  } | j | ƒ | j r( Pq( q( W| j ƒ  Wn t k
 rj n X| j S(   s…   Reads HTML data from a response object `response` searching for `anchor`.
    Returns True if anchor was found, False otherwise.
    t
   chunk_sizei   t   decode_unicode(   R   t   iter_contentR&   t   feedR"   t   closeR   (   t   responset   anchort   parsert   chunk(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   check_anchorE   s    	t   CheckExternalLinksBuilderc           B   st   e  Z d  Z d Z e d ƒ Z d „  Z d „  Z d „  Z d d „ Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   s+   
    Checks for broken external links.
    t	   linkchecksC   Look for any errors in the above output or in %(outdir)s/output.txtc         C   s0  g  |  j  j j D] } t j | ƒ ^ q |  _ g  |  j  j j D] } t j | ƒ ^ q> |  _ t ƒ  |  _	 i  |  _
 i  |  _ t j d ƒ t t j |  j d ƒ d ƒ j ƒ  t j ƒ  |  _ t j ƒ  |  _ g  |  _ xY t |  j  j j ƒ D]B } t j d |  j ƒ } | j t ƒ | j ƒ  |  j j  | ƒ qæ Wd  S(   Ng      @s
   output.txtt   wt   target(!   t   appt   configt   linkcheck_ignoret   ret   compilet	   to_ignoret   linkcheck_anchors_ignoret   anchors_ignoret   sett   goodt   brokent
   redirectedt   sockett   setdefaulttimeoutt   openR    t   joint   outdirR1   R   t   Queuet   wqueuet   rqueuet   workerst   ranget   linkcheck_workerst	   threadingt   Threadt   check_threadt	   setDaemonR&   t   startt   append(   R#   t   xt   it   thread(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   initb   s     .+		"	
c            s×   i t  d 6i d d 6d 6‰ ˆ j j j r@ ˆ j j j ˆ d <n  ‡ ‡ ‡ f d †  ‰  ‡  ‡ ‡ f d †  } xf t  rÒ ˆ j j ƒ  \ ‰ } } ˆ d  k r› Pn  | ƒ  \ } } } ˆ j j ˆ | | | | | f ƒ qm Wd  S(   Nt   allow_redirectss/   text/html,application/xhtml+xml;q=0.9,*/*;q=0.8t   Acceptt   headerst   timeoutc             sr  d ˆ k rT ˆ j  d d ƒ \ }  } x9 ˆ j D] } | j | ƒ r. d  } Pq. q. Wn ˆ }  d  } y |  j d ƒ Wn t k
 r t |  ƒ }  n Xyâ | rˆ j j j	 rt
 j |  d t d ˆ j j ˆ  } t | t | ƒ ƒ } | srt t d ƒ | ƒ ‚ qrnn y, t
 j |  d ˆ j j ˆ  } | j ƒ  Wn? t k
 rqt
 j |  d t d ˆ j j ˆ  } | j ƒ  n XWn€ t k
 r±} | j j d k r›d Sd t | ƒ d
 f SnE t k
 rõ} t | ƒ rßd t | ƒ d
 f Sd t | ƒ d
 f Sn X| j j d ƒ |  j d ƒ k rd S| j } | r;| d | 7} n  | j ra| j d j } d | | f Sd | d
 f Sd  S(   Nt   #i   t   asciit   streamR<   s   Anchor '%s' not foundi‘  t   workings    - unauthorizedi    RE   t   ignoredt   /t    iÿÿÿÿRF   (   Rc   s    - unauthorizedi    (   Rc   Rf   i    (   t   splitRB   t   matcht   Nonet   encodet   UnicodeErrorR   R;   R<   t   linkcheck_anchorsR   t   getR&   R6   R   t	   ExceptionR
   t   headt   raise_for_statusR   R2   t   status_codet   strR   t   urlt   rstript   history(   t   req_urlR3   t   rexR2   R"   t   errt   new_urlt   code(   t   kwargsR#   t   uri(    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt	   check_uri„   sV    		!		c             ss  t  ˆ ƒ d k s! ˆ j d ƒ r% d Sˆ j d ƒ s8 d Sˆ ˆ j k rK d Sˆ ˆ j k rn d ˆ j ˆ d f Sˆ ˆ j k r  d ˆ j ˆ d ˆ j ˆ d f Sx$ ˆ j D] }  |  j ˆ ƒ rª d Sqª Wx? t ˆ j j	 j
 ƒ D]( } ˆ  ƒ  \ } } } | d k rÝ PqÝ qÝ W| d
 k r(ˆ j j ˆ ƒ n> | d k rD| ˆ j ˆ <n" | d k rf| | f ˆ j ˆ <n  | | | f S(   Ni    R`   s   mailto:s   ftp:t	   uncheckedRf   s   http:s   https:t   localRc   t   oldRE   RF   i   Rd   (   R`   s   mailto:s   ftp:(   R~   Rf   i    (   s   http:s   https:(   R   Rf   i    (   Rc   R€   i    (   Rd   Rf   i    (   t   lent
   startswithRD   RE   RF   R@   Rh   RP   R;   R<   t   linkcheck_retriest   add(   Rw   t   _t   statust   infoRz   (   R}   R#   R|   (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   checkÄ   s0    !#(	   R&   R;   R<   t   linkcheck_timeoutRM   Rm   Ri   RN   t   put(   R#   Rˆ   t   docnamet   linenoR†   R‡   Rz   (    (   R}   R{   R#   R|   s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyRT   y   s    @ 	c   
      C   sh  | \ } } } } } } | d k r( d  S| d k rD | d k rD d  S| rc t  j d | d d ƒn  | d k r± | r— t  j t d ƒ | d	 | ƒ qdt  j t d ƒ | ƒ n³| d
 k rí t  j t d ƒ | ƒ |  j d
 | | | ƒ nw| d k rt  j t d ƒ | | ƒ nM| d k rµ|  j d | | | d	 | ƒ |  j j sY|  j j rt  j t	 d ƒ | | d |  j
 j | ƒ | f ƒqdt  j t d ƒ | t d | ƒ ƒ n¯ | d k rdi d t f d 6d t f d 6d t f d 6d t f d 6d t f d 6| \ } }	 |  j d | | | | d | ƒ t  j |	 d ƒ | |	 d | d | ƒ ƒ n  d  S(    NR~   Rc   R€   s   (line %4d) t   nonli   Rd   s
   -ignored- s   : R   s
   -local-   s
   ok        RE   s   broken link: %s (%s)t   locations
   broken    s    - RF   t   permanentlyi-  s
   with Foundi.  s   with See Otheri/  t   temporarilyi3  s   with unknown codei    s   redirected s    to s
   redirect  (   t   loggerR‡   R   t   write_entryR   R;   t   quiett   warningiserrort   warningR
   t   envt   doc2pathR   R   R   R   (
   R#   t   resultR|   R‹   RŒ   R†   R‡   Rz   t   textt   color(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   process_resultë   sB    "(c         C   s   d S(   NRf   (    (   R#   R‹   t   typ(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   get_target_uri  s    c         C   s
   |  j  j S(   N(   R–   t
   found_docs(   R#   (    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   get_outdated_docs  s    c         C   s   d  S(   N(    (   R#   t   docnames(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   prepare_writing  s    c         C   s  t  j d ƒ d } x— | j t j ƒ D]ƒ } d | k r> q& n  | d } d  } x2 | d  k r‚ | j } | d  k rv Pn  | j } qQ W|  j j	 | | | f t
 ƒ | d 7} q& Wd } x0 | | k  rå |  j |  j j ƒ  ƒ | d 7} q¶ W|  j rþ d |  j _ n  d  S(   NRf   i    t   refurii   (   R‘   R‡   t   traverseR   t	   referenceRi   t   parentt   lineRM   RŠ   R!   R›   RN   Rm   RE   R;   t
   statuscode(   R#   R‹   t   doctreet   nt   nodeR|   RŒ   t   done(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt	   write_doc  s(    
		c      
   C   s]   t  j t j |  j d ƒ d d ƒ 3 } | j d |  j j | d  ƒ | | | f ƒ Wd  QXd  S(   Ns
   output.txtt   as   utf-8s   %s:%s: [%s] %s
(	   t   codecsRI   R    RJ   RK   t   writeR–   R—   Ri   (   R#   t   whatR‹   R¦   R|   t   output(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyR’   5  s    'c         C   s+   x$ |  j  D] } |  j j d t ƒ q
 Wd  S(   N(   NNN(   RO   RM   RŠ   Ri   R!   (   R#   t   worker(    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   finish;  s    N(   R   R   R,   R%   R
   t   epilogR[   RT   R›   Ri   R   RŸ   R¡   R¬   R’   R³   (    (    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyR7   Z   s   		r	&				c         C   s¡   |  j  t ƒ |  j d g  d  ƒ |  j d d d  ƒ |  j d d  d  t g ƒ |  j d d d  ƒ |  j d t d  ƒ |  j d d	 g d  ƒ i d
 d 6t d 6t d 6S(   NR=   Rƒ   i   R‰   RQ   i   Rl   RA   s   ^!t   builtint   versiont   parallel_read_safet   parallel_write_safe(   t   add_builderR7   t   add_config_valueRi   t   intR&   (   R;   (    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   setupA  s    (9   R,   R®   R>   RG   RR   t   osR    t   docutilsR   t   requests.exceptionsR   t	   six.movesR   R   t   six.moves.urllib.parseR   t   six.moves.html_parserR   t   ImportErrorRn   t   sphinx.buildersR	   t   sphinx.localeR
   t   sphinx.utilR   R   R   t   sphinx.util.consoleR   R   R   R   R   R   t   sphinx.util.requestsR   R!   t   typingR   R   R   R   R   R   t   sphinx.applicationR   t   sphinx.util.requests.requestsR   t	   getLoggerR   R‘   R   R   R6   R7   R¼   (    (    (    s8   lib/python2.7/site-packages/sphinx/builders/linkcheck.pyt   <module>
   s6   ..	ç