o
    a                     @   s   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlZe	dd d dl
mZmZ d dlZd dlZd dlZd dlZeeZd dlZdZd	d
 Zdd Zdd Zdd Zdd Zdd ZdddZdddZdS )    )print_functionN)datetimewrapsGtkz3.0)Gior   z%/var/lib/ubuntu-advantage/status.jsonc                 C   sz   t  | _| j| | j| | j|  | j D ]}tt|t j	r3t j	
|}t| || qtd|  qd S )Nz can not get name for object '%s')r   ZBuilderZbuilderZset_translation_domainZadd_from_fileZconnect_signalsZget_objects
issubclasstypeZ	BuildableZget_namesetattrloggingdebug)selfpathdomainoname r   >/usr/lib/python3/dist-packages/softwareproperties/gtk/utils.pysetup_ui'   s   
r   c                  C   s,   zt jd} | d kW S  ty   Y dS w )Nz#gnome-online-accounts-panel.desktopF)r   ZDesktopAppInfonew	Exception)dr   r   r   has_gnome_online_accounts4   s   
r   c                  C   s   t j } t }|| jS N)
aptsourcesdistro
get_distrodistro_infoUbuntuDistroInfoZis_ltscodenamer   dir   r   r   is_current_distro_lts;   s   
r"   c                  C   s*   t j } t }| j|t 	 v S r   )
r   r   r   r   r   r   Z	supportedr   Znowdater    r   r   r   is_current_distro_supported@   s   
r$   c                  C   s@   t j } t }|jdd}|D ]}|j| jkr|  S qd S )Nobject)result)r   r   r   r   r   Zget_allZseriesr   )r   r!   Zreleasesreleaser   r   r   current_distroE   s   
r(   c               
   C   s,  d} t jtr!tt}| } W d   n1 sw   Y  n8ztjg dtjd}W n t	yG } zt
d|  i W  Y d}~S d}~ww |jdkrVt
d|j  i S |j} | sat
d i S zt| }W n tjy } zt
d	|  i W  Y d}~S d}~ww |d
ddkrt
d|d
   |S )zBReturn a dict of all UA status information or empty dict on error. N)Zuastatusz--format=json)stdoutzFailed to run `ua status`:
%sr   z(Ubuntu Advantage client returned code %dzOWarning: no Ubuntu Advantage status found. Is ubuntu-advantage-tools installed?z0Failed to parse ubuntu advantage client JSON:
%sZ_schema_versionz0.1z#UA status schema version change: %s)osr   existsUA_STATUS_JSONopenread
subprocessrunPIPEr   print
returncoder+   jsonloadsZJSONDecodeErrorget)Zstatus_jsonstreamr&   er*   r   r   r   get_ua_statusN   sL   





r;   	esm-infrac                 C   sd   |st  }d}d}|dg D ]}|d| krqd|v r%td|d k}d|v r-|d }q||fS )ak  Get service availability and status for a specific UA service.

    Return a tuple (available, service_status).
      :boolean available: set True when either:
        - attached contract is entitled to the service
        - unattached machine reports service "availability" as "yes"
      :str service_status: will be one of the following:
        - "disabled" when the service is available and applicable but not
          active
        - "enabled" when the service is available and active
        - "n/a" when the service is not applicable to the environment or not
          entitled for the attached contract
    Fzn/aservicesr   	availableZyesr*   )r;   r8   bool)Zservice_namer*   r>   Zservice_statusservicer   r   r   get_ua_service_status{   s   rA   
   皙?   c                    s    fdd}|S )a  
    Retry calling the decorated function using an exponential backoff.

    Args:
        exceptions: The exception to check. may be a tuple of
            exceptions to check.
        tries: Number of times to try (not retry) before giving up.
        delay: Initial delay between retries in seconds.
        backoff: Backoff multiplier (e.g. value of 2 will double the delay
            each retry).
    c                    s    t   fdd}|S )Nc               
      s   }}|dkr@z| i |W S  y; } zd ||}t| t| |d8 }| 9 }W Y d }~nd }~ww |dks	| i |S )N   z{}, Retrying in {} seconds...)formatr   Zwarningtimesleep)argskwargsZmtriesZmdelayr:   msg)backoffdelay
exceptionsftriesr   r   f_retry   s   


	z*retry.<locals>.deco_retry.<locals>.f_retryr   )rO   rQ   rL   rM   rN   rP   )rO   r   
deco_retry   s   zretry.<locals>.deco_retryr   )rN   rP   rM   rL   rS   r   rR   r   retry   s   rT   )r<   N)rB   rC   rD   )Z
__future__r   Zaptsources.distror   r   r   	functoolsr   ZgiZrequire_versionZgi.repositoryr   r   r6   r,   r1   r   Z	getLogger__name__ZLOGrG   r.   r   r   r"   r$   r(   r;   rA   rT   r   r   r   r   <module>   s.   
	
-