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

Augie Fackler raf at durin42.com
Mon Feb 9 17:09:15 CST 2015


On Sat, Feb 07, 2015 at 05:13:40PM -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc at 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 at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel


More information about the Mercurial-devel mailing list