##// END OF EJS Templates
templater: move getdictitem() to hybrid class...
Yuya Nishihara -
r38260:12b6ee9e default
parent child Browse files
Show More
@@ -262,12 +262,12 b' def get(context, mapping, args):'
262 raise error.ParseError(_("get() expects two arguments"))
262 raise error.ParseError(_("get() expects two arguments"))
263
263
264 dictarg = evalwrapped(context, mapping, args[0])
264 dictarg = evalwrapped(context, mapping, args[0])
265 if not util.safehasattr(dictarg, 'get'):
265 if not util.safehasattr(dictarg, 'getmember'):
266 # i18n: "get" is a keyword
266 # i18n: "get" is a keyword
267 raise error.ParseError(_("get() expects a dict as first argument"))
267 raise error.ParseError(_("get() expects a dict as first argument"))
268
268
269 key = evalfuncarg(context, mapping, args[1])
269 key = evalfuncarg(context, mapping, args[1])
270 return templateutil.getdictitem(dictarg, key)
270 return dictarg.getmember(context, mapping, key)
271
271
272 @templatefunc('if(expr, then[, else])')
272 @templatefunc('if(expr, then[, else])')
273 def if_(context, mapping, args):
273 def if_(context, mapping, args):
@@ -128,6 +128,17 b' class hybrid(wrapped):'
128 self._joinfmt = joinfmt
128 self._joinfmt = joinfmt
129 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved
129 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved
130
130
131 def getmember(self, context, mapping, key):
132 # TODO: maybe split hybrid list/dict types?
133 if not util.safehasattr(self._values, 'get'):
134 raise error.ParseError(_('not a dictionary'))
135 return self._wrapvalue(key, self._values.get(key))
136
137 def _wrapvalue(self, key, val):
138 if val is None:
139 return
140 return wraphybridvalue(self, key, val)
141
131 def itermaps(self, context):
142 def itermaps(self, context):
132 makemap = self._makemap
143 makemap = self._makemap
133 for x in self._values:
144 for x in self._values:
@@ -667,8 +678,8 b' def runmember(context, mapping, data):'
667 lm = context.overlaymap(mapping, d.tomap())
678 lm = context.overlaymap(mapping, d.tomap())
668 return runsymbol(context, lm, memb)
679 return runsymbol(context, lm, memb)
669 try:
680 try:
670 if util.safehasattr(d, 'get'):
681 if util.safehasattr(d, 'getmember'):
671 return getdictitem(d, memb)
682 return d.getmember(context, mapping, memb)
672 raise error.ParseError
683 raise error.ParseError
673 except error.ParseError:
684 except error.ParseError:
674 sym = findsymbolicname(darg)
685 sym = findsymbolicname(darg)
@@ -693,12 +704,6 b' def runarithmetic(context, mapping, data'
693 except ZeroDivisionError:
704 except ZeroDivisionError:
694 raise error.Abort(_('division by zero is not defined'))
705 raise error.Abort(_('division by zero is not defined'))
695
706
696 def getdictitem(dictarg, key):
697 val = dictarg.get(key)
698 if val is None:
699 return
700 return wraphybridvalue(dictarg, key, val)
701
702 def joinitems(itemiter, sep):
707 def joinitems(itemiter, sep):
703 """Join items with the separator; Returns generator of bytes"""
708 """Join items with the separator; Returns generator of bytes"""
704 first = True
709 first = True
General Comments 0
You need to be logged in to leave comments. Login now