[gnome-keysign: 6/18] babelglade: add Python 3 support



commit 200d2afd64a0a1d4a8f0c8a2bffa88e668ccd2e9
Author: RyuzakiKK <aasonykk gmail com>
Date:   Thu Oct 5 14:55:37 2017 +0200

    babelglade: add Python 3 support
    
    Closes #20
    
    The check for unicode is no more needed because 'data' will always be
    str type with Python 2 and bytes type with Python 3

 babelglade/__init__.py | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
---
diff --git a/babelglade/__init__.py b/babelglade/__init__.py
index ec0e008..c527811 100644
--- a/babelglade/__init__.py
+++ b/babelglade/__init__.py
@@ -15,13 +15,13 @@
 
 from xml.parsers import expat
 
+
 class GladeParser(object):
     def __init__(self, source):
         self.source = source
 
-        parser = expat.ParserCreate()
+        parser = expat.ParserCreate("utf-8")
         parser.buffer_text = True
-        parser.returns_unicode = True
         parser.ordered_attributes = True
         parser.StartElementHandler = self._handle_start
         parser.EndElementHandler = self._handle_end
@@ -42,14 +42,12 @@ class GladeParser(object):
             done = False
             while not done and len(self._queue) == 0:
                 data = self.source.read(bufsize)
-                if data == '': # end of data
+                if data == b'':  # end of data
                     if hasattr(self, 'expat'):
                         self.expat.Parse('', True)
                         del self.expat # get rid of circular references
                     done = True
                 else:
-                    if isinstance(data, unicode):
-                        data = data.encode('utf-8')
                     self.expat.Parse(data, False)
                 for event in self._queue:
                     yield event
@@ -61,11 +59,11 @@ class GladeParser(object):
             raise ParseError(msg, self.filename, e.lineno, e.offset)
 
     def _handle_start(self, tag, attrib):
-        if u'translatable' in attrib:
-            if attrib[attrib.index(u'translatable')+1] == u'yes':
+        if 'translatable' in attrib:
+            if attrib[attrib.index('translatable')+1] == 'yes':
                 self._translate = True
-                if u'comments' in attrib:
-                    self._comments.append(attrib[attrib.index(u'comments')+1])
+                if 'comments' in attrib:
+                    self._comments.append(attrib[attrib.index('comments')+1])
 
     def _handle_end(self, tag):
         if self._translate is True:
@@ -77,7 +75,7 @@ class GladeParser(object):
 
     def _handle_data(self, text):
         if self._translate:
-            if not text.startswith(u'gtk-'):
+            if not text.startswith('gtk-'):
                 self._data.append(text)
             else:
                 self._translate = False
@@ -87,7 +85,7 @@ class GladeParser(object):
     def _enqueue(self, kind, data=None, comments=None, pos=None):
         if pos is None:
             pos = self._getpos()
-        if kind in (u'property', 'property'):
+        if kind == 'property':
             if '\n' in data:
                 lines = data.splitlines()
                 lineno = pos[0] - len(lines) + 1


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