o
    ÙKÕi¡#  ã                   @   s~   d dl Z d dlmZ d dlmZmZmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ daG dd„ dƒZdd	efd
d„ZdS )é    N)Údatetime)ÚAnyÚDictÚOptional)ÚdefaultsÚ
exceptionsÚsystemÚutil)ÚPublicMachineTokenData)ÚUAFilec                   @   sb  e Zd Zejdfdedee fdd„Zdefdd„Z	d	d
„ Z
dee fdd„Zedd„ ƒZedd„ ƒZedee fdd„ƒZdd„ Zedeeef fdd„ƒZd2dee fdd„Ze	d2deeef dee fdd„ƒZedee fdd„ƒZed d!„ ƒZedee fd"d#„ƒZed3d%d&„ƒZed3d'd(„ƒZed4d*d+„ƒZed,d-„ ƒZed.d/„ ƒZ ed0d1„ ƒZ!dS )5ÚMachineTokenFileNÚ	directoryÚmachine_token_overlay_pathc                 C   sF   t j}t||d t j ƒ| _t||dƒ| _|| _d | _d | _d | _	d S )Nú/F)
r   ÚMACHINE_TOKEN_FILEr   ÚPRIVATE_SUBDIRÚprivate_fileÚpublic_filer   Ú_machine_tokenÚ_entitlementsÚ_contract_expiry_datetime)Úselfr   r   Ú	file_name© r   ú>/usr/lib/python3/dist-packages/uaclient/files/machine_token.pyÚ__init__   s   ÿ
zMachineTokenFile.__init__Úprivate_contentc                 C   sp   t  ¡ r4tj|t jd�}| j |¡ t |¡j	dd�}tj|t jd�}| j
 |¡ d| _d| _d| _dS t ¡ ‚)z8Update the machine_token file for both pub/private files©ÚclsF)Ú	keep_noneN)r	   Úwe_are_currently_rootÚjsonÚdumpsÚDatetimeAwareJSONEncoderr   Úwriter
   Ú	from_dictÚto_dictr   r   r   r   r   ÚNonRootUserError)r   r   Úprivate_content_strÚpublic_contentÚpublic_content_strr   r   r   r$      s$   ÿÿþÿ
zMachineTokenFile.writec                 C   s:   t  ¡ r| j ¡  | j ¡  d| _d| _d| _dS t 	¡ ‚)z!Delete both pub and private filesN)
r	   r    r   Údeleter   r   r   r   r   r'   ©r   r   r   r   r+   4   s   


zMachineTokenFile.deleteÚreturnc                 C   sR   t  ¡ r| j}n| j}| ¡ }|sd S ztj|t jd�}W |S  ty(   Y |S w ©Nr   )	r	   r    r   r   Úreadr!   ÚloadsÚDatetimeAwareJSONDecoderÚ	Exception)r   Úfile_handlerÚcontentr   r   r   r/   @   s   þþzMachineTokenFile.readc                 C   s    t  ¡ r| jjo| jjS | jjS ©N)r	   r    r   Ú
is_presentr   r,   r   r   r   r6   N   s   zMachineTokenFile.is_presentc                 C   sB   | j s|  ¡ }|r| jr|  | j¡}|rtj||d� || _ | j S )zAReturn the machine-token if cached in the machine token response.)Ú	base_dictÚoverlay_dict)r   r/   r   Úparse_machine_token_overlayr	   Údepth_first_merge_overlay_dict)r   r4   Úmachine_token_overlayr   r   r   Úmachine_tokenU   s   
ÿþzMachineTokenFile.machine_tokenc                 C   ó&   | j r| j  di ¡ di ¡ d¡S d S )NÚmachineTokenInfoÚcontractInfoÚname©r<   Úgetr,   r   r   r   Úcontract_nameg   s   ýzMachineTokenFile.contract_namec                 C   s   t  |¡}tj|tjd�S r.   )r   Ú	load_filer!   r0   r	   r1   )r   r   Úmachine_token_overlay_contentr   r   r   r9   r   s   ÿþz,MachineTokenFile.parse_machine_token_overlayc                 C   s   t | jƒr| jd d S i S )Nr>   ÚaccountInfo©Úboolr<   r,   r   r   r   Úaccount{   s   
zMachineTokenFile.accountÚseriesc                 C   s,   | j r| j S | jsi S |  | j|¡| _ | j S )z9Return configured entitlements keyed by entitlement named)r   r<   Úget_entitlements_from_token)r   rJ   r   r   r   Úentitlements�   s   ÿzMachineTokenFile.entitlementsr<   c           
      C   s¬   ddl m} | s
i S i }|  di ¡ d¡}|si S tdd„ |  dg ¡D ƒƒ}tdd„ | d	g ¡D ƒƒ}| ¡ D ]\}}d
|i}	||v rI|| |	d< ||	|d� |	||< q7|S )z…Return a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        r   )Úapply_contract_overridesr>   r?   c                 s   s$   � | ]}|  d ¡|  d¡fV  qdS )ÚtypeÚtokenN©rB   ©Ú.0Úer   r   r   Ú	<genexpr>    s
   € ÿ
ÿz?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>ÚresourceTokensc                 s   s   � | ]
}|  d ¡|fV  qdS )rN   NrP   rQ   r   r   r   rT   ¤   s
   € ÿ
ÿÚresourceEntitlementsÚentitlementÚresourceToken)rJ   )Úuaclient.contractrM   rB   ÚdictÚitems)
r<   rJ   rM   rL   r?   Útokens_by_nameÚent_by_nameÚentitlement_nameÚ	ent_valueÚentitlement_cfgr   r   r   rK   Œ   s0   ÿ
þ
þÿ
z,MachineTokenFile.get_entitlements_from_tokenc                 C   s2   | j s| jr| j di ¡ di ¡ dd¡| _ | j S )z6Return a datetime of the attached contract expiration.r>   r?   ÚeffectiveToN)r   Úis_attachedr<   rB   r,   r   r   r   Úcontract_expiry_datetime²   s   ýz)MachineTokenFile.contract_expiry_datetimec                 C   s
   t | jƒS )z<Report whether this machine configuration is attached to UA.rG   r,   r   r   r   rb   ¾   s   
zMachineTokenFile.is_attachedc                 C   s*   | j du rdS | j  ¡ t ¡  ¡  }|jS )a  Report num days until contract expiration based on effectiveTo

        :return: A positive int representing the number of days the attached
            contract remains in effect. Return a negative int for the number
            of days beyond contract's effectiveTo date.
        N)rc   Údater   ÚutcnowÚdays)r   Údeltar   r   r   Úcontract_remaining_daysÃ   s   
z(MachineTokenFile.contract_remaining_daysúOptional[str]c                 C   ó   | j r| j  di ¡ d¡S d S )NÚactivityInfoÚactivityTokenrA   r,   r   r   r   Úactivity_tokenÐ   ó
   ÿzMachineTokenFile.activity_tokenc                 C   rj   )Nrk   Ú
activityIDrA   r,   r   r   r   Úactivity_idØ   s   zMachineTokenFile.activity_idúOptional[int]c                 C   rj   )Nrk   ÚactivityPingIntervalrA   r,   r   r   r   Úactivity_ping_intervalÞ   rn   z'MachineTokenFile.activity_ping_intervalc                 C   r=   )Nr>   r?   ÚidrA   r,   r   r   r   Úcontract_idæ   s   ýzMachineTokenFile.contract_idc                 C   s   | j r
| j  dg ¡S d S )NrU   rA   r,   r   r   r   Úresource_tokensð   s   z MachineTokenFile.resource_tokensc                 C   s,   |   ¡  di ¡ di ¡ di ¡ dd ¡}|S )NÚsupportrW   ÚaffordancesÚ
onlySeries)rL   rB   )r   Úonly_seriesr   r   r   rz   ÷   s   ûzMachineTokenFile.only_seriesr5   )r-   ri   )r-   rq   )"Ú__name__Ú
__module__Ú__qualname__r   ÚDEFAULT_DATA_DIRÚstrr   r   rZ   r$   r+   r/   Úpropertyr6   r<   rC   r9   r   r   rI   rL   ÚstaticmethodrK   r   rc   rb   Úintrh   rm   rp   rs   ru   rv   rz   r   r   r   r   r      s\    ýþ
ý


	ÿ
ÿÿ%

	
r   r-   c                 C   s4   ddl m} ts| s|ƒ } t| j| j d¡d�atS )Nr   )ÚUAConfigr;   )r   r   )Úuaclient.configrƒ   Ú_machine_token_filer   Údata_dirÚfeaturesrB   )Úcfgrƒ   r   r   r   Úget_machine_token_file  s   ÿþr‰   r5   )r!   r   Útypingr   r   r   Úuaclientr   r   r   r	   Úuaclient.contract_data_typesr
   Úuaclient.files.filesr   r…   r   r‰   r   r   r   r   Ú<module>   s     x