o
    2.a                     @   sZ   d dl Z d dlZd dlZddlmZ ddlmZ ddlmZ ddlmZ G dd dZ	dS )	    N   )	constants)errors)create_environment_dict)find_executablec                   @   s>   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd ZdS )StoreNc                 C   s>   t j| | _t| j| _|| _| jdu rtd| jdS )z Create a store object that acts as an interface to
            perform the basic operations for storing, retrieving
            and erasing credentials using `program`.
        N){} not installed or not available in PATH)	r   ZPROGRAM_PREFIXprogramr   exeenvironmentr   ZInitializationErrorformat)selfr	   r    r   :/usr/lib/python3/dist-packages/docker/credentials/store.py__init__   s   
zStore.__init__c                 C   s^   t |ts
|d}| d|}t|d}|d dkr-|d dkr-td| j	 |S )zt Retrieve credentials for `server`. If no credentials are found,
            a `StoreError` will be raised.
        utf-8getUsername SecretzNo matching credentials in )

isinstancebytesencode_executejsonloadsdecoder   ZCredentialsNotFoundr	   )r   serverdataresultr   r   r   r      s   


z	Store.getc                 C   s$   t |||dd}| d|S )z_ Store credentials for `server`. Raises a `StoreError` if an error
            occurs.
        )Z	ServerURLr   r   r   store)r   dumpsr   r   )r   r   ZusernameZsecret
data_inputr   r   r   r    /   s   zStore.storec                 C   s$   t |ts
|d}| d| dS )z_ Erase credentials for `server`. Raises a `StoreError` if an error
            occurs.
        r   eraseN)r   r   r   r   )r   r   r   r   r   r#   :   s   

zStore.erasec                 C   s   |  dd}t|dS )zB List stored credentials. Requires v0.4.0+ of the helper.
        listNr   )r   r   r   r   )r   r   r   r   r   r$   B   s   z
Store.listc              
   C   s   d }t | j}ztj| j|g||d}W |S  tjy) } zt|| jd }~w t	yN } z|j
t
jkr?td| jtd|j|j
d }~ww )N)inputenvr   z"Unexpected OS error "{}", errno={})r   r   
subprocessZcheck_outputr
   ZCalledProcessErrorr   Zprocess_store_errorr	   OSErrorerrnoENOENTZ
StoreErrorr   strerror)r   Zsubcmdr"   outputr&   er   r   r   r   H   s0   
zStore._execute)N)	__name__
__module____qualname__r   r   r    r#   r$   r   r   r   r   r   r      s    
r   )
r)   r   r'   r   r   r   Zutilsr   r   r   r   r   r   r   <module>   s    