o
    Vi                     @  s   U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZ er9ddlmZ ddlmZ eeZd	ed
< eG dd deZeG dd deZG dd dZG dd dZG dd deZG dd deZg dZdS )zMCache Protocol and built-in implementations for Python interpreter discovery.    )annotationsN)contextmanagersuppress)sha256)TYPE_CHECKINGFinalProtocolruntime_checkable)	Generator)PathzFinal[logging.Logger]_LOGGERc                   @  F   e Zd ZdZdddZdddZdddZdddZedddZ	dS )ContentStorez/A store for reading and writing cached content.returnboolc                 C     dS )z)Return whether the cached content exists.N selfr   r   K/home/ubuntu/.local/lib/python3.10/site-packages/python_discovery/_cache.pyexists      zContentStore.existsdict | Nonec                 C  r   )z?Read the cached content, or ``None`` if unavailable or corrupt.Nr   r   r   r   r   read   r   zContentStore.readcontentdictNonec                 C  r   )zi
        Persist *content* to the store.

        :param content: interpreter metadata to cache.
        Nr   r   r   r   r   r   write      zContentStore.writec                 C  r   )zDelete the cached content.Nr   r   r   r   r   remove&   r   zContentStore.removeGenerator[None]c                 C  r   )z>Context manager that acquires an exclusive lock on this store.Nr   r   r   r   r   locked*   s   zContentStore.lockedNr   r   r   r   r   r   r   r   r   r   r   r!   
__name__
__module____qualname____doc__r   r   r   r    r   r"   r   r   r   r   r      s    



r   c                   @  $   e Zd ZdZdddZdd	d
ZdS )PyInfoCachez3Cache interface for Python interpreter information.pathr   r   r   c                 C  r   )
        Return the content store for the interpreter at *path*.

        :param path: absolute path to a Python executable.
        Nr   r   r/   r   r   r   py_info4   r   zPyInfoCache.py_infor   c                 C  r   )*Remove all cached interpreter information.Nr   r   r   r   r   py_info_clear<   r   zPyInfoCache.py_info_clearN)r/   r   r   r   r&   r)   r*   r+   r,   r2   r4   r   r   r   r   r.   0   s    
r.   c                   @  s^   e Zd ZdZddd	Zedd
dZdddZdddZd ddZ	d!ddZ
ed"ddZdS )#DiskContentStorez0JSON file-based content store with file locking.folderr   keystrr   r   c                 C  s   || _ || _d S N_folder_key)r   r7   r8   r   r   r   __init__D   s   
zDiskContentStore.__init__c                 C  s   | j | j d S )N.jsonr;   r   r   r   r   _fileH   s   zDiskContentStore._filer   c                 C  s
   | j  S r:   )r@   r   r   r   r   r   r   L      
zDiskContentStore.existsr   c                 C  s   d\}}zt | jjdd}W n ty   d}Y n ty,   tjd| jdd Y n
w td| j |S |rQtt | 	  W d    d S 1 sLw   Y  d S )NNFutf-8encodingTzfailed to read %s)exc_infozgot python info from %s)
jsonloadsr@   	read_text
ValueErrorOSErrorr   debugr   r    )r   data
bad_formatr   r   r   r   O   s"   


zDiskContentStore.readr   r   c                 C  s>   | j jddd | jjtj|ddddd td| j d S )NTparentsexist_ok   )	sort_keysindentrC   rD   zwrote python info at %s)r<   mkdirr@   
write_textrG   dumpsr   rL   r   r   r   r   r   _   s   zDiskContentStore.writec                 C  sD   t t | j  W d    n1 sw   Y  td| j d S )Nzremoved python info at %s)r   rK   r@   unlinkr   rL   r   r   r   r   r    d   s   
zDiskContentStore.remover!   c                 c  sh    ddl m} | j| j d }|jjddd |t| d V  W d    d S 1 s-w   Y  d S )Nr   )FileLockz.lockTrO   )filelockrY   r<   r=   parentrU   r9   )r   rY   	lock_pathr   r   r   r"   i   s   "zDiskContentStore.lockedN)r7   r   r8   r9   r   r   r   r   r#   r$   r%   r&   r'   )r)   r*   r+   r,   r>   propertyr@   r   r   r   r    r   r"   r   r   r   r   r6   A   s    




r6   c                   @  s<   e Zd ZdZdddZeddd	ZdddZdddZdS )	DiskCachez
    File-system based Python interpreter info cache (``<root>/py_info/4/<sha256>.json``).

    :param root: root directory for the on-disk cache.
    rootr   r   r   c                 C  s
   || _ d S r:   _root)r   r`   r   r   r   r>   z   rA   zDiskCache.__init__c                 C  s   | j d d S )Nr2   4ra   r   r   r   r   _py_info_dir}   s   zDiskCache._py_info_dirr/   r6   c                 C  s"   t t|d }t| j|S )r0   rC   )r   r9   encode	hexdigestr6   rd   )r   r/   r8   r   r   r   r2      s   zDiskCache.py_infoc              	   C  s^   | j }| r+| D ]!}|jdkr*tt |  W d   n1 s%w   Y  qdS dS )r3   r?   N)rd   r   iterdirsuffixr   rK   rX   )r   r7   entryr   r   r   r4      s   


zDiskCache.py_info_clearN)r`   r   r   r   r]   )r/   r   r   r6   r&   )	r)   r*   r+   r,   r>   r^   rd   r2   r4   r   r   r   r   r_   s   s    

	r_   c                   @  r   )NoOpContentStorezDContent store that does nothing -- implements ContentStore protocol.r   r   c                 C  r   rB   r   r   r   r   r   r         zNoOpContentStore.existsr   c                 C     d S r:   r   r   r   r   r   r      rk   zNoOpContentStore.readr   r   r   c                 C  rl   r:   r   r   r   r   r   r      rk   zNoOpContentStore.writec                 C  rl   r:   r   r   r   r   r   r       rk   zNoOpContentStore.remover!   c                 c  s    d V  d S r:   r   r   r   r   r   r"      s   
zNoOpContentStore.lockedNr#   r$   r%   r&   r'   r(   r   r   r   r   rj      s    



rj   c                   @  r-   )	NoOpCachez;Cache that does nothing -- implements PyInfoCache protocol.r/   r   r   rj   c                 C  s   t  S r:   )rj   r1   r   r   r   r2      s   zNoOpCache.py_infor   c                 C  rl   r:   r   r   r   r   r   r4      rk   zNoOpCache.py_info_clearN)r/   r   r   rj   r&   r5   r   r   r   r   rm      s    
rm   )r   r_   r6   rm   rj   r.   )r,   
__future__r   rG   logging
contextlibr   r   hashlibr   typingr   r   r   r	   collections.abcr
   pathlibr   	getLoggerr)   r   __annotations__r   r.   r6   r_   rj   rm   __all__r   r   r   r   <module>   s(    2!
