a
    K56a'                     @   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	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d ZG d	d
 d
ejjZeejdrG dd dejjZG dd dejjZdadddZdddZdd Z dS )    N   )	NoNetwork)VERSION_NUMBERz
reportbug/z	 (Debian)c                 C   s   |   d}|dv rddlm} |  }|dkrJddl}|||}nddl}|ddd	||}t	j
|d}td
}t|    D ]}	||	s|   |	 ||	< qt|||  }
t| dr| j|
_t| dr| j|
_|
S | S )z#gunzip or deflate a compressed pagezContent-Encoding)gzipzx-gzipdeflater   )StringIOr   N rb	   z(?i)content-encoding:codemsg)infogetior   readzlib
decompressr   ZGzipFilehttpclientZHTTPMessagerecompilelistkeysmatchurllibZ
addinfourlZgeturlhasattrr   r   )pageencodingr   Zcontentr   fpr   headersZceheaderhZnewpage r!   4/usr/lib/python3/dist-packages/reportbug/urlutils.pydecode-   s*    



r#   c                   @   s   e Zd ZdZdd ZdS )HttpWithGzipHandlersupport gzip encodingc                 C   s   t tjj| |S N)r#   r   requestHTTPHandler	http_openselfZreqr!   r!   r"   r)   N   s    zHttpWithGzipHandler.http_openN)__name__
__module____qualname____doc__r)   r!   r!   r!   r"   r$   K   s   r$   ZHTTPSc                   @   s   e Zd ZdZdd ZdS )HttpsWithGzipHandlerr%   c                 C   s   t tjj| |S r&   )r#   r   r'   HTTPSHandler
https_openr*   r!   r!   r"   r2   V   s    zHttpsWithGzipHandler.https_openN)r,   r-   r.   r/   r2   r!   r!   r!   r"   r0   S   s   r0   c                   @   s   e Zd Zdd ZdS )handlepasswdc                 C   sb   t jj| ||\}}|d ur&||fS td||f }td|||f }| |||| ||fS )NzEnter username for %s at %s: z#Enter password for %s in %s at %s: )r   r'   HTTPPasswordMgrWithDefaultRealmfind_user_passwordinputgetpassZadd_password)r+   ZrealmZauthurluserZpasswordr!   r!   r"   r5   [   s    zhandlepasswd.find_user_passwordN)r,   r-   r.   r5   r!   r!   r!   r"   r3   Z   s   r3   <   c                 C   s,   |st j }tdd}tj| |||djS )zFetch an URL.

    :param url:

        The URL to open, e.g. ``"http://example.com/"``.

    :returns:

        The contents returned by invoking GET on the given URL, as a
        Unicode string, e.g. ``u"Hello"``.

    z)gzip;q=1.0, deflate;q=0.9, identity;q=0.5)z
User-AgentzAccept-Encoding)r   proxiestimeout)r   r'   
getproxiesUA_STRrequestsr   text)urlr:   r;   datar   r!   r!   r"   urlopenj   s    
rB   c           
   
   C   sn  t | tj }|r(||d< ||d< zt| ||}W n. tjjy| } z&|jdv rfW Y d}~dS  W Y d}~nd}~0  t j	t jtjj
fy } ztW Y d}~nd}~0  ty } z:|r|d dkr|d dkrW Y d}~dS tW Y d}~npd}~0  ty&   td	tjd
 tY nD tjjyh } z&|jj}dt  }	t|	W Y d}~n
d}~0 0 |S )a  :param url:

        The URL to open, e.g. ``"http://example.org/"``.

    :param http_proxy:

        The proxy to use for the http protocol.  By default, use the
        :func:`urllib.request.getproxies()` settings.

    :param timeout:

        Socket timeout, in seconds.

    :returns:

        The contents returned by invoking GET on the given URL, as a
        Unicode string, e.g. ``u"Hello"``.

    r   Zhttps)  i  i  Nr   z
http errorr   rC   z@http_proxy environment variable must be formatted as a valid URI)filez.Failed to open %(url)r (%(exc_name)s: %(exc)s))socketZsetdefaulttimeoutr   r'   r<   rB   errorZ	HTTPErrorr   ZgaierrorZURLErrorr   IOError	TypeErrorprintsysstderrr   r   ZHTTPException	__class__r,   vars)
r@   Z
http_proxyr;   r:   r   xrA   excZexc_namemessager!   r!   r"   open_url   s2    



rQ   c                 C   s<   t ds&dt|  }t | d S tr8t|  d S d S )Nz#command -v xdg-open >/dev/null 2>&1z	xdg-open )ossystemshlexZquote
webbrowseropen)r@   cmdr!   r!   r"   launch_browser   s    


rX   )Nr9   N)Nr9   )!Zhttp.clientr   Zurllib.requestr   Zurllib.parseZurllib.errorr7   r   rE   rT   rR   rJ   rU   r>   
exceptionsr   __init__r   r=   r#   r'   r(   r$   r   r   r1   r0   r4   r3   Z_openerrB   rQ   rX   r!   r!   r!   r"   <module>   s,   
/
5