##// END OF EJS Templates
templater: define interface for objects requiring unwrapvalue()...
Yuya Nishihara -
r37297:26f6fc17 default
parent child Browse files
Show More
@@ -29,7 +29,11 b' class TemplateNotFound(error.Abort):'
29
29
30 class wrapped(object):
30 class wrapped(object):
31 """Object requiring extra conversion prior to displaying or processing
31 """Object requiring extra conversion prior to displaying or processing
32 as value"""
32 as value
33
34 Use unwrapvalue(), unwrapastype(), or unwraphybrid() to obtain the inner
35 object.
36 """
33
37
34 __metaclass__ = abc.ABCMeta
38 __metaclass__ = abc.ABCMeta
35
39
@@ -42,6 +46,13 b' class wrapped(object):'
42 not printable.
46 not printable.
43 """
47 """
44
48
49 @abc.abstractmethod
50 def tovalue(self, context, mapping):
51 """Move the inner value object out or create a value representation
52
53 A returned value must be serializable by templaterfilters.json().
54 """
55
45 # stub for representing a date type; may be a real date type that can
56 # stub for representing a date type; may be a real date type that can
46 # provide a readable string value
57 # provide a readable string value
47 class date(object):
58 class date(object):
@@ -85,6 +96,10 b' class hybrid(wrapped):'
85 return gen()
96 return gen()
86 return gen
97 return gen
87
98
99 def tovalue(self, context, mapping):
100 # TODO: return self._values and get rid of proxy methods
101 return self
102
88 def __contains__(self, x):
103 def __contains__(self, x):
89 return x in self._values
104 return x in self._values
90 def __getitem__(self, key):
105 def __getitem__(self, key):
@@ -108,8 +123,7 b' class mappable(wrapped):'
108 - "{manifest.rev}"
123 - "{manifest.rev}"
109
124
110 Unlike a hybrid, this does not simulate the behavior of the underling
125 Unlike a hybrid, this does not simulate the behavior of the underling
111 value. Use unwrapvalue(), unwrapastype(), or unwraphybrid() to obtain
126 value.
112 the inner object.
113 """
127 """
114
128
115 def __init__(self, gen, key, value, makemap):
129 def __init__(self, gen, key, value, makemap):
@@ -133,6 +147,9 b' class mappable(wrapped):'
133 return gen()
147 return gen()
134 return gen
148 return gen
135
149
150 def tovalue(self, context, mapping):
151 return _unthunk(context, mapping, self._value)
152
136 def hybriddict(data, key='key', value='value', fmt=None, gen=None):
153 def hybriddict(data, key='key', value='value', fmt=None, gen=None):
137 """Wrap data to support both dict-like and string-like operations"""
154 """Wrap data to support both dict-like and string-like operations"""
138 prefmt = pycompat.identity
155 prefmt = pycompat.identity
@@ -159,9 +176,9 b' def unwraphybrid(context, mapping, thing'
159
176
160 def unwrapvalue(context, mapping, thing):
177 def unwrapvalue(context, mapping, thing):
161 """Move the inner value object out of the wrapper"""
178 """Move the inner value object out of the wrapper"""
162 if not util.safehasattr(thing, '_value'):
179 if not isinstance(thing, wrapped):
163 return thing
180 return thing
164 return thing._value
181 return thing.tovalue(context, mapping)
165
182
166 def wraphybridvalue(container, key, value):
183 def wraphybridvalue(container, key, value):
167 """Wrap an element of hybrid container to be mappable
184 """Wrap an element of hybrid container to be mappable
General Comments 0
You need to be logged in to leave comments. Login now