From 2478ccd357efa7c38cc4acbfe2bfab2f3a8bf0cd Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Wed, 19 Nov 2008 03:31:29 -0700 Subject: Fixed some unicode encoded/decode issues in textui.prompt_password() and textui.prompt() --- ipalib/cli.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'ipalib/cli.py') diff --git a/ipalib/cli.py b/ipalib/cli.py index b3aa1099..37fdad44 100644 --- a/ipalib/cli.py +++ b/ipalib/cli.py @@ -98,6 +98,30 @@ class textui(backend.Backend): return max(len(row) for row in rows) return max(len(row[col]) for row in rows) + def __get_encoding(self, stream): + assert stream in (sys.stdin, sys.stdout) + if stream.encoding is None: + if stream.isatty(): + return sys.getdefaultencoding() + return 'UTF-8' + return stream.encoding + + def decode(self, str_buffer): + """ + Decode text from stdin. + """ + assert type(str_buffer) is str + encoding = self.__get_encoding(sys.stdin) + return str_buffer.decode(encoding) + + def encode(self, unicode_text): + """ + Encode text for output to stdout. + """ + assert type(unicode_text) is unicode + encoding = self.__get_encoding(sys.stdout) + return unicode_text.encode(encoding) + def choose_number(self, n, singular, plural=None): if n == 1 or plural is None: return singular % n @@ -327,11 +351,14 @@ class textui(backend.Backend): """ Prompt user for input. """ + # TODO: Add tab completion using readline if default is None: - prompt = '%s: ' % label + prompt = u'%s: ' % label else: - prompt = '%s [%s]: ' % (label, default) - return raw_input(prompt) + prompt = u'%s [%s]: ' % (label, default) + return self.decode( + raw_input(self.encode(prompt)) + ) def prompt_password(self, label): """ @@ -342,7 +369,7 @@ class textui(backend.Backend): pw1 = getpass.getpass('%s: ' % label) pw2 = getpass.getpass('Enter again to verify: ') if pw1 == pw2: - return pw1 + return self.decode(pw1) print ' ** Passwords do not match. Please enter again. **' except KeyboardInterrupt: print '' -- cgit