Refactor the fixed getpass into its own module

This commit is contained in:
Kovid Goyal 2017-08-04 13:26:03 +05:30
parent 0866a75579
commit 9a9703242e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 38 additions and 26 deletions

View File

@ -8,7 +8,7 @@ import sys
from functools import partial from functools import partial
from calibre import prints from calibre import prints
from calibre.constants import preferred_encoding, iswindows from calibre.constants import preferred_encoding
# Manage users CLI {{{ # Manage users CLI {{{
@ -70,31 +70,7 @@ def manage_users_cli(path=None):
return get_valid(_('Enter the username'), validate) return get_valid(_('Enter the username'), validate)
def get_pass(username): def get_pass(username):
from calibre.utils.getpass import getpass
def getpass(prompt):
if iswindows:
# getpass is broken on windows with python 2.x and unicode, the
# below implementation is from the python 3 source code
import msvcrt
for c in prompt:
msvcrt.putwch(c)
pw = ""
while 1:
c = msvcrt.getwch()
if c == '\r' or c == '\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
pw = pw[:-1]
else:
pw = pw + c
msvcrt.putwch('\r')
msvcrt.putwch('\n')
return pw
else:
from getpass import getpass
return getpass(prompt).decode(enc)
while True: while True:
one = getpass( one = getpass(

View File

@ -0,0 +1,36 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
from calibre.constants import iswindows, preferred_encoding
def getpass(prompt):
if iswindows:
# getpass is broken on windows with python 2.x and unicode, the
# below implementation is from the python 3 source code
import msvcrt
for c in prompt:
msvcrt.putwch(c)
pw = ""
while 1:
c = msvcrt.getwch()
if c == '\r' or c == '\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
pw = pw[:-1]
else:
pw = pw + c
msvcrt.putwch('\r')
msvcrt.putwch('\n')
return pw
else:
enc = getattr(sys.stdin, 'encoding', preferred_encoding) or preferred_encoding
from getpass import getpass
return getpass(prompt).decode(enc)