Patchwork [07,of,11] templatefilters: declare input type as date where appropriate

login
register
mail settings
Submitter Yuya Nishihara
Date March 31, 2018, 1:49 a.m.
Message ID <baf331e387375dd7ad56.1522460975@mimosa>
Download mbox | patch
Permalink /patch/30044/
State Accepted
Headers show

Comments

Yuya Nishihara - March 31, 2018, 1:49 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521357164 -32400
#      Sun Mar 18 16:12:44 2018 +0900
# Node ID baf331e387375dd7ad56b45b59fce950c385d17b
# Parent  044cf5cbae04306dc21919b1dbb461a1f01b3a2e
templatefilters: declare input type as date where appropriate

I'm not sure if the templateutil.date type can be a thing. Currently it's
just a constant.

Patch

diff --git a/mercurial/registrar.py b/mercurial/registrar.py
--- a/mercurial/registrar.py
+++ b/mercurial/registrar.py
@@ -333,7 +333,7 @@  class templatefilter(_templateregistrarb
     The first string argument is used also in online help.
 
     Optional argument 'intype' defines the type of the input argument,
-    which should be (bytes, int, or None for any.)
+    which should be (bytes, int, templateutil.date, or None for any.)
 
     'templatefilter' instance in example above can be used to
     decorate multiple functions.
diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -55,7 +55,7 @@  agescales = [("year", 3600 * 24 * 365, '
              ("minute", 60, 'm'),
              ("second", 1, 's')]
 
-@templatefilter('age')
+@templatefilter('age', intype=templateutil.date)
 def age(date, abbrev=False):
     """Date. Returns a human-readable date/time difference between the
     given date/time and the current date/time.
@@ -195,21 +195,21 @@  def hexfilter(text):
     """
     return node.hex(text)
 
-@templatefilter('hgdate')
+@templatefilter('hgdate', intype=templateutil.date)
 def hgdate(text):
     """Date. Returns the date as a pair of numbers: "1157407993
     25200" (Unix timestamp, timezone offset).
     """
     return "%d %d" % text
 
-@templatefilter('isodate')
+@templatefilter('isodate', intype=templateutil.date)
 def isodate(text):
     """Date. Returns the date in ISO 8601 format: "2009-08-18 13:00
     +0200".
     """
     return dateutil.datestr(text, '%Y-%m-%d %H:%M %1%2')
 
-@templatefilter('isodatesec')
+@templatefilter('isodatesec', intype=templateutil.date)
 def isodatesec(text):
     """Date. Returns the date in ISO 8601 format, including
     seconds: "2009-08-18 13:00:13 +0200". See also the rfc3339date
@@ -303,14 +303,14 @@  def revescape(text):
     """
     return urlreq.quote(text, safe='/@').replace('/', '%252F')
 
-@templatefilter('rfc3339date')
+@templatefilter('rfc3339date', intype=templateutil.date)
 def rfc3339date(text):
     """Date. Returns a date using the Internet date format
     specified in RFC 3339: "2009-08-18T13:00:13+02:00".
     """
     return dateutil.datestr(text, "%Y-%m-%dT%H:%M:%S%1:%2")
 
-@templatefilter('rfc822date')
+@templatefilter('rfc822date', intype=templateutil.date)
 def rfc822date(text):
     """Date. Returns a date using the same format used in email
     headers: "Tue, 18 Aug 2009 13:00:13 +0200".
@@ -335,7 +335,7 @@  def shortbisect(label):
         return label[0:1].upper()
     return ' '
 
-@templatefilter('shortdate')
+@templatefilter('shortdate', intype=templateutil.date)
 def shortdate(text):
     """Date. Returns a date like "2006-09-18"."""
     return dateutil.shortdate(text)
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -26,6 +26,11 @@  class ResourceUnavailable(error.Abort):
 class TemplateNotFound(error.Abort):
     pass
 
+# stub for representing a date type; may be a real date type that can
+# provide a readable string value
+class date(object):
+    pass
+
 class hybrid(object):
     """Wrapper for list or dict to support legacy template
 
@@ -361,6 +366,7 @@  def evalstringliteral(context, mapping, 
 _unwrapfuncbytype = {
     None: _unwrapvalue,
     bytes: stringify,
+    date: unwrapdate,
     int: unwrapinteger,
 }
 
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -2806,7 +2806,8 @@  Behind the scenes, this would throw Type
 Behind the scenes, this will throw a ValueError
 
   $ hg log -l 3 --template 'line: {desc|shortdate}\n'
-  abort: template filter 'shortdate' is not compatible with keyword 'desc'
+  hg: parse error: invalid date: 'Modify, add, remove, rename'
+  (template filter 'shortdate' is not compatible with keyword 'desc')
   [255]
 
 Behind the scenes, this would throw AttributeError without intype=bytes
@@ -2827,11 +2828,13 @@  Behind the scenes, this will throw Value
   [255]
 
   $ hg tip -T '{author|email|shortdate}\n'
-  abort: template filter 'shortdate' is not compatible with keyword 'author'
+  hg: parse error: invalid date: 'test'
+  (template filter 'shortdate' is not compatible with keyword 'author')
   [255]
 
   $ hg tip -T '{get(extras, "branch")|shortdate}\n'
-  abort: incompatible use of template filter 'shortdate'
+  hg: parse error: invalid date: 'default'
+  (incompatible use of template filter 'shortdate')
   [255]
 
 Error in nested template: