ó
7˜£[c           @   s=  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 Z d  d l	 Z	 d  d l
 m Z d  d l m Z d e d Z d „  Z d e j f d „  ƒ  YZ e e  d	 ƒ ró d
 e j f d „  ƒ  YZ n  d e j f d „  ƒ  YZ d a d d d „ Z d d d „ Z d „  Z d S(   iÿÿÿÿN(   t	   NoNetwork(   t   VERSION_NUMBERs
   reportbug/s	    (Debian)c         C   s]  |  j  ƒ  j d ƒ } | d k rYd d l m } |  j ƒ  } | d k rm d d l } | | j | ƒ ƒ } n* d d l } | j d d	 d
 | | ƒ ƒ } t	 j
 | d ƒ ƒ } t j d ƒ } x@ |  j  ƒ  j ƒ  D], }	 | j |	 ƒ sÎ |  j  ƒ  |	 | |	 <qÎ qÎ Wt j | | |  j ƒ  ƒ }
 t |  d ƒ r7|  j |
 _ n  t |  d ƒ rU|  j |
 _ n  |
 S|  S(   s#   gunzip or deflate a compressed pages   Content-Encodingt   gzips   x-gzipt   deflateiÿÿÿÿ(   t   StringIONt    t   rbi	   s   (?i)content-encoding:t   codet   msg(   s   gzips   x-gzipR   (   t   infot   gett	   cStringIOR   t   readt   zlibt
   decompressR   t   GzipFilet   httplibt   HTTPMessaget   ret   compilet   keyst   matcht   urllibt
   addinfourlt   geturlt   hasattrR   R   (   t   paget   encodingR   t   contentR   t   fpR   t   headerst   ceheadert   ht   newpage(    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyt   decode+   s*    t   HttpWithGzipHandlerc           B   s   e  Z d  Z d „  Z RS(   s   support gzip encodingc         C   s   t  t j j |  | ƒ ƒ S(   N(   R"   t   urllib2t   HTTPHandlert	   http_open(   t   selft   req(    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyR&   J   s    (   t   __name__t
   __module__t   __doc__R&   (    (    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyR#   H   s   t   HTTPSt   HttpsWithGzipHandlerc           B   s   e  Z d  Z d „  Z RS(   s   support gzip encodingc         C   s   t  t j j |  | ƒ ƒ S(   N(   R"   R$   t   HTTPSHandlert
   https_open(   R'   R(   (    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyR/   P   s    (   R)   R*   R+   R/   (    (    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyR-   N   s   t   handlepasswdc           B   s   e  Z d  „  Z RS(   c         C   s†   t  j j |  | | ƒ \ } } | d  k	 r4 | | f St d | | f ƒ } t j d | | | f ƒ } |  j | | | | ƒ | | f S(   Ns   Enter username for %s at %s: s#   Enter password for %s in %s at %s: (   R$   t   HTTPPasswordMgrWithDefaultRealmt   find_user_passwordt   Nonet	   raw_inputt   getpasst   add_password(   R'   t   realmt   authurlt   usert   password(    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyR2   T   s    
(   R)   R*   R2   (    (    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyR0   S   s   c      	   C   sû   | s t  j ƒ  } n  i t d 6d d 6} t j |  | | ƒ } t j | ƒ } t d  k rî t ƒ  } | t j	 t
 t j | ƒ t j | ƒ t j | ƒ t j | ƒ t j t j g	 } t t d ƒ rÏ | j t ƒ n  t j | Œ  a t j t ƒ n  t j | ƒ S(   Ns
   User-Agents)   gzip;q=1.0, deflate;q=0.9, identity;q=0.5s   Accept-EncodingR,   (   R   t
   getproxiest   UA_STRR$   t   Requestt   ProxyHandlert   _openerR3   R0   t   UnknownHandlerR#   t   HTTPBasicAuthHandlert   ProxyBasicAuthHandlert   HTTPDigestAuthHandlert   ProxyDigestAuthHandlert   HTTPDefaultErrorHandlert   HTTPRedirectHandlerR   R   t   appendR-   t   build_openert   install_openert   open(   t   urlt   proxiest   dataR   R(   t   proxy_supportt   pwd_managert   handlers(    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyt   urlopen`   s(    

		i<   c   
      C   s;  t  j | ƒ t j ƒ  } | r, | | d <n  y t |  | ƒ } Wnõ t j k
 rm } | j d
 k rg d  S‚  nÊ t  j	 t  j
 t j f k
 r— } t ‚ n  t k
 rÙ } | rÐ | d d k rÐ | d d k rÐ d  St ‚ n^ t k
 rü t j d IJt ‚ n; t j k
 r6} | j j } d	 t ƒ  }	 t |	 ƒ ‚ n X| S(   Nt   httpi”  iô  i÷  i    s
   http errori   s@   http_proxy environment variable must be formatted as a valid URIs.   Failed to open %(url)r (%(exc_name)s: %(exc)s)(   i”  iô  i÷  (   t   sockett   setdefaulttimeoutR   R;   RQ   R$   t	   HTTPErrorR   R3   t   gaierrort   errort   URLErrorR    t   IOErrort	   TypeErrort   syst   stderrR   t   HTTPExceptiont	   __class__R)   t   vars(
   RK   t
   http_proxyt   timeoutRL   R   t   xRM   t   exct   exc_namet   message(    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyt   open_url€   s2    !	&		
c         C   sä   t  j d ƒ s3 d t j |  ƒ } t  j | ƒ d  St rJ t j |  ƒ d  St  j j d d ƒ } t  j j d d ƒ } t  j j d ƒ rº t  j d | d	 ƒ rº d
 | t j |  ƒ f } n d | t j |  ƒ f } t  j | ƒ d  S(   Ns#   command -v xdg-open >/dev/null 2>&1s   xdg-opent
   X11BROWSERs   mozilla-firefoxt   CONSOLEBROWSERt   lynxt   DISPLAYs   command -v s    &> /dev/nulls   %s %s &s   %s %s(	   t   ost   systemt   commandst   mkargt
   webbrowserRJ   t   environR
   t   has_key(   RK   t   cmdRg   Rh   (    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyt   launch_browser£   s    (   R   R   R$   R5   R   RS   Rm   Rk   R[   Ro   t
   exceptionsR    t   __init__R   R<   R"   R%   R#   R   R.   R-   R1   R0   R3   R?   RQ   Rf   Rs   (    (    (    s6   /usr/lib/python2.7/dist-packages/reportbug/urlutils.pyt   <module>   s*   	 #