[weboob] [PATCH 1/4] formatters: use locale.getpreferredencoding() instead of utf-8

Matthieu Weber mweber+weboob at free.fr
Wed May 13 20:16:06 CEST 2015


Signed-off-by: Matthieu Weber <mweber+weboob at free.fr>
---
 weboob/tools/application/formatters/iformatter.py |    5 +++--
 weboob/tools/application/formatters/table.py      |    3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/weboob/tools/application/formatters/iformatter.py b/weboob/tools/application/formatters/iformatter.py
index a4a7a1b..1e4f6dd 100644
--- a/weboob/tools/application/formatters/iformatter.py
+++ b/weboob/tools/application/formatters/iformatter.py
@@ -22,6 +22,7 @@ from __future__ import print_function
 
 import os
 import sys
+import locale
 import subprocess
 
 try:
@@ -121,7 +122,7 @@ class IFormatter(object):
     def output(self, formatted):
         if self.outfile != sys.stdout:
             with open(self.outfile, "a+") as outfile:
-                outfile.write(formatted.encode('utf-8') + os.linesep)
+                outfile.write(formatted.encode(locale.getpreferredencoding(), errors='replace') + os.linesep)
 
         else:
             for line in formatted.split('\n'):
@@ -133,7 +134,7 @@ class IFormatter(object):
                     self.print_lines = 0
 
                 if isinstance(line, unicode):
-                    line = line.encode('utf-8')
+                    line = line.encode(locale.getpreferredencoding(), errors='replace')
                 print(line)
                 self.print_lines += 1
 
diff --git a/weboob/tools/application/formatters/table.py b/weboob/tools/application/formatters/table.py
index 73c4a89..228d8ef 100644
--- a/weboob/tools/application/formatters/table.py
+++ b/weboob/tools/application/formatters/table.py
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with weboob. If not, see <http://www.gnu.org/licenses/>.
 
+import locale
 
 from prettytable import PrettyTable
 
@@ -40,7 +41,7 @@ class TableFormatter(IFormatter):
     def flush(self):
         s = self.get_formatted_table()
         if s is not None:
-            self.output(s.encode('utf-8'))
+            self.output(s.encode(locale.getpreferredencoding(), errors='replace'))
 
     def get_formatted_table(self):
         if len(self.queue) == 0:
-- 
1.7.10.4




More information about the weboob mailing list