comparison mercurial/templateutil.py @ 37019:c97b936d8bb5

templater: use named function to expand template against mapping dict (API) And replace __call__(t, **mapping) in favor of generate(t, mapping). I prefer a named function here since the templater isn't a simple function-like object. .. api:: The templater is no longer callable. Use ``templater.generate(t, mapping)`` instead of ``templater(t, **pycompat.strkwargs(mapping))``.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 16 Mar 2018 21:39:32 +0900
parents 255f635c3204
children aa97e06a1912
comparison
equal deleted inserted replaced
37018:3e74d3cc500f 37019:c97b936d8bb5
182 for each value, expand 'foo'. if 'last_foo' in template 182 for each value, expand 'foo'. if 'last_foo' in template
183 map, expand it instead of 'foo' for last key. 183 map, expand it instead of 'foo' for last key.
184 184
185 expand 'end_foos'. 185 expand 'end_foos'.
186 ''' 186 '''
187 strmapping = pycompat.strkwargs(mapping)
188 if not plural: 187 if not plural:
189 plural = name + 's' 188 plural = name + 's'
190 if not values: 189 if not values:
191 noname = 'no_' + plural 190 noname = 'no_' + plural
192 if noname in templ: 191 if noname in templ:
193 yield templ(noname, **strmapping) 192 yield templ.generate(noname, mapping)
194 return 193 return
195 if name not in templ: 194 if name not in templ:
196 if isinstance(values[0], bytes): 195 if isinstance(values[0], bytes):
197 yield separator.join(values) 196 yield separator.join(values)
198 else: 197 else:
201 r.update(mapping) 200 r.update(mapping)
202 yield r 201 yield r
203 return 202 return
204 startname = 'start_' + plural 203 startname = 'start_' + plural
205 if startname in templ: 204 if startname in templ:
206 yield templ(startname, **strmapping) 205 yield templ.generate(startname, mapping)
207 vmapping = mapping.copy() 206 vmapping = mapping.copy()
208 def one(v, tag=name): 207 def one(v, tag=name):
209 try: 208 try:
210 vmapping.update(v) 209 vmapping.update(v)
211 # Python 2 raises ValueError if the type of v is wrong. Python 210 # Python 2 raises ValueError if the type of v is wrong. Python
216 # bytes. Python 3 raises TypeError. 215 # bytes. Python 3 raises TypeError.
217 for a, b in v: 216 for a, b in v:
218 vmapping[a] = b 217 vmapping[a] = b
219 except (TypeError, ValueError): 218 except (TypeError, ValueError):
220 vmapping[name] = v 219 vmapping[name] = v
221 return templ(tag, **pycompat.strkwargs(vmapping)) 220 return templ.generate(tag, vmapping)
222 lastname = 'last_' + name 221 lastname = 'last_' + name
223 if lastname in templ: 222 if lastname in templ:
224 last = values.pop() 223 last = values.pop()
225 else: 224 else:
226 last = None 225 last = None
228 yield one(v) 227 yield one(v)
229 if last is not None: 228 if last is not None:
230 yield one(last, tag=lastname) 229 yield one(last, tag=lastname)
231 endname = 'end_' + plural 230 endname = 'end_' + plural
232 if endname in templ: 231 if endname in templ:
233 yield templ(endname, **strmapping) 232 yield templ.generate(endname, mapping)
234 233
235 def stringify(thing): 234 def stringify(thing):
236 """Turn values into bytes by converting into text and concatenating them""" 235 """Turn values into bytes by converting into text and concatenating them"""
237 thing = unwraphybrid(thing) 236 thing = unwraphybrid(thing)
238 if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes): 237 if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes):