[PATCH 2 of 3 RFC] templater: add support for mapping lookups

Mads Kiilerich mads at kiilerich.com
Sun Sep 19 15:03:42 CDT 2010


  Dan Villiom Podlaski Christiansen wrote, On 09/19/2010 09:08 PM:
> # HG changeset patch
> # User Dan Villiom Podlaski Christiansen<danchr at gmail.com>
> # Date 1284923228 -7200
> # Node ID 2be04fa93c4d8804814df7143c4f9864f60eb6bf
> # Parent  f6c0b2e438255faa3eda18e78d3478ccfce66a44
> templater: add support for mapping lookups.
>
> This is a relatively simple implementation; the lookup character is
> '.'. Please note that in this implementation, filters cannot be
> applied to the results of mappings. In other words, '{x.y|z}' won't
> work as expected.

Shouldn't that be fixed before we consider including this patch?

/Mads

> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -110,6 +110,14 @@ class engine(object):
>                   # If so, return the expanded value.
>                   yield i
>
> +    def _getitem(self, mapping, args):
> +        name, item = args
> +        v = self._get(mapping, name)
> +        if not isinstance(v, dict):
> +            raise SyntaxError(_("error expanding '%s.%s'")
> +                              % (name, item))
> +        return v.get(item, '')
> +
>       def _parse(self, tmpl):
>           '''preparse a template'''
>           parsed = []
> @@ -141,6 +149,9 @@ class engine(object):
>                   key, t = expr.split('%')
>                   parsed.append((self._format, (key.strip(),
>                                                 self._load(t.strip()))))
> +            elif '.' in expr:
> +                key, member = expr.split('.')
> +                parsed.append((self._getitem, (key.strip(), member.strip())))
>               elif '|' in expr:
>                   parts = expr.split('|')
>                   val = parts[0].strip()



More information about the Mercurial-devel mailing list