changeset 37229:9bcf096a2da2

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.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 18 Mar 2018 16:12:44 +0900
parents 920589f52be9
children a0b17f744cbc
files mercurial/registrar.py mercurial/templatefilters.py mercurial/templateutil.py tests/test-command-template.t
diffstat 4 files changed, 20 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/registrar.py	Tue Mar 20 22:57:36 2018 +0900
+++ b/mercurial/registrar.py	Sun Mar 18 16:12:44 2018 +0900
@@ -333,7 +333,7 @@
     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.
--- a/mercurial/templatefilters.py	Tue Mar 20 22:57:36 2018 +0900
+++ b/mercurial/templatefilters.py	Sun Mar 18 16:12:44 2018 +0900
@@ -55,7 +55,7 @@
              ("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 @@
     """
     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 @@
     """
     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 @@
         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)
--- a/mercurial/templateutil.py	Tue Mar 20 22:57:36 2018 +0900
+++ b/mercurial/templateutil.py	Sun Mar 18 16:12:44 2018 +0900
@@ -26,6 +26,11 @@
 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 @@
 _unwrapfuncbytype = {
     None: _unwrapvalue,
     bytes: stringify,
+    date: unwrapdate,
     int: unwrapinteger,
 }
 
--- a/tests/test-command-template.t	Tue Mar 20 22:57:36 2018 +0900
+++ b/tests/test-command-template.t	Sun Mar 18 16:12:44 2018 +0900
@@ -2806,7 +2806,8 @@
 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 @@
   [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: