changeset 2161:62dfa6e02f54

jp (base, blog, output(std)): fixed bad unicode handling in stdout, use of jp.disp instead of simple print
author Goffi <goffi@goffi.org>
date Tue, 21 Feb 2017 21:01:40 +0100
parents e67e8cd24141
children c9a67eb5bf72
files frontends/src/jp/base.py frontends/src/jp/cmd_blog.py frontends/src/jp/output_std.py
diffstat 3 files changed, 27 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/base.py	Tue Feb 21 21:01:40 2017 +0100
+++ b/frontends/src/jp/base.py	Tue Feb 21 21:01:40 2017 +0100
@@ -251,9 +251,9 @@
         """
         if self.verbosity >= verbosity:
             if error:
-                print >>sys.stderr,msg
+                print >>sys.stderr,msg.encode('utf-8')
             else:
-                print msg
+                print msg.encode('utf-8')
 
     def output(self, type_, name, extra_outputs, data):
         if name in extra_outputs:
--- a/frontends/src/jp/cmd_blog.py	Tue Feb 21 21:01:40 2017 +0100
+++ b/frontends/src/jp/cmd_blog.py	Tue Feb 21 21:01:40 2017 +0100
@@ -277,7 +277,7 @@
                     k_fmt_e = A.RESET,
                     sep = u'\n' if 'content' in k else u'')
                 value = k_cb[k](item, keys) if k in k_cb else item[k]
-                print header + value
+                self.disp(header + value)
             # we want a separation line after each item but the last one
             if idx < len(items)-1:
                 print(u'')
@@ -332,7 +332,7 @@
             if (title or tags) and content:
                 print("")
             if content:
-                print content
+                self.disp(content)
 
             print(u'\n' + sep  + '\n')
 
@@ -812,16 +812,16 @@
                 if getattr(self.args, name):
                     self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name))
             if self.args.importer is None:
-                print u'\n'.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.blogImportList()])
+                self.disp(u'\n'.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.blogImportList()]))
             else:
                 try:
                     short_desc, long_desc = self.host.bridge.blogImportDesc(self.args.importer)
                 except Exception as e:
                     msg = [l for l in unicode(e).split('\n') if l][-1] # we only keep the last line
-                    print msg
+                    self.disp(msg)
                     self.host.quit(1)
                 else:
-                    print u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc)
+                    self.disp(u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc))
             self.host.quit()
         else:
             # we have a location, an import is requested
--- a/frontends/src/jp/output_std.py	Tue Feb 21 21:01:40 2017 +0100
+++ b/frontends/src/jp/output_std.py	Tue Feb 21 21:01:40 2017 +0100
@@ -31,37 +31,39 @@
 class Simple(object):
     """Default outputs"""
 
-    def __init__(self, jp):
-        jp.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print)
-        jp.register_output(C.OUTPUT_LIST, SIMPLE, self.list)
-        jp.register_output(C.OUTPUT_DICT, SIMPLE, self.dict)
-        jp.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print)
+    def __init__(self, host):
+        self.host = host
+        host.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print)
+        host.register_output(C.OUTPUT_LIST, SIMPLE, self.list)
+        host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict)
+        host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print)
 
     def simple_print(self, data):
-        print data
+        self.host.disp(data)
 
     def list(self, data):
-        print u'\n'.join(data)
+        self.host.disp(u'\n'.join(data))
 
     def dict(self, data):
         for k, v in data:
-            print(u'{key}: {value}'.format(key=k, value=v))
+            self.host.disp((u'{key}: {value}'.format(key=k, value=v)))
 
 
 class Json(object):
     """outputs in json format"""
 
-    def __init__(self, jp):
-        jp.register_output(C.OUTPUT_TEXT, JSON, self.dump)
-        jp.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty)
-        jp.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump)
-        jp.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty)
-        jp.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump)
-        jp.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty)
-        jp.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)
+    def __init__(self, host):
+        self.host = host
+        host.register_output(C.OUTPUT_TEXT, JSON, self.dump)
+        host.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)
 
     def dump(self, data):
-        print json.dumps(data, default=str)
+        self.host.disp(json.dumps(data, default=str))
 
     def dump_pretty(self, data):
-        print json.dumps(data, indent=4, default=str)
+        self.host.disp(json.dumps(data, indent=4, default=str))