comparison mercurial/templatefilters.py @ 41997:4df7c4b70e03

templatefilters: add {x|cbor} filter for custom CBOR output
author Yuya Nishihara <yuya@tcha.org>
date Sun, 10 Mar 2019 13:07:36 +0900
parents e2697acd9381
children 2372284d9457
comparison
equal deleted inserted replaced
41996:77ef3498ceb3 41997:4df7c4b70e03
21 templateutil, 21 templateutil,
22 url, 22 url,
23 util, 23 util,
24 ) 24 )
25 from .utils import ( 25 from .utils import (
26 cborutil,
26 dateutil, 27 dateutil,
27 stringutil, 28 stringutil,
28 ) 29 )
29 30
30 urlerr = util.urlerr 31 urlerr = util.urlerr
96 """Any text. Treats the text as a path, and returns the last 97 """Any text. Treats the text as a path, and returns the last
97 component of the path after splitting by the path separator. 98 component of the path after splitting by the path separator.
98 For example, "foo/bar/baz" becomes "baz" and "foo/bar//" becomes "". 99 For example, "foo/bar/baz" becomes "baz" and "foo/bar//" becomes "".
99 """ 100 """
100 return os.path.basename(path) 101 return os.path.basename(path)
102
103 @templatefilter('cbor')
104 def cbor(obj):
105 """Any object. Serializes the object to CBOR bytes."""
106 return b''.join(cborutil.streamencode(obj))
101 107
102 @templatefilter('commondir') 108 @templatefilter('commondir')
103 def commondir(filelist): 109 def commondir(filelist):
104 """List of text. Treats each list item as file name with / 110 """List of text. Treats each list item as file name with /
105 as path separator and returns the longest common directory 111 as path separator and returns the longest common directory