o
    ÞñÐaT  ã                   @   s€   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mZm	Z	m
Z
 G dd„ dƒZdd	d
„Zedkr>e eƒ ¡ dS dS )z@Simple command line interface to get/set password from a keyringé    Né   )Úcore)Úbackend)Úset_keyringÚget_passwordÚset_passwordÚdelete_passwordc                   @   sl   e Zd Zdd„ Zdd„ Zd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 )ÚCommandLineToolc                 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� | jjdddd� | jjddd� | jjddd� d S )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Ú	operationzget|set|delú?)r   ÚnargsÚservice)r   Úusername)ÚargparseÚArgumentParserÚparserÚadd_argument©Úself© r   ú-/usr/lib/python3/dist-packages/keyring/cli.pyÚ__init__   sH   
ûûýÿýþ
þzCommandLineTool.__init__c                 C   s|   | j  |¡}t| ƒ t|ƒ¡ |jrt ¡ D ]}t|ƒ qd S |jr(t	 ¡  d S |  
¡  |  ¡  t| d| j› �| jƒ}|ƒ S )NÚdo_)r   Ú
parse_argsÚvarsÚupdateÚlist_backendsr   Úget_all_keyringÚprintÚdisabler   Ú_check_argsÚ_load_spec_backendÚgetattrr   Ú
invalid_op)r   ÚargvÚargsÚkÚmethodr   r   r   Úrun4   s   
zCommandLineTool.runc                 C   s:   | j r| jd u s| jd u r| j | j › d�¡ d S d S d S )Nz requires service and username)r   r   r   r   Úerrorr   r   r   r   r'   F   s
   þzCommandLineTool._check_argsc                 C   s*   t | j| jƒ}|d u rtdƒ‚t|ƒ d S )Nr   )r   r   r   Ú
SystemExitr%   ©r   Úpasswordr   r   r   Údo_getK   s   zCommandLineTool.do_getc                 C   s0   |   d| j› d| j› d�¡}t| j| j|ƒ d S )NzPassword for 'z' in 'z': )Úinput_passwordr   r   r   r2   r   r   r   Údo_setQ   s   ÿzCommandLineTool.do_setc                 C   s   t | j| jƒ d S ©N)r   r   r   r   r   r   r   Údo_delW   s   zCommandLineTool.do_delc                 C   s   | j  d¡ d S )Nz)Specify operation 'get', 'del', or 'set'.)r   r0   r   r   r   r   r*   Z   s   zCommandLineTool.invalid_opc              
   C   sv   | j d u rd S z| jrtj d| j¡ tt | j ¡ƒ W d S  tfy: } 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   r0   )r   Úexcr   r   r   r(   ]   s   
 €úz"CommandLineTool._load_spec_backendc                 C   s   |   ¡ pt |¡S )zRetrieve password from input.)Úpass_from_pipeÚgetpass)r   Úpromptr   r   r   r5   m   s   zCommandLineTool.input_passwordc                 C   s    t j ¡  }|o|  t j ¡ ¡S )z4Return password from pipe if not on TTY, else False.)r9   ÚstdinÚisattyÚstrip_last_newlineÚread)ÚclsÚis_piper   r   r   r?   q   s   zCommandLineTool.pass_from_pipec                 C   s   | d|   d¡ … S )z#Strip one last newline, if present.NÚ
)Úendswith)Ústrr   r   r   rD   w   s   z"CommandLineTool.strip_last_newlineN)Ú__name__Ú
__module__Ú__qualname__r   r/   r'   r4   r6   r8   r*   r(   r5   Úclassmethodr?   ÚstaticmethodrD   r   r   r   r   r	      s    &
r	   c                 C   s&   | du rt jdd… } tƒ }| | ¡S )zMain command line interface.Nr   )r9   r+   r	   r/   )r+   Úclir   r   r   Úmain}   s   
rQ   Ú__main__r7   )Ú__doc__r@   r   r9   Ú r   r   r   r   r   r   r	   rQ   rK   Úexitr   r   r   r   Ú<module>   s   
p
ÿ