o
    Vh                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZmZmZmZmZ ddlmZ G dd dZdd	d
ZedkrLee  dS dS )z@Simple command line interface to get/set password from a keyring    )annotationsN   )	backend
completioncorecredentialsdelete_passwordget_credentialget_passwordset_keyringset_password)	platform_c                   @  s   e Zd ZU ded< ded< ded< ded< ded< ded< ded	< d
d Zdd Zdd Zdd Zd/ddZd/ddZ	d0ddZ
d0ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zed*d+ Zed,d- Zd.S )1CommandLineToolz
str | Nonekeyring_pathkeyring_backendstrget_modeoutput_format	operationserviceusernamec                 C  s   t  | _| jjdddd dd | jjdddd d	d | jjd
ddd | jjdddd ddg| j_| jjd| jjdddd ddg| j_| jjd| jjdddd g d| j_| jjd| jjdd | jjddd  | jjd!dd  t| j d S )"Nz-pz--keyring-pathr   zPath to the keyring backend)destdefaulthelpz-bz--keyring-backendr   zName of the keyring backendz--list-backends
store_truezList keyring backends and exit)actionr   z	--disablezDisable keyring and exitpasswordcredsz--moder   a  
            Mode for 'get' operation.
            'password' requires a username and will return only the password.
            'creds' does not require a username and will return both the username and password separated by a newline.

            Default is 'password'
            )choicesr   r   r   plainjsonz--outputr   z\
            Output format for 'get' operation.

            Default is 'plain'
            )getsetdeldiagnoser   ?)r   nargsr   )r&   r   )	argparseArgumentParserparseradd_argument
_get_modes_output_formats_operationsr   installself r1   _/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/keyring/cli.py__init__"   sl   
zCommandLineTool.__init__c                 C  s   | j |}t| t| |jrt D ]}t| qd S |jr(t	  d S |j
dkr3|   d S |   |   t| d| j
 | j}| S )Nr$   do_)r)   
parse_argsvarsupdatelist_backendsr   get_all_keyringprintdisabler   r   r$   _check_args_load_spec_backendgetattr
invalid_op)r0   argvargskmethodr1   r1   r2   rund   s    

zCommandLineTool.runc                   sj    j dkp	 jdk}dgdg|  t j  }t fdd|D r3 j j  dd|  d S d S )	Nr!   r   r   r   c                 3  s    | ]
}t  |d u V  qd S N)r>   ).0paramr/   r1   r2   	<genexpr>}   s    z.CommandLineTool._check_args.<locals>.<genexpr>z
 requires z and )r   r   boolanyr)   errorjoin)r0   needs_usernamerequiredr1   r/   r2   r<   z   s
   "zCommandLineTool._check_argsc                 C  s>   t | d| j  }|d u rtdt | d| j | d S )N_get_r   _emit_)r>   r   
SystemExitr   r0   
credentialr1   r1   r2   do_get   s   zCommandLineTool.do_getrS   credentials.Credentialc                 C  s   t t|  d S rE   )r:   r    dumps_varsrR   r1   r1   r2   
_emit_json   s   zCommandLineTool._emit_jsonc                 C  s   |   D ]}t| qd S rE   )rW   valuesr:   )r0   rS   valr1   r1   r2   _emit_plain   s   
zCommandLineTool._emit_plainreturncredentials.Credential | Nonec                 C  s   t | j| jS rE   )r	   r   r   r/   r1   r1   r2   
_get_creds   s   zCommandLineTool._get_credsc                 C  s$   t | j| j}|d urt|S d S rE   )r
   r   r   r   AnonymousCredentialr0   r   r1   r1   r2   _get_password   s
   zCommandLineTool._get_passwordc                 C  s0   |  d| j d| j d}t| j| j| d S )NzPassword for 'z' in 'z': )input_passwordr   r   r   r`   r1   r1   r2   do_set   s   zCommandLineTool.do_setc                 C  s   t | j| j d S rE   )r   r   r   r/   r1   r1   r2   do_del   s   zCommandLineTool.do_delc                 C  s:   t  }| rtd| ntd|d tdt  d S )Nzconfig path:z(absent)z
data root:)r   _config_pathexistsr:   r   	data_root)r0   config_rootr1   r1   r2   r$      s
   zCommandLineTool.diagnosec                 C  s"   | j dd| j j d d S )NzSpecify operation (z, z).)r)   rK   rL   r-   r/   r1   r1   r2   r?      s   "zCommandLineTool.invalid_opc              
   C  st   | j d u rd S z| jrtjd| j tt| j  W d S  ty9 } z| j	
d|  W Y d }~d S d }~ww )Nr   z"Unable to load specified keyring: )r   r   syspathinsertr   r   load_keyring	Exceptionr)   rK   )r0   excr1   r1   r2   r=      s   
 z"CommandLineTool._load_spec_backendc                 C  s   |   pt|S )zRetrieve password from input.)pass_from_pipegetpass)r0   promptr1   r1   r2   rb      s   zCommandLineTool.input_passwordc                 C  s    t j  }|o| t j S )z4Return password from pipe if not on TTY, else False.)ri   stdinisattystrip_last_newlineread)clsis_piper1   r1   r2   ro      s   zCommandLineTool.pass_from_pipec                 C  s   t | drdnd}| | S )zStrip one last newline, if present.

        >>> CommandLineTool.strip_last_newline('foo')
        'foo'
        >>> CommandLineTool.strip_last_newline('foo\n')
        'foo'
        
N)sliceendswith)r   slcr1   r1   r2   rt      s   	z"CommandLineTool.strip_last_newlineN)rS   rU   )r\   r]   )__name__
__module____qualname____annotations__r3   rD   r<   rT   rX   r[   r^   ra   rc   rd   r$   r?   r=   rb   classmethodro   staticmethodrt   r1   r1   r1   r2   r      s4   
 B




r   c                 C  s&   | du rt jdd } t }|| S )zMain command line interface.Nr   )ri   r@   r   rD   )r@   clir1   r1   r2   main   s   
r   __main__rE   )__doc__
__future__r   r'   rp   r    ri    r   r   r   r   r   r	   r
   r   r   utilr   r   r   r}   exitr1   r1   r1   r2   <module>   s    , 
:
