Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/templateutil.py @ 38237:d48b80d58848
templater: unify unwrapvalue() with _unwrapvalue()
All weird generators got removed from the hgweb codebase. We still have
inconsistent behavior regarding join() of a byte string, which will be
addressed later.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 04 Apr 2018 21:06:14 +0900 |
parents | 61cecab0cc20 |
children | 7824783a6d5e |
comparison
equal
deleted
inserted
replaced
38236:61cecab0cc20 | 38237:d48b80d58848 |
---|---|
282 """Return an object which can be stringified possibly by using a legacy | 282 """Return an object which can be stringified possibly by using a legacy |
283 template""" | 283 template""" |
284 if not isinstance(thing, wrapped): | 284 if not isinstance(thing, wrapped): |
285 return thing | 285 return thing |
286 return thing.show(context, mapping) | 286 return thing.show(context, mapping) |
287 | |
288 def unwrapvalue(context, mapping, thing): | |
289 """Move the inner value object out of the wrapper""" | |
290 if not isinstance(thing, wrapped): | |
291 return thing | |
292 return thing.tovalue(context, mapping) | |
293 | 287 |
294 def wraphybridvalue(container, key, value): | 288 def wraphybridvalue(container, key, value): |
295 """Wrap an element of hybrid container to be mappable | 289 """Wrap an element of hybrid container to be mappable |
296 | 290 |
297 The key is passed to the makemap function of the given container, which | 291 The key is passed to the makemap function of the given container, which |
453 func, data = arg | 447 func, data = arg |
454 return func(context, mapping, data) | 448 return func(context, mapping, data) |
455 | 449 |
456 def evalfuncarg(context, mapping, arg): | 450 def evalfuncarg(context, mapping, arg): |
457 """Evaluate given argument as value type""" | 451 """Evaluate given argument as value type""" |
458 return _unwrapvalue(context, mapping, evalrawexp(context, mapping, arg)) | 452 return unwrapvalue(context, mapping, evalrawexp(context, mapping, arg)) |
459 | 453 |
460 # TODO: unify this with unwrapvalue() once the bug of templatefunc.join() | 454 def unwrapvalue(context, mapping, thing): |
461 # is fixed. we can't do that right now because join() has to take a generator | 455 """Move the inner value object out of the wrapper""" |
462 # of byte strings as it is, not a lazy byte string. | |
463 def _unwrapvalue(context, mapping, thing): | |
464 if isinstance(thing, wrapped): | 456 if isinstance(thing, wrapped): |
465 return thing.tovalue(context, mapping) | 457 return thing.tovalue(context, mapping) |
466 # evalrawexp() may return string, generator of strings or arbitrary object | 458 # evalrawexp() may return string, generator of strings or arbitrary object |
467 # such as date tuple, but filter does not want generator. | 459 # such as date tuple, but filter does not want generator. |
468 return _unthunk(context, mapping, thing) | 460 return _unthunk(context, mapping, thing) |
490 a (unixtime, offset) tuple""" | 482 a (unixtime, offset) tuple""" |
491 thing = evalrawexp(context, mapping, arg) | 483 thing = evalrawexp(context, mapping, arg) |
492 return unwrapdate(context, mapping, thing, err) | 484 return unwrapdate(context, mapping, thing, err) |
493 | 485 |
494 def unwrapdate(context, mapping, thing, err=None): | 486 def unwrapdate(context, mapping, thing, err=None): |
495 thing = _unwrapvalue(context, mapping, thing) | 487 thing = unwrapvalue(context, mapping, thing) |
496 try: | 488 try: |
497 return dateutil.parsedate(thing) | 489 return dateutil.parsedate(thing) |
498 except AttributeError: | 490 except AttributeError: |
499 raise error.ParseError(err or _('not a date tuple nor a string')) | 491 raise error.ParseError(err or _('not a date tuple nor a string')) |
500 except error.ParseError: | 492 except error.ParseError: |
505 def evalinteger(context, mapping, arg, err=None): | 497 def evalinteger(context, mapping, arg, err=None): |
506 thing = evalrawexp(context, mapping, arg) | 498 thing = evalrawexp(context, mapping, arg) |
507 return unwrapinteger(context, mapping, thing, err) | 499 return unwrapinteger(context, mapping, thing, err) |
508 | 500 |
509 def unwrapinteger(context, mapping, thing, err=None): | 501 def unwrapinteger(context, mapping, thing, err=None): |
510 thing = _unwrapvalue(context, mapping, thing) | 502 thing = unwrapvalue(context, mapping, thing) |
511 try: | 503 try: |
512 return int(thing) | 504 return int(thing) |
513 except (TypeError, ValueError): | 505 except (TypeError, ValueError): |
514 raise error.ParseError(err or _('not an integer')) | 506 raise error.ParseError(err or _('not an integer')) |
515 | 507 |
525 else: | 517 else: |
526 thing = func(context, mapping, data) | 518 thing = func(context, mapping, data) |
527 return stringify(context, mapping, thing) | 519 return stringify(context, mapping, thing) |
528 | 520 |
529 _unwrapfuncbytype = { | 521 _unwrapfuncbytype = { |
530 None: _unwrapvalue, | 522 None: unwrapvalue, |
531 bytes: stringify, | 523 bytes: stringify, |
532 date: unwrapdate, | 524 date: unwrapdate, |
533 int: unwrapinteger, | 525 int: unwrapinteger, |
534 } | 526 } |
535 | 527 |