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

login
register
mail settings
Submitter Yuya Nishihara
Date June 26, 2017, 1:48 a.m.
Message ID <8407931dc104dd45e7c7.1498441683@mimosa>
Download mbox | patch
Permalink /patch/21717/
State Accepted
Headers show

Comments

Yuya Nishihara - June 26, 2017, 1:48 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.

Patch

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)