ó
éŠTc           @   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*   	 #