diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-11-19 03:31:29 -0700 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-11-19 03:31:29 -0700 |
commit | 2478ccd357efa7c38cc4acbfe2bfab2f3a8bf0cd (patch) | |
tree | 443db44161471d3f306ff125a6c9b33321519014 | |
parent | 500b8166811e39ac63bdbaee8dcf01eb0643d868 (diff) | |
download | freeipa.git-2478ccd357efa7c38cc4acbfe2bfab2f3a8bf0cd.tar.gz freeipa.git-2478ccd357efa7c38cc4acbfe2bfab2f3a8bf0cd.tar.xz freeipa.git-2478ccd357efa7c38cc4acbfe2bfab2f3a8bf0cd.zip |
Fixed some unicode encoded/decode issues in textui.prompt_password() and textui.prompt()
-rw-r--r-- | ipalib/cli.py | 35 |
1 files changed, 31 insertions, 4 deletions
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 '' |