o
    �PÔaþ  ã                   @   sÄ   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZmZ e e¡ZdZG dd„ de jƒZd	eeef d
e
eeef ef fdd„ZG dd„ deƒZG dd„ dƒZdS )é    N)ÚPath)ÚAnyÚDictÚListÚ
NamedTupleÚOptionalÚUnion)Úhazard)Ú	PipxErrorÚ	pipx_wrapzpipx_metadata.jsonc                       s&   e Zd Zdedef‡ fdd„Z‡  ZS )ÚJsonEncoderHandlesPathÚobjÚreturnc                    s$   t |tƒrdt|ƒdœS tƒ  |¡S )Nr   )Ú__type__Ú__Path__)Ú
isinstancer   ÚstrÚsuperÚdefault)Úselfr   ©Ú	__class__© ú9/usr/lib/python3/dist-packages/pipx/pipx_metadata_file.pyr      s   
zJsonEncoderHandlesPath.default)Ú__name__Ú
__module__Ú__qualname__r   r   Ú__classcell__r   r   r   r   r      s    r   Ú	json_dictr   c                 C   s(   |   dd ¡dkrd| v rt| d ƒS | S )Nr   r   r   )Úgetr   )r   r   r   r   Ú_json_decoder_object_hook   s   r    c                   @   sŽ   e Zd ZU ee ed< ee ed< ee ed< eed< eed< ee ed< ee ed< ee ed< e	eee f ed	< eed
< dZ
eed< dS )ÚPackageInfoÚpackageÚpackage_or_urlÚpip_argsÚinclude_dependenciesÚinclude_appsÚappsÚ	app_pathsÚapps_of_dependenciesÚapp_paths_of_dependenciesÚpackage_versionÚ ÚsuffixN)r   r   r   r   r   Ú__annotations__r   Úboolr   r   r-   r   r   r   r   r!      s   
 r!   c                   @   s¨   e Zd ZU dZeed< ddedefdd„Zde	ee
f fd	d
„Zde	ee
f de	ee
f fdd„Zde	ee
f ddfdd„Zddd„Zddd„Zddeddfdd„ZdS )ÚPipxMetadataz0.2Ú__METADATA_VERSION__TÚvenv_dirÚreadc                 C   sJ   || _ td d g ddg g g i dd�
| _d | _g | _i | _|r#|  ¡  d S d S )NFTr,   )
r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   )r2   r!   Úmain_packageÚpython_versionÚ	venv_argsÚinjected_packagesr3   )r   r2   r3   r   r   r   Ú__init__/   s&   öÿzPipxMetadata.__init__r   c                 C   s,   | j  ¡ | j| jdd„ | j ¡ D ƒ| jdœS )Nc                 S   s   i | ]	\}}||  ¡ “qS r   )Ú_asdict©Ú.0ÚnameÚdatar   r   r   Ú
<dictcomp>N   s    ÿz(PipxMetadata.to_dict.<locals>.<dictcomp>)r4   r5   r6   r7   Úpipx_metadata_version)r4   r9   r5   r6   r7   Úitemsr1   ©r   r   r   r   Úto_dictI   s   ÿùzPipxMetadata.to_dictÚmetadata_dictc                 C   sp   |d | j kr	|S |d dkr)|d }|d | jjkr'| jj |d d¡|d< |S td| jj› d|d › d	�ƒ‚)
Nr?   z0.1r4   r"   r,   r-   z
                z+: Unknown metadata version
                zY. Perhaps it was
                installed with a later version of pipx.
                )r1   r2   r<   Úreplacer
   )r   rC   Úmain_package_datar   r   r   Ú_convert_legacy_metadataT   s    ÿÿþÿz%PipxMetadata._convert_legacy_metadataÚ
input_dictNc                 C   sN   |   |¡}tdi |d ¤Ž| _|d | _|d | _dd„ |d  ¡ D ƒ| _d S )Nr4   r5   r6   c                 S   s0   i | ]\}}|› |  d d¡› �tdi |¤Ž“qS )r-   r,   r   )r   r!   r:   r   r   r   r>   m   s    ÿÿz*PipxMetadata.from_dict.<locals>.<dictcomp>r7   r   )rF   r!   r4   r5   r6   r@   r7   )r   rG   r   r   r   Ú	from_dicth   s   



þzPipxMetadata.from_dictc                 C   s@   | j jd u s| j jd u s| j jst d|  ¡ › �¡ tdƒ‚d S )NzPipxMetadata corrupt:
z6Internal Error: PipxMetadata is corrupt, cannot write.)r4   r"   r#   r&   ÚloggerÚdebugrB   r
   rA   r   r   r   Ú_validate_before_writer   s   ÿüz#PipxMetadata._validate_before_writec                 C   s¢   |   ¡  z)t| jt dƒ�}tj|  ¡ |ddtd� W d   ƒ W d S 1 s&w   Y  W d S  tyP   t	 
tdt› dt› d| j› d| jj› d	�	d
d�¡ Y d S w )NÚwé   T)ÚindentÚ	sort_keysÚclsz
                    z  Unable to write z to
                    zG.  This may cause future pipx operations
                    involving zH to fail or behave
                    incorrectly.
                    ú    ©Úsubsequent_indent)rK   Úopenr2   ÚPIPX_INFO_FILENAMEÚjsonÚdumprB   r   ÚIOErrorrI   Úwarningr   r	   r<   )r   Úpipx_metadata_fhr   r   r   Úwrite{   s6   û&ÿÿÿþýù
ÿÿzPipxMetadata.writeFÚverbosec                 C   sš   z't | jt dƒ�}|  tj|td�¡ W d   ƒ W d S 1 s w   Y  W d S  tyL   |rIt 	t
dt› dt› d| j› d| jj› d�	dd	�¡ Y d S w )
NÚr)Úobject_hookz
                        z  Unable to read z in
                        zS.  This may cause this or future pipx
                        operations involving zP to fail or
                        behave incorrectly.
                        rQ   rR   )rT   r2   rU   rH   rV   Úloadr    rX   rI   rY   r   r	   r<   )r   r\   rZ   r   r   r   r3   “   s0   ÿ&ÿÿÿþýùÿózPipxMetadata.read)T)r   N)F)r   r   r   r1   r   r.   r   r/   r8   r   r   rB   rF   rH   rK   r[   r3   r   r   r   r   r0   +   s   
 "


	r0   )rV   ÚloggingÚpathlibr   Útypingr   r   r   r   r   r   Úpipx.emojisr	   Ú	pipx.utilr
   r   Ú	getLoggerr   rI   rU   ÚJSONEncoderr   r   r    r!   r0   r   r   r   r   Ú<module>   s     
*