diff genshi/filters/i18n.py @ 794:f9e23d472a6e trunk

Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
author cmlenz
date Tue, 16 Dec 2008 23:02:36 +0000
parents da90cee22560
children 878c4313c7d5
line wrap: on
line diff
--- a/genshi/filters/i18n.py
+++ b/genshi/filters/i18n.py
@@ -16,13 +16,13 @@
 :since: version 0.4
 """
 
-from compiler import ast
 from gettext import NullTranslations
 import re
 from types import FunctionType
 
 from genshi.core import Attrs, Namespace, QName, START, END, TEXT, START_NS, \
                         END_NS, XML_NAMESPACE, _ensure
+from genshi.template.eval import _ast
 from genshi.template.base import DirectiveFactory, EXPR, SUB, _apply_directives
 from genshi.template.directives import Directive
 from genshi.template.markup import MarkupTemplate, EXEC
@@ -526,25 +526,32 @@
     :since: version 0.5
     """
     def _walk(node):
-        if isinstance(node, ast.CallFunc) and isinstance(node.node, ast.Name) \
-                and node.node.name in gettext_functions:
+        if isinstance(node, _ast.Call) and isinstance(node.func, _ast.Name) \
+                and node.func.id in gettext_functions:
             strings = []
             def _add(arg):
-                if isinstance(arg, ast.Const) \
-                        and isinstance(arg.value, basestring):
-                    strings.append(unicode(arg.value, 'utf-8'))
-                elif arg and not isinstance(arg, ast.Keyword):
+                if isinstance(arg, _ast.Str) and isinstance(arg.s, basestring):
+                    strings.append(unicode(arg.s, 'utf-8'))
+                elif arg:
                     strings.append(None)
             [_add(arg) for arg in node.args]
-            _add(node.star_args)
-            _add(node.dstar_args)
+            _add(node.starargs)
+            _add(node.kwargs)
             if len(strings) == 1:
                 strings = strings[0]
             else:
                 strings = tuple(strings)
-            yield node.node.name, strings
-        else:
-            for child in node.getChildNodes():
+            yield node.func.id, strings
+        elif node._fields:
+            children = []
+            for field in node._fields:
+                child = getattr(node, field, None)
+                if isinstance(child, list):
+                    for elem in child:
+                        children.append(elem)
+                elif isinstance(child, _ast.AST):
+                    children.append(child)
+            for child in children:
                 for funcname, strings in _walk(child):
                     yield funcname, strings
     return _walk(code.ast)
Copyright (C) 2012-2017 Edgewall Software