[PATCH] templater: add 'env()' to fetch environment variables
Matt Harbison
mharbison72 at gmail.com
Wed Jan 18 04:40:41 UTC 2017
On Tue, 17 Jan 2017 07:51:26 -0500, Yuya Nishihara <yuya at tcha.org> wrote:
> On Mon, 16 Jan 2017 22:28:17 -0500, Matt Harbison wrote:
>> On Mon, 16 Jan 2017 07:25:24 -0500, Yuya Nishihara <yuya at tcha.org>
>> wrote:
>> > This can be a simple {env} (or {environ}) keyword and you can use
>> get()
>> > function, e.g. {get(env, 'pattern')}. showextras() is a good example
>> of
>> > building template dict.
>>
>> I've got to be missing something simple. I added this code:
>>
>> @templatekeyword('environ')
>> def showenviron(**args):
>> """A dictionary of environment variables."""
>>
>> env = encoding.environ
>> env = util.sortdict((k, env[k]) for k in sorted(env))
>> makemap = lambda k: {'key': k, 'value': env[k]}
>> c = [makemap(k) for k in env]
>> f = _showlist('environ', c, **args)
>> return _hybrid(f, env, makemap,
>> lambda x: '%s=%s' % (x['key'], x['value']))
>>
>> And these forms work:
>>
>> $ pattern=foo ../hg log -r . -T "{get(environ, 'PATTERN')}"
>> foo
>> $ pattern=foo ../hg log -r . -T "{environ % '{key} -> {value}\n'}"
>> !:: -> ::\
>> ALLUSERSPROFILE -> C:\ProgramData
>> ...
>>
>> But this doesn't. It spews a wall of text that looks like templater
>> internals:
>>
>> $ pattern=foo ../hg log -r . -T "{environ}"
>> filestermwidthnamespacesrevcachetroublesobsoleteactivebookmarkdatep1nodebookmarks...
>>
>> The {extras} template that I copy/pasted from lists key=value lines. I
>> also looked at 'namespaces', but couldn't figure out what triggered
>> this.
>
> The bare {extras} is rendered by _showlist() as an old-style template,
> and
> it appears _showlist() can't process dict values if no template provided.
> (It somehow yields a dict of full args.)
>
> https://www.mercurial-scm.org/repo/hg/file/4.0.2/mercurial/templatekw.py#l89
>
> One way to avoid spilling out the internal variables is to set the
> default
> 'environ' template.
Thanks! I never would have noticed that.
I see that the default template for 'extras' filters through
'stringescape', which isn't documented, so it isn't in the help. Any
reason it shouldn't be documented?
> https://www.mercurial-scm.org/repo/hg/file/4.0.2/mercurial/cmdutil.py#l1507
>
> Another problem is the 'environ' keyword conflict with the 'environ'
> template.
> We'll have to pick a plural name (e.g. 'envs', 'environs', 'envvars'.)
More information about the Mercurial-devel
mailing list