o
    K&b                  	   @   s   d dl Z d dlmZmZ d dlmZmZmZmZm	Z	 dZ
g dZdZdZe Zdefd	d
Z			ddee dee deee  ddfddZ	ddedeee  ddfddZdedee fddZdS )    N)ListOptional)aptevent_logger
exceptionsmessagesutilz/usr/bin/snap)g      ?g      ?g      @z
proxy.httpzproxy.httpsreturnc                   C   s   dt  v S )z(Returns whether or not snap is installedZsnapd)r   Zget_installed_packages r
   r
   //usr/lib/python3/dist-packages/uaclient/snap.pyis_installed   s   r   
http_proxyhttps_proxyretry_sleepsc                 C   s   t tstdt dS | s|rttj	jdd | r-t j
dddd| g|d |r?t j
dddd	|g|d dS dS )
a  
    Configure snap to use http and https proxies.

    :param http_proxy: http proxy to be used by snap. If None, it will
                       not be configured
    :param https_proxy: https proxy to be used by snap. If None, it will
                        not be configured
    :param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] tells subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the second retry.
    z1Skipping configure snap proxy. {} does not exist.Nsnap)servicesetsystemzproxy.http={}r   zproxy.https={})r   whichSNAP_CMDloggingdebugformateventinfor   ZSETTING_SERVICE_PROXYsubp)r   r   r   r
   r
   r   configure_snap_proxy   s(   

r   protocol_typec                 C   s>   t tstdt dS t jdddd| g|d dS )a  
    Unset snap configuration settings for http and https proxies.

    :param protocol_type: String either http or https
    :param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] tells subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the second retry.
    z3Skipping unconfigure snap proxy. {} does not exist.Nr   Zunsetr   zproxy.{}r   )r   r   r   r   r   r   r   )r   r   r
   r
   r   unconfigure_snap_proxy<   s   

r   keyc                 C   s8   zt ddd| g\}}| W S  tjy   Y dS w )z
    Gets the config value from snap.
    :param protocol: can be any valid snap config option
    :return: the value of the snap config option, or None if not set
    r   getr   N)r   r   stripr   ZProcessExecutionError)r    out_r
   r
   r   get_config_option_valueU   s   
r%   )NNN)N)r   typingr   r   Zuaclientr   r   r   r   r   r   ZSNAP_INSTALL_RETRIESZHTTP_PROXY_OPTIONZHTTPS_PROXY_OPTIONZget_event_loggerr   boolr   strfloatr   r   r%   r
   r
   r
   r   <module>   s<    

*

