[gnome-keysign: 60/75] Added a glib_markup_escape_text_to_text helper function



commit 10065923d81c0063b97238391f8909d5e7b02441
Author: Tobias Mueller <muelli cryptobitch de>
Date:   Wed Sep 20 15:31:59 2017 +0200

    Added a glib_markup_escape_text_to_text helper function
    
    GLib.markup_escape_text returns a str which is not satisfactory all the
    time.  In Python2 we'd like to get a proper text type, i.e. unicode.

 keysign/keylistwidget.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
---
diff --git a/keysign/keylistwidget.py b/keysign/keylistwidget.py
index 0047f3d..1448c49 100755
--- a/keysign/keylistwidget.py
+++ b/keysign/keylistwidget.py
@@ -37,6 +37,22 @@ class ListBoxRowWithKey(Gtk.ListBoxRow):
         label = Gtk.Label(s, use_markup=True, xalign=0)
         self.add(label)
 
+    @staticmethod
+    def glib_markup_escape_text_to_text(s):
+        """A helper function to return the text type
+        markup_escape_text returns a "str" which is
+        a binary type in python2.
+        This function tries to decode the returned
+        str object.  It will fail in Python3.
+        """
+        m = GLib.markup_escape_text(s)
+        try:
+            ret = m.decode('utf-8')
+        except AttributeError:
+            # We are in Python3 land. All is fine.
+            ret = m
+        return ret
+        
 
     @classmethod
     def format_uid(cls, uid):
@@ -48,7 +64,7 @@ class ListBoxRowWithKey(Gtk.ListBoxRow):
                           for k in items}
         log.info("format dicT: %r", format_dict)
         d = {k: (log.debug("handling kv: %r %r", k, v),
-                  GLib.markup_escape_text(
+                  cls.glib_markup_escape_text_to_text(
                     "{}".format(v)))[1]
              for k, v in format_dict.items()}
         log.info("Formatting UID %r", d)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]