[Python-modules-commits] [python-kajiki] 01/08: New upstream version 0.7.1

Takaki Taniguchi takaki at moszumanska.debian.org
Sat Feb 3 13:04:36 UTC 2018


This is an automated email from the git hooks/post-receive script.

takaki pushed a commit to branch master
in repository python-kajiki.

commit 4efda31097c40eb01ebd3da570842d9ff200d7af
Author: TANIGUCHI Takaki <takaki at asis.media-as.org>
Date:   Sat Feb 3 21:56:50 2018 +0900

    New upstream version 0.7.1
---
 CHANGES.rst                        |  5 +++++
 Kajiki.egg-info/PKG-INFO           |  7 ++++++-
 Kajiki.egg-info/SOURCES.txt        |  2 ++
 Kajiki.egg-info/requires.txt       |  2 +-
 PKG-INFO                           |  7 ++++++-
 kajiki/template.py                 | 31 +++++++++++++++++++++-------
 kajiki/tests/data/file_child.html  |  1 +
 kajiki/tests/data/file_parent.html |  1 +
 kajiki/tests/test_xml.py           | 41 ++++++++++++++++++++++++++++++++++++++
 kajiki/version.py                  |  2 +-
 kajiki/xml_template.py             |  5 +++--
 setup.cfg                          |  1 -
 12 files changed, 91 insertions(+), 14 deletions(-)

diff --git a/CHANGES.rst b/CHANGES.rst
index 14f42ca..47a6169 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,6 +1,11 @@
 CHANGES
 =======
 
+0.7.1 (2017-09-11)
+------------------
+
+* Allow to replace ``gettext`` function by providing it in the template context or through ``base_globals`` in Loader
+
 0.7.0 (2017-06-27)
 ------------------
 
diff --git a/Kajiki.egg-info/PKG-INFO b/Kajiki.egg-info/PKG-INFO
index 255e661..3a7cddc 100644
--- a/Kajiki.egg-info/PKG-INFO
+++ b/Kajiki.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: Kajiki
-Version: 0.7.0
+Version: 0.7.1
 Summary: Fast XML-based template engine with Genshi syntax and Jinja blocks
 Home-page: https://github.com/nandoflorestan/kajiki
 Author: Nando Florestan
@@ -78,6 +78,11 @@ Description: Kajiki provides fast well-formed XML templates
         CHANGES
         =======
         
+        0.7.1 (2017-09-11)
+        ------------------
+        
+        * Allow to replace ``gettext`` function by providing it in the template context or through ``base_globals`` in Loader
+        
         0.7.0 (2017-06-27)
         ------------------
         
diff --git a/Kajiki.egg-info/SOURCES.txt b/Kajiki.egg-info/SOURCES.txt
index d484f54..0530e8f 100644
--- a/Kajiki.egg-info/SOURCES.txt
+++ b/Kajiki.egg-info/SOURCES.txt
@@ -60,4 +60,6 @@ kajiki/tests/data/__init__.py
 kajiki/tests/data/debug.html
 kajiki/tests/data/debug.txt
 kajiki/tests/data/error.html
+kajiki/tests/data/file_child.html
+kajiki/tests/data/file_parent.html
 kajiki/tests/data/simple.html
\ No newline at end of file
diff --git a/Kajiki.egg-info/requires.txt b/Kajiki.egg-info/requires.txt
index c1cbf47..879da98 100644
--- a/Kajiki.egg-info/requires.txt
+++ b/Kajiki.egg-info/requires.txt
@@ -2,4 +2,4 @@ nine
 
 [testing]
 babel
-nose
\ No newline at end of file
+nose
diff --git a/PKG-INFO b/PKG-INFO
index 255e661..3a7cddc 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: Kajiki
-Version: 0.7.0
+Version: 0.7.1
 Summary: Fast XML-based template engine with Genshi syntax and Jinja blocks
 Home-page: https://github.com/nandoflorestan/kajiki
 Author: Nando Florestan
@@ -78,6 +78,11 @@ Description: Kajiki provides fast well-formed XML templates
         CHANGES
         =======
         
+        0.7.1 (2017-09-11)
+        ------------------
+        
+        * Allow to replace ``gettext`` function by providing it in the template context or through ``base_globals`` in Loader
+        
         0.7.0 (2017-06-27)
         ------------------
         
diff --git a/kajiki/template.py b/kajiki/template.py
index e6b9c2a..5204eb4 100644
--- a/kajiki/template.py
+++ b/kajiki/template.py
@@ -64,11 +64,11 @@ class _Template(object):
             context = {}
         self._context = context
         base_globals = self.base_globals or {}
-        self.__globals__ = dict(base_globals, local=self, self=self,
+        self.__globals__ = dict(local=self, self=self,
             defined=lambda x: x in self.__globals__,
-            literal=literal, Markup=literal, _=i18n.gettext,
+            literal=literal, Markup=literal, gettext=i18n.gettext,
             __builtins__=__builtins__, __kj__=kajiki)
-        self.__globals__['value_of'] = self.__globals__.get
+        self.__globals__.update(base_globals)
         for k, v in self.__methods__:
             v = v.bind_instance(self)
             setattr(self, k, v)
@@ -80,7 +80,7 @@ class _Template(object):
             case=self._case,
             import_=self._import,
             escape=self._escape,
-            gettext=i18n.gettext,
+            gettext=self._gettext,
             render_attrs=self._render_attrs,
             push_with=self._push_with,
             pop_with=self._pop_with,
@@ -89,6 +89,8 @@ class _Template(object):
         self._switch_stack = []
         self._with_stack = []
         self.__globals__.update(context)
+        self.__globals__['_'] = self.__globals__['gettext']
+        self.__globals__['value_of'] = self.__globals__.get
 
     def __iter__(self):
         """We convert the chunk to string because it can be of any type
@@ -102,6 +104,10 @@ class _Template(object):
         """Render the template to a string."""
         return ''.join(self)
 
+    def _gettext(self, s):
+        """Used by the code generated by the template to translate static text"""
+        return self.__globals__['gettext'](s)
+
     def _push_with(self, locals_, vars):
         """Enter a ``py:with`` block.
 
@@ -122,6 +128,11 @@ class _Template(object):
         return self._with_stack.pop()
 
     def _extend(self, parent):
+        """
+        Called when a child template extends a parent template
+        the first thing it does is asking the loader of the
+        child template to load the parent template
+        """
         if isinstance(parent, basestring):
             parent = self.loader.import_(parent)
         p_inst = parent(self._context)
@@ -279,13 +290,18 @@ def Template(ns):
     return type(ns.__name__, (_Template,), dct)
 
 
-def from_ir(ir_node):
+def from_ir(ir_node, base_globals=None):
     """Creates a template class from Intermediate Representation TemplateNode.
 
     This actually creates the class defined by the TemplateNode by executing
     its code and returns a subclass of it.
     The returned class is a subclass of :class:`kajiki.template._Template`.
+
+    It is possible to use `base_globals` to set context values
+    or replace default ones
     """
+    if base_globals is None:
+        base_globals = dict()
     py_lines = list(generate_python(ir_node))
     py_text = '\n'.join(map(str, py_lines))
     py_linenos = []
@@ -300,7 +316,8 @@ def from_ir(ir_node):
     except (SyntaxError, IndentationError) as e:  # pragma no cover
         raise KajikiSyntaxError(e.msg, py_text, e.filename, e.lineno, e.offset)
     tpl = dct['template']
-    tpl.base_globals = dct
+    tpl.base_globals = base_globals.copy()
+    tpl.base_globals.update(dct)
     tpl.py_text = py_text
     tpl.filename = ir_node.filename
     tpl.annotate_lnotab(py_linenos)
@@ -438,4 +455,4 @@ class KajikiSyntaxError(Exception):
         try:
             return source.splitlines()[linen]
         except:
-            return ''
\ No newline at end of file
+            return ''
diff --git a/kajiki/tests/data/file_child.html b/kajiki/tests/data/file_child.html
new file mode 100644
index 0000000..43cf8e2
--- /dev/null
+++ b/kajiki/tests/data/file_child.html
@@ -0,0 +1 @@
+<py:extends href="file_parent.html"><div>child</div></py:extends>
diff --git a/kajiki/tests/data/file_parent.html b/kajiki/tests/data/file_parent.html
new file mode 100644
index 0000000..97d1cf3
--- /dev/null
+++ b/kajiki/tests/data/file_parent.html
@@ -0,0 +1 @@
+<div>parent</div>
diff --git a/kajiki/tests/test_xml.py b/kajiki/tests/test_xml.py
index 81353dc..a5240c6 100644
--- a/kajiki/tests/test_xml.py
+++ b/kajiki/tests/test_xml.py
@@ -882,6 +882,47 @@ class TestTranslation(TestCase):
         else:
             assert False, 'Should have raised'
 
+    def test_substituting_gettext_with_lambda(self):
+        src = '''<xml>hi</xml>'''
+        expected = '''<xml>spam</xml>'''
+
+        perform(src, expected, context=dict(gettext=lambda x: 'spam'))
+
+    def test_substituting_gettext_with_lambda_extending(self):
+        gettext = lambda x: 'egg'
+        loader = MockLoader({
+            'parent.html': XMLTemplate('''<div>parent</div>'''),
+            'child.html': XMLTemplate('''<py:extends href="parent.html"><div>child</div></py:extends>''')})
+        tpl = loader.import_('child.html')
+        rsp = tpl(dict(gettext=gettext)).render()
+        assert rsp == '''<div>egg</div><div>egg</div>''', rsp
+
+    def test_substituting_gettext_with_lambda_extending_twice(self):
+        gettext = lambda x: 'egg'
+        loader = MockLoader({
+            'parent.html': XMLTemplate('''<div>parent</div>'''),
+            'mid.html': XMLTemplate('''<py:extends href="parent.html"><div>${variable}</div></py:extends>'''),
+            'child.html': XMLTemplate('''<py:extends href="mid.html"><div>child</div></py:extends>''')})
+        tpl = loader.import_('child.html')
+        rsp = tpl(dict(variable='spam', gettext=gettext)).render()
+        # variables must not be translated
+        assert rsp == '''<div>egg</div><div>spam</div><div>egg</div>''', rsp
+
+    def test_substituting_gettext_with_lambda_extending_file(self):
+        loader = FileLoader(path=os.path.join(os.path.dirname(__file__),
+                            'data'), base_globals=dict(gettext=lambda x: 'egg'))
+        tpl = loader.import_('file_child.html')
+        rsp = tpl(dict()).render()
+        assert rsp == '''<div>egg</div><div>egg</div>''', rsp
+
+    def test_without_substituting_gettext_with_lambda_extending_file(self):
+        # this should use i18n.gettext
+        loader = FileLoader(path=os.path.join(os.path.dirname(__file__),
+                            'data'))
+        tpl = loader.import_('file_child.html')
+        rsp = tpl(dict()).render()
+        assert rsp == '''<div>parent</div><div>child</div>''', rsp
+
 
 class TestDOMTransformations(TestCase):
     def test_empty_text_extraction(self):
diff --git a/kajiki/version.py b/kajiki/version.py
index 58b5751..ae4e622 100644
--- a/kajiki/version.py
+++ b/kajiki/version.py
@@ -3,4 +3,4 @@ from __future__ import (absolute_import, division, print_function,
                         unicode_literals)
 
 __version__ = '0.6'
-__release__ = '0.7.0'
+__release__ = '0.7.1'
diff --git a/kajiki/xml_template.py b/kajiki/xml_template.py
index 9632015..1092dea 100644
--- a/kajiki/xml_template.py
+++ b/kajiki/xml_template.py
@@ -29,7 +29,7 @@ impl = dom.getDOMImplementation(' ')
 
 def XMLTemplate(source=None, filename=None, mode=None, is_fragment=False,
                 encoding='utf-8', autoblocks=None, cdata_scripts=True,
-                strip_text=False):
+                strip_text=False, base_globals=None):
     """Given XML source code of a Kajiki Templates parses and returns a template class.
 
     The source code is parsed to its DOM representation by :class:`._Parser`,
@@ -53,7 +53,8 @@ def XMLTemplate(source=None, filename=None, mode=None, is_fragment=False,
     doc = _DomTransformer(doc, strip_text=strip_text).transform()
     ir_ = _Compiler(filename, doc, mode=mode, is_fragment=is_fragment,
                     autoblocks=autoblocks, cdata_scripts=cdata_scripts).compile()
-    return template.from_ir(ir_)
+    t = template.from_ir(ir_, base_globals=base_globals)
+    return t
 
 
 def annotate(gen):
diff --git a/setup.cfg b/setup.cfg
index 861a9f5..8bfd5a1 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,5 +1,4 @@
 [egg_info]
 tag_build = 
 tag_date = 0
-tag_svn_revision = 0
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-kajiki.git



More information about the Python-modules-commits mailing list