[PATCH] templates: limit -Tlist to supported templates (RFC)

Yuya Nishihara yuya at tcha.org
Tue Dec 15 08:24:06 CST 2015


On Sun, 13 Dec 2015 23:21:44 -0600, timeless wrote:
> # HG changeset patch
> # User timeless <timeless at mozdev.org>
> # Date 1450065308 0
> #      Mon Dec 14 03:55:08 2015 +0000
> # Node ID 377cf7350fe05e06a5e8217c0e22c17d647e3cd5
> # Parent  944af8e2eb4cddf96ba5b8a96854528b40979715
> templates: limit -Tlist to supported templates (RFC)
> 
> I'm not sure whether using __contains__/in would be preferred
> or using a try-raise-except (this patch does both, which is
> wrong....).
> 
> The benefit of this is that -T list output is vaguely useful.

> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -828,7 +828,7 @@
>  
>  engines = {'default': engine}
>  
> -def stylelist():
> +def stylelist(topic=None):
>      paths = templatepaths()
>      if not paths:
>          return _('no templates found, try `hg debuginstall` for more info')
> @@ -837,7 +837,12 @@
>      for file in dirlist:
>          split = file.split(".")
>          if split[0] == "map-cmdline":
> -            stylelist.append(split[1])
> +            try:
> +                t = templater(templatepath(file), topic=topic, liststyles=False)
> +                if topic is None or t.__contains__(topic):
> +                    stylelist.append(split[1])
> +            except error.Abort:
> +                pass

"topic in t" should work.

> @@ -846,7 +851,7 @@
>  class templater(object):
>  
>      def __init__(self, mapfile, filters=None, defaults=None, cache=None,
> -                 minchunk=1024, maxchunk=65536):
> +                 minchunk=1024, maxchunk=65536, topic=None, liststyles=True):
>          '''set up template engine.
>          mapfile is name of file to read map definitions from.
>          filters is dict of functions. each transforms a value into another.
> @@ -873,8 +878,11 @@
>          if not mapfile:
>              return
>          if not os.path.exists(mapfile):
> +            hint = None
> +            if liststyles:
> +                hint=_("available styles: %s") % stylelist(topic)
>              raise error.Abort(_("style '%s' not found") % mapfile,
> -                             hint=_("available styles: %s") % stylelist())
> +                             hint=hint)

It seems passing topic to the templater is layering violation. And we can't
list all style-like items here because the formatter has its own built-in
formats (e.g. "json").

Assuming this code path would be only reachable by log --style option, we can
leave it listing all style files.


More information about the Mercurial-devel mailing list