
    Ki                        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      Zdd	Zed
k(  r ej,                   e              yy)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                      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
 Zd Zd Zd ZddZddZ	ddZ
ddZd Zd Zd Zd Zd Zd Zed        Zed        Zy)CommandLineToolz
str | Nonekeyring_pathkeyring_backendstrget_modeoutput_format	operationserviceusernamec                   t        j                         | _        | j                  j                  dddd d       | j                  j                  dddd d	       | j                  j                  d
dd       | j                  j                  ddd       ddg| j                  _        | j                  j                  d| j                  j                  ddd       ddg| j                  _        | j                  j                  d| j                  j
                  ddd       g d| j                  _        | j                  j                  d| j                  j                  d       | j                  j                  dd        | j                  j                  d!d        t        j                  | j                         y )"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selfs    W/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/keyring/cli.py__init__zCommandLineTool.__init__"   s   --/  . 	! 	
 	  ". 	! 	
 	  1 	! 	

 	  3M 	! 	
 #-g!6  KK** 	! 	
 (/&7#  KK//  	! 
	
 #D  KK++ 	! 	

 	   	! 	
 	   	! 	
 	4;;'    c                   | j                   j                  |      }t        |       j                  t        |             |j                  r%t        j                         D ]  }t        |        y |j                  rt        j                          y |j                  dk(  r| j                          y | j                          | j                          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)r3   argvargskmethods        r4   runzCommandLineTool.rund   s    {{%%d+T
$t*%,,. a<<LLN>>Z'MMO!T^^$45tGxr6   c                "     j                   dk7  xs  j                  dk7  }dgdg|z  z   t         j                         z  }t         fd|D              r: j                  j                   j                    ddj                  |              y y )Nr$   r    r   r   c              3  :   K   | ]  }t        |      d u   y wN)rB   ).0paramr3   s     r4   	<genexpr>z.CommandLineTool._check_args.<locals>.<genexpr>}   s     BwtU#t+Bs   z
 requires z and )r   r   boolanyr,   errorjoin)r3   needs_usernamerequireds   `  r4   r@   zCommandLineTool._check_argsz   s    50LDMMW4LK:,"??4CWWBBBKK 0
7<<;Q:RST Cr6   c                     t        | d| j                                }|t        d       t        | d| j                         |       y )N_get_r   _emit_)rB   r   
SystemExitr   r3   
credentials     r4   do_getzCommandLineTool.do_get   sM    ;WTU4==/#:;=
Q-4t11234Z@r6   c                \    t        t        j                  |j                                      y rK   )r>   r#   dumps_varsrY   s     r4   
_emit_jsonzCommandLineTool._emit_json   s    djj))+,-r6   c                `    |j                         j                         D ]  }t        |        y rK   )r^   valuesr>   )r3   rZ   vals      r4   _emit_plainzCommandLineTool._emit_plain   s*    ##%,,. 	C#J	r6   c                B    t        | j                  | j                        S rK   )r
   r   r   r2   s    r4   
_get_credszCommandLineTool._get_creds   s    dllDMM::r6   c                t    t        | j                  | j                        }|t        j                  |      S d S rK   )r   r   r   r   AnonymousCredentialr3   r   s     r4   _get_passwordzCommandLineTool._get_password   s6    dmm<9A9MK++H5	
SW	
r6   c                    | j                  d| j                   d| j                   d      }t        | j                  | j                  |       y )NzPassword for 'z' in 'z': )input_passwordr   r   r   rh   s     r4   do_setzCommandLineTool.do_set   sB    &&T]]O6$,,sC
 	T\\4==(;r6   c                D    t        | j                  | j                         y rK   )r	   r   r   r2   s    r4   do_delzCommandLineTool.do_del   s    dmm4r6   c                    t        j                         }|j                         rt        d|       nt        d|d       t        dt	        j
                                y )Nzconfig path:z(absent)z
data root:)r   _config_pathexistsr>   r   	data_root)r3   config_roots     r4   r'   zCommandLineTool.diagnose   sE    '').+..+z:lI//12r6   c                    | j                   j                  ddj                  | j                   j                         d       y )NzSpecify operation (z, z).)r,   rQ   rR   r0   r2   s    r4   rC   zCommandLineTool.invalid_op   s3    /		$++:Q:Q0R/SSUVWr6   c                D   | j                   y 	 | j                  r*t        j                  j	                  d| j                         t        t        j                  | j                                y # t        $ r(}| j                  j                  d|        Y d }~y d }~ww xY w)Nr   z"Unable to load specified keyring: )r   r   syspathinsertr   r   load_keyring	Exceptionr,   rQ   )r3   excs     r4   rA   z"CommandLineTool._load_spec_backend   s    '
	J  4#4#45))$*>*>?@ 	J KK B3%HII	Js   AA. .	B7BBc                P    | j                         xs t        j                  |      S )zRetrieve password from input.)pass_from_pipegetpass)r3   prompts     r4   rk   zCommandLineTool.input_password   s    ""$?(??r6   c                    t         j                  j                          }|xr- | j                  t         j                  j	                               S )z4Return password from pipe if not on TTY, else False.)rv   stdinisattystrip_last_newlineread)clsis_pipes     r4   r}   zCommandLineTool.pass_from_pipe   s:     ii&&((C311#))..2BCCr6   c                H    t        | j                  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   slcs     r4   r   z"CommandLineTool.strip_last_newline   s$     #,,t,B$73xr6   N)rZ   zcredentials.Credential)returnzcredentials.Credential | None)__name__
__module____qualname____annotations__r5   rH   r@   r[   r_   rc   re   ri   rl   rn   r'   rC   rA   rk   classmethodr}   staticmethodr    r6   r4   r   r      s    MNLM@(D,UA.;
<53XJ @ D D
 	 	r6   r   c                b    | t         j                  dd } t               }|j                  |       S )zMain command line interface.Nr   )rv   rD   r   rH   )rD   clis     r4   mainr      s.     |xx|

C774=r6   __main__rK   )__doc__
__future__r   r*   r~   r#   rv    r   r   r   r   r	   r
   r   r   r   utilr   r   r   r   exitr   r6   r4   <module>r      s\    F "    

 
 
 v vr zCHHTV r6   