o
    (f                  
   @   s  d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZmZ d dlmZ d dlmZ e	 ZG dd	 d	ejZd
d ZdefddZd%ddZdd Z dd Z!d%ddZ"dd Z#dd Z$deddfddZ%ded ed!e&d"ee& ddf
d#d$Z'dS )&    wraps)Optional)
actionsapidaemonentitlementsevent_logger
exceptionslockmessagesstatusutil)_is_attached)AptProxyScopesetup_apt_proxy)UAConfig)machine_tokenc                	   @   sJ   e Zd ZdefddZdededee dee fdd	Zd
efddZ	dS )CLIEnableDisableProgress
assume_yesc                C   s   | | _ || _d S N)is_interactiver   )selfr    r   7/usr/lib/python3/dist-packages/uaclient/cli/cli_util.py__init__   s   
z!CLIEnableDisableProgress.__init__total_steps
done_stepsprevious_step_messagecurrent_step_messagec                C   s   |d ur
t | d S d S r   )print)r   r   r   r   r   r   r   r   progress   s   z!CLIEnableDisableProgress.progresseventc                 C   s:   |dkr
t | d S |dkrt|| jst d S d S )Ninfomessage_operation)r    r   handle_message_operationsr   r
   PromptDeniedError)r   r"   payloadr   r   r   	_on_event(   s   z"CLIEnableDisableProgress._on_eventN)
__name__
__module____qualname__boolr   intr   strr!   r(   r   r   r   r   r      s    
r   c                  O   s   d S r   r   argskwargsr   r   r   _null_print0   s   r2   json_outputc                 C   s   | rt S tS r   )r2   r    )r3   r   r   r   &create_interactive_only_print_function4   s   r4   Nc                        fdd}|S )z1Decorator asserting exclusive access to lock filec                       t   fdd}|S )Nc                    sF   t jdd  |d| i|}W d    |S 1 sw   Y  |S )N   )lock_holder
sleep_timecfg)r   	RetryLock)r:   r0   r1   retval)fr8   r   r   new_f?   s   
z0assert_lock_file.<locals>.wrapper.<locals>.new_fr   r=   r>   r8   r=   r   wrapper>   s   z!assert_lock_file.<locals>.wrapperr   )r8   rB   r   r@   r   assert_lock_file;   s   	rC   c                       t   fdd}|S )zDecorator asserting root userc                     s   t  st  | i |S r   )r   we_are_currently_rootr
   NonRootUserErrorr/   rA   r   r   r>   M   s   zassert_root.<locals>.new_fr   r?   r   rA   r   assert_rootJ   s   rG   c                    rD   )z>Decorator to verify if correct params are used for json formatc                    sH   | s | g|R i |S | j dkr| jst  | g|R i |S )Njson)formatr   r
   CLIJSONFormatRequireAssumeYes)cmd_argsr0   r1   rA   r   r   r>   Z   s
   z&verify_json_format_args.<locals>.new_fr   r?   r   rA   r   verify_json_format_argsW   s   	rL   c                    r5   )zDecorator asserting attached config.
    :param msg_function: Optional function to generate a custom message
    if raising an UnattachedError
    c                    r6   )Nc                    sR   t |jsrt| dd}t| dd}|||d nt  | fd|i|S )Ncommand service)rM   service_namesr:   r:   )r   is_attachedgetattrr
   UnattachedError)r0   r:   r1   rM   rP   )r=   raise_custom_error_functionr   r   r>   n   s   
z/assert_attached.<locals>.wrapper.<locals>.new_fr   r?   rT   rA   r   rB   m   s   z assert_attached.<locals>.wrapperr   )rT   rB   r   rU   r   assert_attachedg   s   rV   c                    rD   )z&Decorator asserting unattached config.c                    s<   t |jrt }tj|jddd | fd|i|S )NnamerN   )account_namer:   )r   rQ   r   get_machine_token_filer
   AlreadyAttachedErroraccountget)r0   r:   r1   machine_token_filerA   r   r   r>      s   
z"assert_not_attached.<locals>.new_fr   r?   r   rA   r   assert_not_attached   s   r^   c                 C   sf   t j||d\}}|r|rtjd|| d|dd|r(tjd|| dtj| d|dd)zRaises a custom error for enable/disable commands when unattached.

    Takes into consideration if the services exist or not, and notify the user
    accordingly.)namesr:   z, rN   )valid_service	operationinvalid_serviceservice_msg)r`   ra   )ra   rb   rc   )r   get_valid_entitlement_namesr
   UnattachedMixedServicesErrorjoinUnattachedValidServicesErrorUnattachedInvalidServicesError)rM   rP   r:   entitlements_foundentitlements_not_foundr   r   r   &_raise_enable_disable_unattached_error   s(   
rk   r:   returnc                 C   s|   t | }|j}|rttjj|d nttj t	
  t	|  t| \}}t|}tt| t  d S )N)contract_name)r   rY   rm   r"   r#   r   ATTACH_SUCCESS_TMPLrI   ATTACH_SUCCESS_NO_CONTRACT_NAMEr   stopcleanupr   r   format_tabularr   handle_unicode_charactersprocess_events)r:   r]   rm   status_dict_retoutputr   r   r   post_cli_attach   s   


rx   scopeset_key	set_valuec                 C   sR   |t jkr| j}| j}n|t jkr| j}| j}d|v r|}n|}t|||d dS )zS
    Handles setting part the apt proxies - global and uaclient scoped proxies
    https)
http_proxyhttps_proxyproxy_scopeN)r   GLOBALglobal_apt_http_proxyglobal_apt_https_proxyUACLIENTua_apt_http_proxyua_apt_https_proxyr   )r:   ry   rz   r{   r}   r~   r   r   r   configure_apt_proxy   s   
	

r   r   )(	functoolsr   typingr   uaclientr   r   r   r   r	   r
   r   r   r   r   (uaclient.api.u.pro.status.is_attached.v1r   uaclient.aptr   r   uaclient.configr   uaclient.filesr   get_event_loggerr"   AbstractProgressr   r2   r,   r4   rC   rG   rL   rV   r^   rk   rx   r.   r   r   r   r   r   <module>   s:    0

