[PATCH 2 of 2] templatefilters: add {x|cbor} filter for custom CBOR output

Yuya Nishihara yuya at tcha.org
Sun Mar 10 00:56:49 EST 2019


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1552190856 -32400
#      Sun Mar 10 13:07:36 2019 +0900
# Node ID 12cad6eb90a13ab7e82e0b0a925f5c5e2e7a6691
# Parent  343027851edc0337e4058feb6f51d67dc584bc0b
templatefilters: add {x|cbor} filter for custom CBOR output

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -23,6 +23,7 @@ from . import (
     util,
 )
 from .utils import (
+    cborutil,
     dateutil,
     stringutil,
 )
@@ -99,6 +100,11 @@ def basename(path):
     """
     return os.path.basename(path)
 
+ at templatefilter('cbor')
+def cbor(obj):
+    """Any object. Serializes the object to CBOR bytes."""
+    return b''.join(cborutil.streamencode(obj))
+
 @templatefilter('commondir')
 def commondir(filelist):
     """List of text. Treats each list item as file name with /
diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -1495,6 +1495,32 @@ Test with non-strings like dates
      1200000.00
      1300000.00
 
+Test cbor filter:
+
+  $ cat <<'EOF' > "$TESTTMP/decodecbor.py"
+  > from __future__ import absolute_import
+  > from mercurial import pycompat
+  > from mercurial.utils import (
+  >     cborutil,
+  >     stringutil,
+  > )
+  > items = cborutil.decodeall(pycompat.stdin.read())
+  > pycompat.stdout.write(stringutil.pprint(items, indent=1) + b'\n')
+  > EOF
+
+  $ hg log -T "{rev|cbor}" -R a -l2 | "$PYTHON" "$TESTTMP/decodecbor.py"
+  [
+   10,
+   9
+  ]
+
+  $ hg log -T "{extras|cbor}" -R a -l1 | "$PYTHON" "$TESTTMP/decodecbor.py"
+  [
+   {
+    'branch': 'default'
+   }
+  ]
+
 json filter should escape HTML tags so that the output can be embedded in hgweb:
 
   $ hg log -T "{'<foo at example.org>'|json}\n" -R a -l1


More information about the Mercurial-devel mailing list