##// END OF EJS Templates
templater: add wrapped types for pure non-list/dict values...
Yuya Nishihara -
r38228:7824783a default
parent child Browse files
Show More
@@ -32,6 +32,9 b' None'
32 True, False, int, float
32 True, False, int, float
33 can be stringified as such.
33 can be stringified as such.
34
34
35 wrappedbytes, wrappedvalue
36 a wrapper for the above printable types.
37
35 date tuple
38 date tuple
36 a (unixtime, offset) tuple, which produces no meaningful output by itself.
39 a (unixtime, offset) tuple, which produces no meaningful output by itself.
37
40
@@ -66,6 +66,44 b' class wrapped(object):'
66 A returned value must be serializable by templaterfilters.json().
66 A returned value must be serializable by templaterfilters.json().
67 """
67 """
68
68
69 class wrappedbytes(wrapped):
70 """Wrapper for byte string"""
71
72 def __init__(self, value):
73 self._value = value
74
75 def itermaps(self, context):
76 raise error.ParseError(_('%r is not iterable of mappings')
77 % pycompat.bytestr(self._value))
78
79 def join(self, context, mapping, sep):
80 return joinitems(pycompat.iterbytestr(self._value), sep)
81
82 def show(self, context, mapping):
83 return self._value
84
85 def tovalue(self, context, mapping):
86 return self._value
87
88 class wrappedvalue(wrapped):
89 """Generic wrapper for pure non-list/dict/bytes value"""
90
91 def __init__(self, value):
92 self._value = value
93
94 def itermaps(self, context):
95 raise error.ParseError(_('%r is not iterable of mappings')
96 % self._value)
97
98 def join(self, context, mapping, sep):
99 raise error.ParseError(_('%r is not iterable') % self._value)
100
101 def show(self, context, mapping):
102 return pycompat.bytestr(self._value)
103
104 def tovalue(self, context, mapping):
105 return self._value
106
69 # stub for representing a date type; may be a real date type that can
107 # stub for representing a date type; may be a real date type that can
70 # provide a readable string value
108 # provide a readable string value
71 class date(object):
109 class date(object):
@@ -447,6 +485,20 b' def evalrawexp(context, mapping, arg):'
447 func, data = arg
485 func, data = arg
448 return func(context, mapping, data)
486 return func(context, mapping, data)
449
487
488 def evalwrapped(context, mapping, arg):
489 """Evaluate given argument to wrapped object"""
490 thing = evalrawexp(context, mapping, arg)
491 return makewrapped(context, mapping, thing)
492
493 def makewrapped(context, mapping, thing):
494 """Lift object to a wrapped type"""
495 if isinstance(thing, wrapped):
496 return thing
497 thing = _unthunk(context, mapping, thing)
498 if isinstance(thing, bytes):
499 return wrappedbytes(thing)
500 return wrappedvalue(thing)
501
450 def evalfuncarg(context, mapping, arg):
502 def evalfuncarg(context, mapping, arg):
451 """Evaluate given argument as value type"""
503 """Evaluate given argument as value type"""
452 return unwrapvalue(context, mapping, evalrawexp(context, mapping, arg))
504 return unwrapvalue(context, mapping, evalrawexp(context, mapping, arg))
General Comments 0
You need to be logged in to leave comments. Login now