[gnome-keysign: 11/18] rewrote babelglade using lxml instead of expat



commit 4e0a71dbab5af139ed9548864841812dff0eaa14
Author: RyuzakiKK <aasonykk gmail com>
Date:   Thu Oct 26 14:49:51 2017 +0200

    rewrote babelglade using lxml instead of expat

 babelglade/__init__.py | 126 ++++++-------------------------------------------
 1 file changed, 15 insertions(+), 111 deletions(-)
---
diff --git a/babelglade/__init__.py b/babelglade/__init__.py
index 5e3f86f..0d1d6dc 100644
--- a/babelglade/__init__.py
+++ b/babelglade/__init__.py
@@ -1,119 +1,23 @@
-# -*- coding: utf-8 -*-
-# vim: sw=4 ts=4 fenc=utf-8
-# =============================================================================
-# $Id: __init__.py 235 2007-07-20 15:23:26Z palgarvio $
-# =============================================================================
-#             $URL: http://svn.edgewall.org/repos/babel/contrib/GladeBabelExtractor/babelglade/__init__.py $
-# $LastChangedDate: 2007-07-20 16:23:26 +0100 (Fri, 20 Jul 2007) $
-#             $Rev: 235 $
-#   $LastChangedBy: palgarvio $
-# =============================================================================
-# Copyright (C) 2006 Ufsoft.org - Pedro Algarvio <ufs ufsoft org>
-#
-# Please view LICENSE for additional licensing information.
-# =============================================================================
 from __future__ import unicode_literals
 
-from xml.parsers import expat
+from lxml import etree
 
 
-class GladeParser(object):
-    def __init__(self, source):
-        self.source = source
-
-        parser = expat.ParserCreate("utf-8")
-        parser.buffer_text = True
-        parser.ordered_attributes = True
-        parser.StartElementHandler = self._handle_start
-        parser.EndElementHandler = self._handle_end
-        parser.CharacterDataHandler = self._handle_data
-
-        if not hasattr(parser, 'CurrentLineNumber'):
-            self._getpos = self._getpos_unknown
-
-        self.expat = parser
-        self._queue = []
-        self._comments = []
-        self._translate = False
-        self._data = []
-
-    def parse(self):
-        try:
-            bufsize = 4 * 1024 # 4K
-            done = False
-            while not done and len(self._queue) == 0:
-                data = self.source.read(bufsize)
-                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:
-                    self.expat.Parse(data, False)
-                for event in self._queue:
-                    yield event
-                self._queue = []
-                if done:
-                    break
-        except expat.ExpatError as e:
-            msg = str(e)
-            raise ParseError(msg, self.filename, e.lineno, e.offset)
-
-    def _handle_start(self, tag, attrib):
-        if 'translatable' in attrib:
-            if attrib[attrib.index('translatable')+1] == 'yes':
-                self._translate = True
-                if 'comments' in attrib:
-                    self._comments.append(attrib[attrib.index('comments')+1])
-
-    def _handle_end(self, tag):
-        if self._translate is True:
-            if self._data:
-                self._enqueue(tag, self._data, self._comments)
-            self._translate = False
-            self._data = []
-            self._comments = []
-
-    def _handle_data(self, text):
-        if self._translate:
-            if not text.startswith('gtk-'):
-                self._data.append(text)
-            else:
-                self._translate = False
-                self._data = []
-                self._comments = []
-
-    def _enqueue(self, kind, data=None, comments=None, pos=None):
-        if pos is None:
-            pos = self._getpos()
-        if kind == 'property':
-            if '\n' in data:
-                lines = data.splitlines()
-                lineno = pos[0] - len(lines) + 1
-                offset = -1
-            else:
-                lineno = pos[0]
-                offset = pos[1] - len(data)
-            pos = (lineno, offset)
-            self._queue.append((data, comments, pos[0]))
-
-    def _getpos(self):
-        return (self.expat.CurrentLineNumber,
-                self.expat.CurrentColumnNumber)
-    def _getpos_unknown(self):
-        return (-1, -1)
-
 def extract_glade(fileobj, keywords, comment_tags, options):
-    parser = GladeParser(fileobj)
-    def get_messages():
-        for message, comments, lineno in parser.parse():
-            if comment_tags:
-                yield (lineno, None, message, comments)
-            else:
-                yield (lineno, None, message, [])
-    return get_messages()
-
-
+    tree = etree.parse(fileobj)
+    root = tree.getroot()
+    to_translate = []
+    for elem in root.iter():
+        # do we need to check if the element starts with "gtk-"?
+        if elem.get("translatable") == "yes":
+            line_no = elem.sourceline
+            func_name = None
+            message = elem.text
+            comment = []
+            if elem.get("comments"):
+                comment = [elem.get("comments")]
+            to_translate.append([line_no, func_name, message, comment])
+    return to_translate
 
 
 # All localestrings from https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s05.html


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