
    e                          d Z 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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    N   )core)backend)
completion)set_keyringget_passwordset_passworddelete_password)	platform_c                   h    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zed        Zed        Zy)CommandLineToolc                 j   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       g d| j                  _        | j                  j                  d| j                  j                  d       | j                  j                  dd       | j                  j                  dd       t        j                  | j                         y )Nz-pz--keyring-pathkeyring_pathzPath to the keyring backend)destdefaulthelpz-bz--keyring-backendkeyring_backendzName of the keyring backendz--list-backends
store_truezList keyring backends and exit)actionr   z	--disablezDisable keyring and exit)getsetdeldiagnose	operation?)choicesnargsservice)r   username)argparseArgumentParserparseradd_argument_operationsr   installselfs    -/usr/lib/python3/dist-packages/keyring/cli.py__init__zCommandLineTool.__init__   s.   --/  . 	! 	
 	  ". 	! 	
 	  1 	! 	

 	  3M 	! 	
 #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)r'   argvargskmethods        r(   runzCommandLineTool.run7   s    {{%%d+T
$t*%,,. a<<LLN>>Z'MMO!T^^$45tGxr*   c                     | j                   rB| j                  | j                  )| j                  j	                  | j                    d       y y y )Nz requires service and username)r   r   r   r"   errorr&   s    r(   r4   zCommandLineTool._check_argsM   sE    >>||#t}}'<!!T^^$44R"ST (= r*   c                 t    t        | j                  | j                        }|t        d      t	        |       y )Nr   )r   r   r   
SystemExitr2   r'   passwords     r(   do_getzCommandLineTool.do_getR   s-    dmm<Q-hr*   c                     | j                  d| j                   d| j                   d      }t        | j                  | j                  |       y )NzPassword for 'z' in 'z': )input_passwordr   r   r	   rA   s     r(   do_setzCommandLineTool.do_setX   sB    &&T]]O6$,,sC
 	T\\4==(;r*   c                 D    t        | j                  | j                         y N)r
   r   r   r&   s    r(   do_delzCommandLineTool.do_del^   s    dmm4r*   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existsr2   r   	data_root)r'   config_roots     r(   r   zCommandLineTool.diagnosea   sE    '').+..+z:lI//12r*   c                     | j                   j                  ddj                  | j                   j                         d       y )NzSpecify operation (z, z).)r"   r>   joinr$   r&   s    r(   r7   zCommandLineTool.invalid_opi   s3    /		$++:Q:Q0R/SSUVWr*   c                 F   | j                   y 	 | j                  r*t        j                  j	                  d| j                         t        t        j                  | j                                y # t        f$ 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"   r>   )r'   excs     r(   r5   z"CommandLineTool._load_spec_backendl   s    '
	J  4#4#45))$*>*>?@| 	J KK B3%HII	Js   AA. .
B 8BB c                 P    | j                         xs t        j                  |      S )zRetrieve password from input.)pass_from_pipegetpass)r'   prompts     r(   rE   zCommandLineTool.input_password|   s    ""$?(??r*   c                     t         j                  j                          }|xr- | j                  t         j                  j	                               S )z4Return password from pipe if not on TTY, else False.)rR   stdinisattystrip_last_newlineread)clsis_pipes     r(   rY   zCommandLineTool.pass_from_pipe   s:     ii&&((C311#))..2BCCr*   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)strslcs     r(   r_   z"CommandLineTool.strip_last_newline   s$     #,,t,B$73xr*   N)__name__
__module____qualname__r)   r<   r4   rC   rF   rI   r   r7   r5   rE   classmethodrY   staticmethodr_    r*   r(   r   r      sc    &(P,U
<53XJ @ D D
 	 	r*   r   c                 b    | t         j                  dd } t               }|j                  |       S )zMain command line interface.Nr   )rR   r8   r   r<   )r8   clis     r(   mainrr      s.     |xx|

C774=r*   __main__rH   )__doc__rZ   r    rR    r   r   r   r   r   r	   r
   utilr   r   rr   rj   exitro   r*   r(   <module>rx      sT    F   
    F F B BJ zCHHTV r*   