U
    ܦ\                     @   s   d Z ddlmZ ddlZddlmZ ddlZddlmZ ddlm	Z	 ddlm
Z
mZmZmZ eZG d	d
 d
ZdddZedkree  dS )z@Simple command line interface to get/set password from a keyring    )print_functionN)OptionParser   )core)backend)set_keyringget_passwordset_passworddelete_passwordc                   @   sD   e Zd Zdd Zdd Zdd Zedd Zed	d
 Z	dd Z
dS )CommandLineToolc                 C   s`   t dd| _| 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 S )Nz$%prog [get|set|del] SERVICE USERNAME)Zusagez-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)r   parserZ
add_option)self r   -/usr/lib/python3/dist-packages/keyring/cli.py__init__   s*    
 
 zCommandLineTool.__init__c           
      C   s  | j |\}}|jr0t D ]}t| qd S |jrBt  d S z|\}}}W n< tk
r   t	|dkr|| j 
  Y dS | j d Y nX |jd k	rz*|jrtjd|j tt|j W n2 tfk
r   t d }| j d|  Y nX |dkr&t||}	|	d krdS | |	 dS |dkrR| d||f }	t|||	 dS |dkr|| d	||f }	t|| dS | j d
 d S )Nr   r   zWrong number of argumentsz$Unable to load specified keyring: %sgetsetzPassword for '%s' in '%s': delz$Deleting password for '%s' in '%s': z.You can only 'get', 'del' or 'set' a password.)r   
parse_argsZlist_backendsr   Zget_all_keyringprintdisabler   
ValueErrorlenZ
print_helperrorr   r   syspathinsertr   Zload_keyring	Exceptionexc_infor   output_passwordinput_passwordr	   r
   )
r   argvZoptsargskZkindserviceZusernameepasswordr   r   r   run#   sZ    









zCommandLineTool.runc                 C   s   |   pt|S )z&Retrieve password from input.
        )pass_from_pipegetpass)r   promptr   r   r   r'   a   s    zCommandLineTool.input_passwordc                 C   s    t j  }|o| t j S )z=Return password from pipe if not on TTY, else False.
        )r!   stdinisattystrip_last_newlineread)clsZis_piper   r   r   r/   f   s    zCommandLineTool.pass_from_pipec                 C   s   | d|  d  S )z#Strip one last newline, if present.N
)endswith)strr   r   r   r4   m   s    z"CommandLineTool.strip_last_newlinec                 C   s   t |tjd dS )zbOutput the password to the user.

        This mostly exists to ease the testing process.
        )fileN)r   r!   stdout)r   r-   r   r   r   r&   r   s    zCommandLineTool.output_passwordN)__name__
__module____qualname__r   r.   r'   classmethodr/   staticmethodr4   r&   r   r   r   r   r      s   >

r   c                 C   s&   | dkrt jdd } t }|| S )zMain command line interface.Nr   )r!   r(   r   r.   )r(   Zclir   r   r   main{   s    rA   __main__)N)__doc__Z
__future__r   r0   Zoptparser   r!    r   r   r   r   r	   r
   typeZ__metaclass__r   rA   r<   exitr   r   r   r   <module>   s   j

