[PATCH 2 of 3] formatter: proxy fm.context() through converter

Yuya Nishihara yuya at tcha.org
Sun Jun 25 21:48:03 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1498437181 -32400
#      Mon Jun 26 09:33:01 2017 +0900
# Node ID 8407931dc104dd45e7c7d6661710e108c59ce001
# Parent  556d2afe74385fe2a6dd7cdff5651080a827813f
formatter: proxy fm.context() through converter

Otherwise nested template formatter would not see the context objects.

It's just a boolean flag now. We might want to change it to 'ctxs -> items'
function so changectx attributes are populated automatically in JSON, but
I'm not sure.

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -127,6 +127,10 @@ pickle = util.pickle
 
 class _nullconverter(object):
     '''convert non-primitive data types to be processed by formatter'''
+
+    # set to True if context object should be stored as item
+    storecontext = False
+
     @staticmethod
     def formatdate(date, fmt):
         '''convert date tuple to appropriate format'''
@@ -178,7 +182,10 @@ class baseformatter(object):
         return self._converter.formatlist(data, name, fmt, sep)
     def context(self, **ctxs):
         '''insert context objects to be used to render template keywords'''
-        pass
+        ctxs = pycompat.byteskwargs(ctxs)
+        assert all(k == 'ctx' for k in ctxs)
+        if self._converter.storecontext:
+            self._item.update(ctxs)
     def data(self, **data):
         '''insert data into item that's not shown in default output'''
         data = pycompat.byteskwargs(data)
@@ -228,6 +235,9 @@ def _iteritems(data):
 
 class _plainconverter(object):
     '''convert non-primitive data types to text'''
+
+    storecontext = False
+
     @staticmethod
     def formatdate(date, fmt):
         '''stringify date tuple in the given format'''
@@ -323,6 +333,9 @@ class jsonformatter(baseformatter):
 
 class _templateconverter(object):
     '''convert non-primitive data types to be processed by templater'''
+
+    storecontext = True
+
     @staticmethod
     def formatdate(date, fmt):
         '''return date tuple'''
@@ -356,12 +369,6 @@ class templateformatter(baseformatter):
         self._cache = {}  # for templatekw/funcs to store reusable data
         self._renderitem('docheader', {})
 
-    def context(self, **ctxs):
-        '''insert context objects to be used to render template keywords'''
-        ctxs = pycompat.byteskwargs(ctxs)
-        assert all(k == 'ctx' for k in ctxs)
-        self._item.update(ctxs)
-
     def _showitem(self):
         item = self._item.copy()
         item['index'] = index = next(self._counter)


More information about the Mercurial-devel mailing list