Patchwork [4,of,4] templater: make _hybrid provide more list/dict-like methods

login
register
mail settings
Submitter Yuya Nishihara
Date April 4, 2017, 4 p.m.
Message ID <236441d523b03a227a05.1491321659@mimosa>
Download mbox | patch
Permalink /patch/19956/
State Accepted
Headers show

Comments

Yuya Nishihara - April 4, 2017, 4 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1491312719 -32400
#      Tue Apr 04 22:31:59 2017 +0900
# Node ID 236441d523b03a227a0562497cbd060f795d3e13
# Parent  a78c9de39862cd089aaddd9536902c7c52dfe1e6
templater: make _hybrid provide more list/dict-like methods

So the JSON filter works.
Ryan McElroy - April 7, 2017, 8:51 a.m.
On 4/4/17 5:00 PM, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1491312719 -32400
> #      Tue Apr 04 22:31:59 2017 +0900
> # Node ID 236441d523b03a227a0562497cbd060f795d3e13
> # Parent  a78c9de39862cd089aaddd9536902c7c52dfe1e6
> templater: make _hybrid provide more list/dict-like methods

Sorry for the slow review. I've looked over this series and it looks 
like a good improvement to me. Being able to filter things like extra or 
files through json is a big win in my opinion!

I've marked as pre-reviewed in patchwork.

>
> So the JSON filter works.
>
> diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
> --- a/mercurial/templatekw.py
> +++ b/mercurial/templatekw.py
> @@ -28,6 +28,7 @@ class _hybrid(object):
>       and to access raw values:
>       - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}"
>       - "{get(extras, key)}"
> +    - "{files|json}"
>       """
>   
>       def __init__(self, gen, values, makemap, joinfmt):
> @@ -43,8 +44,11 @@ class _hybrid(object):
>           return x in self._values
>       def __len__(self):
>           return len(self._values)
> +    def __iter__(self):
> +        return iter(self._values)
>       def __getattr__(self, name):
> -        if name != 'get':
> +        if name not in ('get', 'items', 'iteritems', 'iterkeys', 'itervalues',
> +                        'keys', 'values'):
>               raise AttributeError(name)
>           return getattr(self._values, name)
>   
> diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> --- a/tests/test-command-template.t
> +++ b/tests/test-command-template.t
> @@ -3392,6 +3392,13 @@ Test get function:
>     hg: parse error: get() expects a dict as first argument
>     [255]
>   
> +Test json filter applied to hybrid object:
> +
> +  $ hg log -r0 -T '{files|json}\n'
> +  ["a"]
> +  $ hg log -r0 -T '{extras|json}\n'
> +  {"branch": "default"}
> +
>   Test localdate(date, tz) function:
>   
>     $ TZ=JST-09 hg log -r0 -T '{date|localdate|isodate}\n'
>
Augie Fackler - April 11, 2017, 3:07 p.m.
On Fri, Apr 07, 2017 at 09:51:19AM +0100, Ryan McElroy wrote:
> On 4/4/17 5:00 PM, Yuya Nishihara wrote:
> ># HG changeset patch
> ># User Yuya Nishihara <yuya@tcha.org>
> ># Date 1491312719 -32400
> >#      Tue Apr 04 22:31:59 2017 +0900
> ># Node ID 236441d523b03a227a0562497cbd060f795d3e13
> ># Parent  a78c9de39862cd089aaddd9536902c7c52dfe1e6
> >templater: make _hybrid provide more list/dict-like methods
>
> Sorry for the slow review. I've looked over this series and it looks like a
> good improvement to me. Being able to filter things like extra or files
> through json is a big win in my opinion!
>
> I've marked as pre-reviewed in patchwork.

Queued, thanks

>
> >
> >So the JSON filter works.
> >
> >diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
> >--- a/mercurial/templatekw.py
> >+++ b/mercurial/templatekw.py
> >@@ -28,6 +28,7 @@ class _hybrid(object):
> >      and to access raw values:
> >      - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}"
> >      - "{get(extras, key)}"
> >+    - "{files|json}"
> >      """
> >      def __init__(self, gen, values, makemap, joinfmt):
> >@@ -43,8 +44,11 @@ class _hybrid(object):
> >          return x in self._values
> >      def __len__(self):
> >          return len(self._values)
> >+    def __iter__(self):
> >+        return iter(self._values)
> >      def __getattr__(self, name):
> >-        if name != 'get':
> >+        if name not in ('get', 'items', 'iteritems', 'iterkeys', 'itervalues',
> >+                        'keys', 'values'):
> >              raise AttributeError(name)
> >          return getattr(self._values, name)
> >diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> >--- a/tests/test-command-template.t
> >+++ b/tests/test-command-template.t
> >@@ -3392,6 +3392,13 @@ Test get function:
> >    hg: parse error: get() expects a dict as first argument
> >    [255]
> >+Test json filter applied to hybrid object:
> >+
> >+  $ hg log -r0 -T '{files|json}\n'
> >+  ["a"]
> >+  $ hg log -r0 -T '{extras|json}\n'
> >+  {"branch": "default"}
> >+
> >  Test localdate(date, tz) function:
> >    $ TZ=JST-09 hg log -r0 -T '{date|localdate|isodate}\n'
> >
>
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -28,6 +28,7 @@  class _hybrid(object):
     and to access raw values:
     - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}"
     - "{get(extras, key)}"
+    - "{files|json}"
     """
 
     def __init__(self, gen, values, makemap, joinfmt):
@@ -43,8 +44,11 @@  class _hybrid(object):
         return x in self._values
     def __len__(self):
         return len(self._values)
+    def __iter__(self):
+        return iter(self._values)
     def __getattr__(self, name):
-        if name != 'get':
+        if name not in ('get', 'items', 'iteritems', 'iterkeys', 'itervalues',
+                        'keys', 'values'):
             raise AttributeError(name)
         return getattr(self._values, name)
 
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -3392,6 +3392,13 @@  Test get function:
   hg: parse error: get() expects a dict as first argument
   [255]
 
+Test json filter applied to hybrid object:
+
+  $ hg log -r0 -T '{files|json}\n'
+  ["a"]
+  $ hg log -r0 -T '{extras|json}\n'
+  {"branch": "default"}
+
 Test localdate(date, tz) function:
 
   $ TZ=JST-09 hg log -r0 -T '{date|localdate|isodate}\n'