Patchwork [4,of,7,path-configs] ui: implement ui.sections()

login
register
mail settings
Submitter Gregory Szorc
Date Feb. 8, 2015, 1:13 a.m.
Message ID <e93fb90e9b8787940214.1423358020@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/7765/
State Changes Requested
Headers show

Comments

Gregory Szorc - Feb. 8, 2015, 1:13 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1423345652 28800
#      Sat Feb 07 13:47:32 2015 -0800
# Node ID e93fb90e9b87879402147a2fdff2a937087639ca
# Parent  0f2bea2665040e626a7b6d039b698def3bf58dd4
ui: implement ui.sections()

An upcoming patch will introduce a configuration mechanism allowing
configurations for different but related items (paths) to be stored
in different config sections. There is currently no public API to
iterate over sections or for all sections within a logical group
of sections. So, we introduce that API.

This API could arguably go on the lower-level config class. This
should be scrutinized during review.
Sean Farley - Feb. 8, 2015, 9:32 p.m.
Gregory Szorc writes:

> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1423345652 28800
> #      Sat Feb 07 13:47:32 2015 -0800
> # Node ID e93fb90e9b87879402147a2fdff2a937087639ca
> # Parent  0f2bea2665040e626a7b6d039b698def3bf58dd4
> ui: implement ui.sections()
>
> An upcoming patch will introduce a configuration mechanism allowing
> configurations for different but related items (paths) to be stored
> in different config sections. There is currently no public API to
> iterate over sections or for all sections within a logical group
> of sections. So, we introduce that API.
>
> This API could arguably go on the lower-level config class. This
> should be scrutinized during review.

Doing my best impression of mpm: I think patches 1-4 should be one
series. Maybe even 1-3.
Augie Fackler - Feb. 9, 2015, 11:09 p.m.
On Sat, Feb 07, 2015 at 05:13:40PM -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1423345652 28800
> #      Sat Feb 07 13:47:32 2015 -0800
> # Node ID e93fb90e9b87879402147a2fdff2a937087639ca
> # Parent  0f2bea2665040e626a7b6d039b698def3bf58dd4
> ui: implement ui.sections()
>
> An upcoming patch will introduce a configuration mechanism allowing
> configurations for different but related items (paths) to be stored
> in different config sections. There is currently no public API to
> iterate over sections or for all sections within a logical group
> of sections. So, we introduce that API.
>
> This API could arguably go on the lower-level config class. This
> should be scrutinized during review.

I don't think it matters overmuch.

>
> diff --git a/mercurial/ui.py b/mercurial/ui.py
> --- a/mercurial/ui.py
> +++ b/mercurial/ui.py
> @@ -230,8 +230,45 @@ class ui(object):
>
>      def _data(self, untrusted):
>          return untrusted and self._ucfg or self._tcfg
>
> +    def sections(self, group=None, untrusted=False):
> +        """Obtain sections from the config with their data.
> +
> +        If ``group`` is defined, only sections belonging to the specified
> +        group will be returned. A section group consists of config sections
> +        where the names are of the from ``[group x]`` or ``[group "x"]``.

I'm not sure I like defining groups using a space, partially because
then setting them on the command line is annoying.

> +
> +        A dict contains sections will be returned. Keys will be the section
> +        name and values will be a dict of config items. If ``group`` is
> +        defined, the keys will be the unique name with the leading group
> +        identifier and any quoting stripped.
> +
> +        >>> u = ui();
> +        >>> u.setconfig('foo', 'test', 't')
> +        >>> u.setconfig('grp x', 'xtest', 'x')
> +        >>> u.setconfig('grp y', 'ytest', 'y')
> +        >>> u.setconfig('grp "z"', 'ztest', 'z')
> +        >>> sorted(u.sections().keys())
> +        ['defaults', 'foo', 'grp "z"', 'grp x', 'grp y', 'largefiles', 'ui']
> +        >>> sorted(u.sections('grp').items())
> +        [('x', {'xtest': 'x'}), ('y', {'ytest': 'y'}), ('z', {'ztest': 'z'})]
> +        """
> +        r = {}
> +        data = self._data(untrusted)
> +        for section in data.sections():
> +            if not group:
> +                r[section] = data.items(section)
> +                continue
> +
> +            if not section.startswith('%s ' % group):
> +                continue
> +
> +            name = section[len(group) + 1:].strip('"\' \t')
> +            r[name] = dict(data.items(section))
> +
> +        return r
> +
>      def configsource(self, section, name, untrusted=False):
>          return self._data(untrusted).source(section, name) or 'none'
>
>      def config(self, section, name, default=None, untrusted=False):
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
Sean Farley - Feb. 9, 2015, 11:21 p.m.
Augie Fackler writes:

> On Sat, Feb 07, 2015 at 05:13:40PM -0800, Gregory Szorc wrote:
>> # HG changeset patch
>> # User Gregory Szorc <gregory.szorc@gmail.com>
>> # Date 1423345652 28800
>> #      Sat Feb 07 13:47:32 2015 -0800
>> # Node ID e93fb90e9b87879402147a2fdff2a937087639ca
>> # Parent  0f2bea2665040e626a7b6d039b698def3bf58dd4
>> ui: implement ui.sections()
>>
>> An upcoming patch will introduce a configuration mechanism allowing
>> configurations for different but related items (paths) to be stored
>> in different config sections. There is currently no public API to
>> iterate over sections or for all sections within a logical group
>> of sections. So, we introduce that API.
>>
>> This API could arguably go on the lower-level config class. This
>> should be scrutinized during review.
>
> I don't think it matters overmuch.
>
>>
>> diff --git a/mercurial/ui.py b/mercurial/ui.py
>> --- a/mercurial/ui.py
>> +++ b/mercurial/ui.py
>> @@ -230,8 +230,45 @@ class ui(object):
>>
>>      def _data(self, untrusted):
>>          return untrusted and self._ucfg or self._tcfg
>>
>> +    def sections(self, group=None, untrusted=False):
>> +        """Obtain sections from the config with their data.
>> +
>> +        If ``group`` is defined, only sections belonging to the specified
>> +        group will be returned. A section group consists of config sections
>> +        where the names are of the from ``[group x]`` or ``[group "x"]``.
>
> I'm not sure I like defining groups using a space, partially because
> then setting them on the command line is annoying.

That is something I forgot about. Good point. Down with spaces!
Anton Shestakov - Feb. 10, 2015, 8:41 a.m.
10.02.2015, 07:10, "Augie Fackler" <raf@durin42.com>:
> On Sat, Feb 07, 2015 at 05:13:40PM -0800, Gregory Szorc wrote:
>>  +    def sections(self, group=None, untrusted=False):
>>  +        """Obtain sections from the config with their data.
>>  +
>>  +        If ``group`` is defined, only sections belonging to the specified
>>  +        group will be returned. A section group consists of config sections
>>  +        where the names are of the from ``[group x]`` or ``[group "x"]``.
>
> I'm not sure I like defining groups using a space, partially because
> then setting them on the command line is annoying.

I'd like to add that, as a user I'd expect to see groups use : as a delimiter,
and using space for that is somewhat surprising. Any reason : can't be used
here?

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -230,8 +230,45 @@  class ui(object):
 
     def _data(self, untrusted):
         return untrusted and self._ucfg or self._tcfg
 
+    def sections(self, group=None, untrusted=False):
+        """Obtain sections from the config with their data.
+
+        If ``group`` is defined, only sections belonging to the specified
+        group will be returned. A section group consists of config sections
+        where the names are of the from ``[group x]`` or ``[group "x"]``.
+
+        A dict contains sections will be returned. Keys will be the section
+        name and values will be a dict of config items. If ``group`` is
+        defined, the keys will be the unique name with the leading group
+        identifier and any quoting stripped.
+
+        >>> u = ui();
+        >>> u.setconfig('foo', 'test', 't')
+        >>> u.setconfig('grp x', 'xtest', 'x')
+        >>> u.setconfig('grp y', 'ytest', 'y')
+        >>> u.setconfig('grp "z"', 'ztest', 'z')
+        >>> sorted(u.sections().keys())
+        ['defaults', 'foo', 'grp "z"', 'grp x', 'grp y', 'largefiles', 'ui']
+        >>> sorted(u.sections('grp').items())
+        [('x', {'xtest': 'x'}), ('y', {'ytest': 'y'}), ('z', {'ztest': 'z'})]
+        """
+        r = {}
+        data = self._data(untrusted)
+        for section in data.sections():
+            if not group:
+                r[section] = data.items(section)
+                continue
+
+            if not section.startswith('%s ' % group):
+                continue
+
+            name = section[len(group) + 1:].strip('"\' \t')
+            r[name] = dict(data.items(section))
+
+        return r
+
     def configsource(self, section, name, untrusted=False):
         return self._data(untrusted).source(section, name) or 'none'
 
     def config(self, section, name, default=None, untrusted=False):