
    +'f	                        d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dl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eeeeejH                  ejJ                  ejL                  eeeeegZ'd(de	de(de(fdZ)	 d)de	de*de*dee(   fdZ+de	dee(   dee(   fdZ,e jZ                   G d de j\                               Z/de	dee(   fdZ0de	dee(   fdZ1de	dee   d e/d!ee(e*f   d"ee(   f
d#Z2de	d e/dee(   fd$Z3d%ee(   de	fd&Z4de	de*dejj                  fd'Z6y)*    N)DictListType)
exceptions)UAConfig)fips)AnboxEntitlement)UAEntitlement)CommonCriteriaEntitlement)CISEntitlement)ESMAppsEntitlementESMInfraEntitlement)LandscapeEntitlement)LivepatchEntitlement)RealtimeKernelEntitlement)ROSEntitlementROSUpdatesEntitlement)EntitlementNotFoundError)is_config_value_truecfgnamevariantc                     t         D ]J  } ||       }||j                  v s|s|c S ||j                  v r|j                  |   c S t        |       t        |      )a  Returns a UAEntitlement class based on the provided name.

    The return type is Optional[Type[UAEntitlement]].
    It cannot be explicit because of the Python version on Xenial (3.5.2).
    :param cfg: UAConfig instance
    :param name: The name of the entitlement to return
    :param not_found_okay: If True and no entitlement with the given name is
        found, then returns None.
    :raise EntitlementNotFoundError: If not_found_okay is False and no
        entitlement with the given name is found, then raises this error.
    r   )entitlement_name)ENTITLEMENT_CLASSESvalid_namesvariantsr   )r   r   r   entitlementents        @/usr/lib/python3/dist-packages/uaclient/entitlements/__init__.pyentitlement_factoryr"   %   sg     + Ic"3??"""CLL(||G,,.HHI #D
99    
allow_beta	all_namesreturnc                 V   t        | j                  d      }||z  }t        }|s|D cg c]  }|j                  s| }}|r6g }|D ]$  }|j	                   ||       j
                         & t        |      S t        |D cg c]  } ||       j                   c}      S c c}w c c}w )a  Return a list of valid (non-beta) services.

    :param cfg: UAConfig instance
    :param allow_beta: if we should allow beta services to be marked as valid
    :param all_names: if we should return all the names for a service instead
        of just the presentation_name
    zfeatures.allow_betar   )r   r   r   is_betaextendr   sortedpresentation_name)r   r$   r%   allow_beta_cfgentitlementsr   namess          r!   valid_servicesr/   =   s     *#''3HIN. J&L  ,
&& 
 
 ' 	;KLL-99:	; e}  ,	
 C 22	
 
	
s   B! B&entsc                 F    t        |       fdt        |fd      S )zR
    A function to sort entitlments for enabling that preserves invalid names
    c                 \    	 j                  |       S # t        $ r t              cY S w xY wN)index
ValueErrorlen)r    valid_ents_ordereds    r!    sort_order_with_nonexistent_lastzIorder_entitlements_for_enabling.<locals>.sort_order_with_nonexistent_lasti   s4    	+%++C00 	+)**	+s    ++c                      |       S r3    )r    r8   s    r!   <lambda>z1order_entitlements_for_enabling.<locals>.<lambda>o   s    (H(M r#   )key)entitlements_enable_orderr*   )r   r0   r8   r7   s     @@r!   order_entitlements_for_enablingr>   a   s%     337+ $MNNr#   c                   (    e Zd Z e       Z e       Zy)	SortOrderN)__name__
__module____qualname__objectREQUIRED_SERVICESDEPENDENT_SERVICESr:   r#   r!   r@   r@   r   s    r#   r@   c                 8    t        | t        j                        S )zR
    Return the entitlements disable order based on dependent services logic.
    r   
sort_order)_sort_entitlementsr@   rF   r   s    r!   entitlements_disable_orderrK   x   s     #)2N2NOOr#   c                 8    t        | t        j                        S )zP
    Return the entitlements enable order based on required services logic.
    rH   )rJ   r@   rE   r   s    r!   r=   r=      s     #)2M2MNNr#   ent_clsrI   visitedorderc                 V   |j                   |v ry |t        j                  k(  r ||       j                  }n ||       j                  }|D ]!  }|j                   |vst        | ||||       # |j                  t        |j                                d|t        |j                         <   y )Nr   rM   rI   rN   rO   T)r   r@   rE   required_servicesdependent_services_sort_entitlements_visitappendstr)r   rM   rI   rN   rO   cls_listcls_dependencys          r!   rT   rT      s     ||wY0003<113<22" <<w&$&% 
LLW\\"#!%GCr#   c                 D    g }i }t         D ]  }t        | ||||        |S )NrQ   )r   rT   )r   rI   rO   rN   rM   s        r!   rJ   rJ      s9    EG& 
 !	

 Lr#   r.   c                     g }| D ]$  }|t        |dd      v s|j                  |       & t        t        |       t        |      z
        }||fS )zReturn a list of valid entitlement names.

    :param names: List of entitlements to validate
    :return: a tuple of List containing the valid and invalid entitlements
    T)r   r$   r%   )r/   rU   r*   set)r.   r   entitlements_foundent_nameentitlements_not_founds        r!   get_valid_entitlement_namesr_      sg      0~
 
 %%h/	0 $CJ5G1H$HI555r#   c                    t        ||      }dj                  |      }dj                  t        j                  d|z   dz   ddd            }t	        j
                  d	dj                  |       |
      S )z{
    Constructs the MESSAGE_INVALID_SERVICE_OP_FAILURE message
    based on the attempted services and valid services.
    )r   r$   z, 
zTry .P   F)widthbreak_long_wordsbreak_on_hyphensenable)	operationinvalid_serviceservice_msg)r/   jointextwrapwrapr   InvalidServiceOpError)r^   r   r$   valid_services_namesr   rj   s         r!   *create_enable_entitlements_not_found_errorrp      sx     *cjI))01K))[ 3&""		
K ++		"89 r#   ) )FF)7enumrl   typingr   r   r   uaclientr   uaclient.configr   uaclient.entitlementsr   uaclient.entitlements.anboxr	   uaclient.entitlements.baser
   uaclient.entitlements.ccr   uaclient.entitlements.cisr   uaclient.entitlements.esmr   r   uaclient.entitlements.landscaper   uaclient.entitlements.livepatchr   uaclient.entitlements.realtimer   uaclient.entitlements.rosr   r   uaclient.exceptionsr   uaclient.utilr   FIPSEntitlementFIPSUpdatesEntitlementFIPSPreviewEntitlementr   rV   r"   boolr/   r>   uniqueEnumr@   rK   r=   rT   rJ   r_   UbuntuProErrorrp   r:   r#   r!   <module>r      s     # #  $ & 8 4 > 4 M @ @ D K 8 .  ":X :S :3 :2 @E!	!#!8<!	#Y!HO	OcO	#YO" "		 " "
PH Pc PO8 OS	 O&	&- & & #t)_	&
 9&:H ) S	  6tCy 6x 6&!):>r#   