Patchwork [1,of,2] templater: move templatefilters.func into the same place as the other funcs

login
register
mail settings
Submitter Sean Farley
Date May 20, 2013, 9:45 p.m.
Message ID <4edf5b7dddd1a3ab5350.1369086301@laptop.local>
Download mbox | patch
Permalink /patch/1657/
State Accepted
Commit 8eef5b93db9d2d4f89463a5d5740ce71694e8284
Headers show

Comments

Sean Farley - May 20, 2013, 9:45 p.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1365638198 18000
#      Wed Apr 10 18:56:38 2013 -0500
# Node ID 4edf5b7dddd1a3ab5350590256f5145b54ea1e30
# Parent  0ec31231afad3fc171f882226aae50d4737559b5
templater: move templatefilters.func into the same place as the other funcs
Wagner Bruna - May 20, 2013, 10:37 p.m.
Em 20-05-2013 18:45, Sean Farley escreveu:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley@gmail.com>
> # Date 1365638198 18000
> #      Wed Apr 10 18:56:38 2013 -0500
> # Node ID 4edf5b7dddd1a3ab5350590256f5145b54ea1e30
> # Parent  0ec31231afad3fc171f882226aae50d4737559b5
> templater: move templatefilters.func into the same place as the other funcs
> 
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -3,13 +3,12 @@
>  # Copyright 2005-2008 Matt Mackall <mpm@selenic.com>
>  #
>  # This software may be used and distributed according to the terms of the
>  # GNU General Public License version 2 or any later version.
>  
> -from i18n import _
>  import cgi, re, os, time, urllib
> -import encoding, node, util, error
> +import encoding, node, util

The module still needs those, for error reporting (which suggests it also
needs a few (doc)tests).

Regards,
Wagner

>  import hbisect
>  
>  def addbreaks(text):
>      """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of
>      every line except the last.
> @@ -399,36 +398,7 @@
>      if websubtable:
>          for regexp, format in websubtable:
>              text = regexp.sub(format, text)
>      return text
>  
> -def fillfunc(context, mapping, args):
> -    if not (1 <= len(args) <= 2):
> -        raise error.ParseError(_("fill expects one or two arguments"))
> -
> -    text = stringify(args[0][0](context, mapping, args[0][1]))
> -    width = 76
> -    if len(args) == 2:
> -        try:
> -            width = int(stringify(args[1][0](context, mapping, args[1][1])))
> -        except ValueError:
> -            raise error.ParseError(_("fill expects an integer width"))
> -
> -    return fill(text, width)
> -
> -def datefunc(context, mapping, args):
> -    if not (1 <= len(args) <= 2):
> -        raise error.ParseError(_("date expects one or two arguments"))
> -
> -    date = args[0][0](context, mapping, args[0][1])
> -    if len(args) == 2:
> -        fmt = stringify(args[1][0](context, mapping, args[1][1]))
> -        return util.datestr(date, fmt)
> -    return util.datestr(date)
> -
> -funcs = {
> -    "fill": fillfunc,
> -    "date": datefunc,
> -}
> -
>  # tell hggettext to extract docstrings from these functions:
>  i18nfunctions = filters.values()
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -197,13 +197,10 @@
>      n = getsymbol(exp[1])
>      args = [compileexp(x, context) for x in getlist(exp[2])]
>      if n in funcs:
>          f = funcs[n]
>          return (f, args)
> -    if n in templatefilters.funcs:
> -        f = templatefilters.funcs[n]
> -        return (f, args)
>      if n in context._filters:
>          if len(args) != 1:
>              raise error.ParseError(_("filter %s expects one argument") % n)
>          f = context._filters[n]
>          return (runfilter, (args[0][0], args[0][1], f))
> @@ -299,10 +296,34 @@
>      text = stringify(args[0][0](context, mapping, args[0][1]))
>      style = stringify(args[1][0](context, mapping, args[1][1]))
>  
>      return minirst.format(text, style=style, keep=['verbose'])
>  
> +def fill(context, mapping, args):
> +    if not (1 <= len(args) <= 2):
> +        raise error.ParseError(_("fill expects one or two arguments"))
> +
> +    text = stringify(args[0][0](context, mapping, args[0][1]))
> +    width = 76
> +    if len(args) == 2:
> +        try:
> +            width = int(stringify(args[1][0](context, mapping, args[1][1])))
> +        except ValueError:
> +            raise error.ParseError(_("fill expects an integer width"))
> +
> +    return templatefilters.fill(text, width)
> +
> +def date(context, mapping, args):
> +    if not (1 <= len(args) <= 2):
> +        raise error.ParseError(_("date expects one or two arguments"))
> +
> +    date = args[0][0](context, mapping, args[0][1])
> +    if len(args) == 2:
> +        fmt = stringify(args[1][0](context, mapping, args[1][1]))
> +        return util.datestr(date, fmt)
> +    return util.datestr(date)
> +
>  methods = {
>      "string": lambda e, c: (runstring, e[1]),
>      "symbol": lambda e, c: (runsymbol, e[1]),
>      "group": lambda e, c: compileexp(e[1], c),
>  #    ".": buildmember,
> @@ -317,10 +338,12 @@
>      "ifeq": ifeq,
>      "join": join,
>      "label": label,
>      "rstdoc": rstdoc,
>      "sub": sub,
> +    "fill": fill,
> +    "date": date,
>  }
>  
>  # template engine
>  
>  path = ['templates', '../templates']
Sean Farley - May 21, 2013, 8:42 p.m.
Wagner Bruna writes:

> Em 20-05-2013 18:45, Sean Farley escreveu:
>> # HG changeset patch
>> # User Sean Farley <sean.michael.farley@gmail.com>
>> # Date 1365638198 18000
>> #      Wed Apr 10 18:56:38 2013 -0500
>> # Node ID 4edf5b7dddd1a3ab5350590256f5145b54ea1e30
>> # Parent  0ec31231afad3fc171f882226aae50d4737559b5
>> templater: move templatefilters.func into the same place as the other funcs
>> 
>> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
>> --- a/mercurial/templatefilters.py
>> +++ b/mercurial/templatefilters.py
>> @@ -3,13 +3,12 @@
>>  # Copyright 2005-2008 Matt Mackall <mpm@selenic.com>
>>  #
>>  # This software may be used and distributed according to the terms of the
>>  # GNU General Public License version 2 or any later version.
>>  
>> -from i18n import _
>>  import cgi, re, os, time, urllib
>> -import encoding, node, util, error
>> +import encoding, node, util
>
> The module still needs those, for error reporting (which suggests it also
> needs a few (doc)tests).

Huh, really? Even if there is no use of the module in that file? I
couldn't find any file in the mercurial repo that imports error but
doesn't have some call to error.foo. I fully admit, though, that I am no
expert in python so I could be missing something obvious here.
Wagner Bruna - May 21, 2013, 10:50 p.m.
Em 21-05-2013 17:42, Sean Farley escreveu:
> 
> Wagner Bruna writes:
> 
>> Em 20-05-2013 18:45, Sean Farley escreveu:
>>> # HG changeset patch
>>> # User Sean Farley <sean.michael.farley@gmail.com>
>>> # Date 1365638198 18000
>>> #      Wed Apr 10 18:56:38 2013 -0500
>>> # Node ID 4edf5b7dddd1a3ab5350590256f5145b54ea1e30
>>> # Parent  0ec31231afad3fc171f882226aae50d4737559b5
>>> templater: move templatefilters.func into the same place as the other funcs
>>>
>>> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
>>> --- a/mercurial/templatefilters.py
>>> +++ b/mercurial/templatefilters.py
>>> @@ -3,13 +3,12 @@
>>>  # Copyright 2005-2008 Matt Mackall <mpm@selenic.com>
>>>  #
>>>  # This software may be used and distributed according to the terms of the
>>>  # GNU General Public License version 2 or any later version.
>>>  
>>> -from i18n import _
>>>  import cgi, re, os, time, urllib
>>> -import encoding, node, util, error
>>> +import encoding, node, util
>>
>> The module still needs those, for error reporting (which suggests it also
>> needs a few (doc)tests).
> 
> Huh, really? Even if there is no use of the module in that file?

Huh, my mistake; I probably didn't apply the patch correctly here. These
changes are fine.

Regards,
Wagner

> I
> couldn't find any file in the mercurial repo that imports error but
> doesn't have some call to error.foo. I fully admit, though, that I am no
> expert in python so I could be missing something obvious here.
Matt Mackall - May 23, 2013, 9:32 p.m.
On Tue, 2013-05-21 at 19:50 -0300, Wagner Bruna wrote:
> Em 21-05-2013 17:42, Sean Farley escreveu:
> > 
> > Wagner Bruna writes:
> > 
> >> Em 20-05-2013 18:45, Sean Farley escreveu:
> >>> # HG changeset patch
> >>> # User Sean Farley <sean.michael.farley@gmail.com>
> >>> # Date 1365638198 18000
> >>> #      Wed Apr 10 18:56:38 2013 -0500
> >>> # Node ID 4edf5b7dddd1a3ab5350590256f5145b54ea1e30
> >>> # Parent  0ec31231afad3fc171f882226aae50d4737559b5
> >>> templater: move templatefilters.func into the same place as the other funcs
> >>>
> >>> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> >>> --- a/mercurial/templatefilters.py
> >>> +++ b/mercurial/templatefilters.py
> >>> @@ -3,13 +3,12 @@
> >>>  # Copyright 2005-2008 Matt Mackall <mpm@selenic.com>
> >>>  #
> >>>  # This software may be used and distributed according to the terms of the
> >>>  # GNU General Public License version 2 or any later version.
> >>>  
> >>> -from i18n import _
> >>>  import cgi, re, os, time, urllib
> >>> -import encoding, node, util, error
> >>> +import encoding, node, util
> >>
> >> The module still needs those, for error reporting (which suggests it also
> >> needs a few (doc)tests).
> > 
> > Huh, really? Even if there is no use of the module in that file?
> 
> Huh, my mistake; I probably didn't apply the patch correctly here. These
> changes are fine.

Ok, I've queued them for default, thanks.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -3,13 +3,12 @@ 
 # Copyright 2005-2008 Matt Mackall <mpm@selenic.com>
 #
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from i18n import _
 import cgi, re, os, time, urllib
-import encoding, node, util, error
+import encoding, node, util
 import hbisect
 
 def addbreaks(text):
     """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of
     every line except the last.
@@ -399,36 +398,7 @@ 
     if websubtable:
         for regexp, format in websubtable:
             text = regexp.sub(format, text)
     return text
 
-def fillfunc(context, mapping, args):
-    if not (1 <= len(args) <= 2):
-        raise error.ParseError(_("fill expects one or two arguments"))
-
-    text = stringify(args[0][0](context, mapping, args[0][1]))
-    width = 76
-    if len(args) == 2:
-        try:
-            width = int(stringify(args[1][0](context, mapping, args[1][1])))
-        except ValueError:
-            raise error.ParseError(_("fill expects an integer width"))
-
-    return fill(text, width)
-
-def datefunc(context, mapping, args):
-    if not (1 <= len(args) <= 2):
-        raise error.ParseError(_("date expects one or two arguments"))
-
-    date = args[0][0](context, mapping, args[0][1])
-    if len(args) == 2:
-        fmt = stringify(args[1][0](context, mapping, args[1][1]))
-        return util.datestr(date, fmt)
-    return util.datestr(date)
-
-funcs = {
-    "fill": fillfunc,
-    "date": datefunc,
-}
-
 # tell hggettext to extract docstrings from these functions:
 i18nfunctions = filters.values()
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -197,13 +197,10 @@ 
     n = getsymbol(exp[1])
     args = [compileexp(x, context) for x in getlist(exp[2])]
     if n in funcs:
         f = funcs[n]
         return (f, args)
-    if n in templatefilters.funcs:
-        f = templatefilters.funcs[n]
-        return (f, args)
     if n in context._filters:
         if len(args) != 1:
             raise error.ParseError(_("filter %s expects one argument") % n)
         f = context._filters[n]
         return (runfilter, (args[0][0], args[0][1], f))
@@ -299,10 +296,34 @@ 
     text = stringify(args[0][0](context, mapping, args[0][1]))
     style = stringify(args[1][0](context, mapping, args[1][1]))
 
     return minirst.format(text, style=style, keep=['verbose'])
 
+def fill(context, mapping, args):
+    if not (1 <= len(args) <= 2):
+        raise error.ParseError(_("fill expects one or two arguments"))
+
+    text = stringify(args[0][0](context, mapping, args[0][1]))
+    width = 76
+    if len(args) == 2:
+        try:
+            width = int(stringify(args[1][0](context, mapping, args[1][1])))
+        except ValueError:
+            raise error.ParseError(_("fill expects an integer width"))
+
+    return templatefilters.fill(text, width)
+
+def date(context, mapping, args):
+    if not (1 <= len(args) <= 2):
+        raise error.ParseError(_("date expects one or two arguments"))
+
+    date = args[0][0](context, mapping, args[0][1])
+    if len(args) == 2:
+        fmt = stringify(args[1][0](context, mapping, args[1][1]))
+        return util.datestr(date, fmt)
+    return util.datestr(date)
+
 methods = {
     "string": lambda e, c: (runstring, e[1]),
     "symbol": lambda e, c: (runsymbol, e[1]),
     "group": lambda e, c: compileexp(e[1], c),
 #    ".": buildmember,
@@ -317,10 +338,12 @@ 
     "ifeq": ifeq,
     "join": join,
     "label": label,
     "rstdoc": rstdoc,
     "sub": sub,
+    "fill": fill,
+    "date": date,
 }
 
 # template engine
 
 path = ['templates', '../templates']