comparison mercurial/templateutil.py @ 38269:cf8d210dfac4

templater: drop hybrid-ness on unwrapvalue() Proxy methods are no longer necessary as list/dict functions go through the wrapped interface.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 19 Mar 2018 00:26:55 +0900
parents 49ef1539b84e
children 630c62804383
comparison
equal deleted inserted replaced
38268:49ef1539b84e 38269:cf8d210dfac4
233 if callable(gen): 233 if callable(gen):
234 return gen() 234 return gen()
235 return gen 235 return gen
236 236
237 def tovalue(self, context, mapping): 237 def tovalue(self, context, mapping):
238 # TODO: return self._values and get rid of proxy methods 238 # TODO: make it non-recursive for trivial lists/dicts
239 return self 239 xs = self._values
240 240 if util.safehasattr(xs, 'get'):
241 def __contains__(self, x): 241 return {k: unwrapvalue(context, mapping, v)
242 return x in self._values 242 for k, v in xs.iteritems()}
243 def __getitem__(self, key): 243 return [unwrapvalue(context, mapping, x) for x in xs]
244 return self._values[key]
245 def __len__(self):
246 return len(self._values)
247 def __iter__(self):
248 return iter(self._values)
249 def __getattr__(self, name):
250 if name not in (r'get', r'items', r'iteritems', r'iterkeys',
251 r'itervalues', r'keys', r'values'):
252 raise AttributeError(name)
253 return getattr(self._values, name)
254 244
255 class mappable(wrapped): 245 class mappable(wrapped):
256 """Wrapper for non-list/dict object to support map operation 246 """Wrapper for non-list/dict object to support map operation
257 247
258 This class allows us to handle both: 248 This class allows us to handle both:
259 - "{manifest}" 249 - "{manifest}"
260 - "{manifest % '{rev}:{node}'}" 250 - "{manifest % '{rev}:{node}'}"
261 - "{manifest.rev}" 251 - "{manifest.rev}"
262
263 Unlike a hybrid, this does not simulate the behavior of the underling
264 value.
265 """ 252 """
266 253
267 def __init__(self, gen, key, value, makemap): 254 def __init__(self, gen, key, value, makemap):
268 self._gen = gen # generator or function returning generator 255 self._gen = gen # generator or function returning generator
269 self._key = key 256 self._key = key