From 1ab91440f17b7a7a3acf08e4353a3bb8ffaa1690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Thu, 29 May 2014 14:55:06 +0200 Subject: [PATCH] [fix] Fix encoding issue due to Python 2 in i18n and cli prompting --- moulinette/core.py | 4 ++-- moulinette/interfaces/cli.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/moulinette/core.py b/moulinette/core.py index 580eb0e0..29bf92d3 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -191,7 +191,7 @@ class Translator(object): logging.warning("unknown key '%s' for locale '%s'" % (key, self.default_locale)) return key - return value.encode('utf-8') + return value def _load_translations(self, locale, overwrite=False): """Load translations for a locale @@ -212,7 +212,7 @@ class Translator(object): try: with open('%s/%s.json' % (self.locale_dir, locale), 'r') as f: - j = json.load(f) + j = json.load(f, 'utf-8') except IOError: return False else: diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 38be8426..dd5b4b1a 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -1,5 +1,8 @@ # -*- coding: utf-8 -*- +# TODO: Switch to python3! +from __future__ import print_function + import os import errno import getpass @@ -32,7 +35,7 @@ def colorize(astr, color): """ if os.isatty(1): - return '\033[{:d}m\033[1m{:s}\033[m'.format(colors_codes[color], astr) + return u'\033[{:d}m\033[1m{:s}\033[m'.format(colors_codes[color], astr) else: return astr @@ -226,10 +229,13 @@ class Interface(BaseInterface): """ if is_password: - prompt = lambda m: getpass.getpass(colorize(m18n.g('colon') % m, - 'blue')) + def prompt(m): + print(colorize(m18n.g('colon') % m, 'blue'), end='') + return getpass.getpass() else: - prompt = lambda m: raw_input(colorize(m18n.g('colon') % m, 'blue')) + def prompt(m): + print(colorize(m18n.g('colon') % m, 'blue'), end='') + return raw_input() value = prompt(message) if confirm: