o aT@sdZddlZddlZddlZddlmZddlmZddlmZmZm Z m Z GdddZ d d d Z e d kr>ee dSdS) z@Simple command line interface to get/set password from a keyringN)core)backend) set_keyring get_password set_passworddelete_passwordc@sleZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ e ddZ eddZdS)CommandLineToolcCst|_|jjdddddd|jjddddd d|jjd d d d |jjdd dd |jjdddd|jjddd|jjddddS)Nz-pz--keyring-path keyring_pathzPath to the keyring backend)destdefaulthelpz-bz--keyring-backendkeyring_backendzName of the keyring backendz--list-backends store_truezList keyring backends and exit)actionr z --disablezDisable keyring and exit operationz get|set|del?)r nargsservice)rusername)argparseArgumentParserparser add_argumentselfr-/usr/lib/python3/dist-packages/keyring/cli.py__init__sH  zCommandLineTool.__init__cCs||j|}t|t||jrtD]}t|qdS|jr(t dS| | t |d|j |j}|S)Ndo_)r parse_argsvarsupdate list_backendsrget_all_keyringprintdisabler _check_args_load_spec_backendgetattrr invalid_op)rargvargskmethodrrrrun4s   zCommandLineTool.runcCs:|jr|jdus |jdur|j|jddSdSdS)Nz requires service and username)rrrrerrorrrrrr'Fs zCommandLineTool._check_argscCs*t|j|j}|durtdt|dS)Nr)rrr SystemExitr%rpasswordrrrdo_getKs zCommandLineTool.do_getcCs0|d|jd|jd}t|j|j|dS)NzPassword for 'z' in 'z': )input_passwordrrrr2rrrdo_setQszCommandLineTool.do_setcCst|j|jdSN)rrrrrrrdo_delWszCommandLineTool.do_delcCs|jddS)Nz)Specify operation 'get', 'del', or 'set'.)rr0rrrrr*ZszCommandLineTool.invalid_opc Csv|jdurdSz|jrtjd|jtt|jWdStfy:}z|j d|WYd}~dSd}~ww)Nrz"Unable to load specified keyring: ) rr syspathinsertrr load_keyring Exceptionrr0)rexcrrrr(]s  z"CommandLineTool._load_spec_backendcCs|pt|S)zRetrieve password from input.)pass_from_pipegetpass)rpromptrrrr5mszCommandLineTool.input_passwordcCs tj }|o|tjS)z4Return password from pipe if not on TTY, else False.)r9stdinisattystrip_last_newlineread)clsis_piperrrr?qs zCommandLineTool.pass_from_pipecCs|d|d S)z#Strip one last newline, if present.N )endswith)strrrrrDwsz"CommandLineTool.strip_last_newlineN)__name__ __module__ __qualname__rr/r'r4r6r8r*r(r5 classmethodr? staticmethodrDrrrrr s& r cCs&|dur tjdd}t}||S)zMain command line interface.Nr)r9r+r r/)r+clirrrmain}s rQ__main__r7)__doc__r@rr9rrrrrrr rQrKexitrrrrs   p