mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Refactor the fixed getpass into its own module
This commit is contained in:
parent
0866a75579
commit
9a9703242e
@ -8,7 +8,7 @@ import sys
|
||||
from functools import partial
|
||||
|
||||
from calibre import prints
|
||||
from calibre.constants import preferred_encoding, iswindows
|
||||
from calibre.constants import preferred_encoding
|
||||
|
||||
# Manage users CLI {{{
|
||||
|
||||
@ -70,31 +70,7 @@ def manage_users_cli(path=None):
|
||||
return get_valid(_('Enter the username'), validate)
|
||||
|
||||
def get_pass(username):
|
||||
|
||||
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)
|
||||
from calibre.utils.getpass import getpass
|
||||
|
||||
while True:
|
||||
one = getpass(
|
||||
|
36
src/calibre/utils/getpass.py
Normal file
36
src/calibre/utils/getpass.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user