Patchwork [1,of,5] templater: extract helper that evaluates filter or function argument

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 1, 2015, 1:25 p.m.
Message ID <ca87b46db7986b048404.1441113911@mimosa>
Download mbox | patch
Permalink /patch/10341/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 1, 2015, 1:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1441101470 -32400
#      Tue Sep 01 18:57:50 2015 +0900
# Node ID ca87b46db7986b048404fbff2ea25067d2db2233
# Parent  885f2d57c73956f20fd62c5ff77dea936a62fb34
templater: extract helper that evaluates filter or function argument

It will be used to get a date tuple from an argument. Perhaps some of
"stringify(args[n][0], ...)" can be replaced by this function.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -215,6 +215,15 @@  def gettemplate(exp, context):
         return context._load(exp[1])
     raise error.ParseError(_("expected template specifier"))
 
+def evalfuncarg(context, mapping, arg):
+    func, data = arg
+    # func() may return string, generator of strings or arbitrary object such
+    # as date tuple, but filter does not want generator.
+    thing = func(context, mapping, data)
+    if isinstance(thing, types.GeneratorType):
+        thing = stringify(thing)
+    return thing
+
 def runinteger(context, mapping, data):
     return int(data)
 
@@ -259,11 +268,7 @@  def buildfilter(exp, context):
 
 def runfilter(context, mapping, data):
     func, data, filt = data
-    # func() may return string, generator of strings or arbitrary object such
-    # as date tuple, but filter does not want generator.
-    thing = func(context, mapping, data)
-    if isinstance(thing, types.GeneratorType):
-        thing = stringify(thing)
+    thing = evalfuncarg(context, mapping, (func, data))
     try:
         return filt(thing)
     except (ValueError, AttributeError, TypeError):