comparison mercurial/templater.py @ 18582:ef78450c8df6

templater: add get() function to access dict element (e.g. extra)
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Fri, 08 Feb 2013 23:49:14 +0100
parents 9bfb53106328
children f5db3092790f
comparison
equal deleted inserted replaced
18581:3490c91a1fcb 18582:ef78450c8df6
205 if len(args) != 1: 205 if len(args) != 1:
206 raise error.ParseError(_("filter %s expects one argument") % n) 206 raise error.ParseError(_("filter %s expects one argument") % n)
207 f = context._filters[n] 207 f = context._filters[n]
208 return (runfilter, (args[0][0], args[0][1], f)) 208 return (runfilter, (args[0][0], args[0][1], f))
209 209
210 def get(context, mapping, args):
211 if len(args) != 2:
212 # i18n: "get" is a keyword
213 raise error.ParseError(_("get() expects two arguments"))
214
215 dictarg = args[0][0](context, mapping, args[0][1])
216 if not util.safehasattr(dictarg, 'get'):
217 # i18n: "get" is a keyword
218 raise error.ParseError(_("get() expects a dict as first argument"))
219
220 key = args[1][0](context, mapping, args[1][1])
221 yield dictarg.get(key)
222
210 def join(context, mapping, args): 223 def join(context, mapping, args):
211 if not (1 <= len(args) <= 2): 224 if not (1 <= len(args) <= 2):
212 # i18n: "join" is a keyword 225 # i18n: "join" is a keyword
213 raise error.ParseError(_("join expects one or two arguments")) 226 raise error.ParseError(_("join expects one or two arguments"))
214 227
283 "%": buildmap, 296 "%": buildmap,
284 "func": buildfunc, 297 "func": buildfunc,
285 } 298 }
286 299
287 funcs = { 300 funcs = {
301 "get": get,
288 "if": if_, 302 "if": if_,
289 "ifeq": ifeq, 303 "ifeq": ifeq,
290 "join": join, 304 "join": join,
305 "label": label,
291 "sub": sub, 306 "sub": sub,
292 "label": label,
293 } 307 }
294 308
295 # template engine 309 # template engine
296 310
297 path = ['templates', '../templates'] 311 path = ['templates', '../templates']