[RFC] [PATCH] Support arguments in template expandos (v2)

Martin Geisler mg at lazybytes.net
Sat May 16 10:12:56 CDT 2009


Rocco Rutte <pdmef at gmx.net> writes:

Hi Rocco,

Some comments below -- I don't know if they're important since we
haven't decided if we should go with this patch or the one from Brendan:

  http://markmail.org/message/uzqkaefuux2qjgu7

> -def nonempty(str):
> -  return str or "(none)"
> +def nonempty(str, args=None):
> +  return str or (args and args[0]) or "(none)"
>  
>  filters = {
>      "addbreaks": nl2br,
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -39,10 +39,13 @@ class engine(object):
>      {key%format}.
>  
>      filter uses function to transform value. syntax is
> -    {key|filter1|filter2|...}.'''
> +    {key|filter1 ["arg1" ["arg2" ...]]|filter2 ["arg1" ["arg2" ...]]|...}.'''
>  
> -    template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))"
> -                             r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]")
> +    template_re = re.compile(r"(?:(?:#(?=[\w\s\"\|%()]+#))|(?:{(?=[\w\s\"\|%()]+})))"
> +                             r"(\w+)(?:(?:%(\w+(?:\s\"[^\"]*\")*))|"
> +                             r"((?:\|\w+(?:\s\"[^\"]*\")*)*))[#}]")

Wow, this regexp is starting to become a little complicated :-) I don't
know if it could be simplified, but we loose some speed here.

> +
> +    args_re = re.compile(r"\"\s\"")

I think it better to write this as r'"\s"' -- it's confusing when raw
strings contain both raw backslashes and backslashes which escape stuff.

>      def __init__(self, loader, filters={}, defaults={}):
>          self.loader = loader
> @@ -101,7 +104,13 @@ class engine(object):
>              else:
>                  if fl:
>                      for f in fl.split("|")[1:]:
> -                        v = self.filters[f](v)
> +                        key = f.split(' ')[0]
> +                        if '"' in f:
> +                            # remove initial ' "' and trailing '"'
> +                            args = self.args_re.split(f[len(key)+2:-1])
> +                            v = self.filters[key](v, args)

Using *args when calling filter[key] would be nice because it allows the
nonempty filter to be defined as

  def nonempty(str, default="(none)"):
      return str or default

(The templater module contains a bunch of unmarked strings -- I think
we'll worry about translating them later when we also know how to
extract the other strings from the .tmpl files.)

-- 
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20090516/df010248/attachment.pgp 


More information about the Mercurial-devel mailing list