[PATCH 1 of 3] ui: add method to return option and all sub-options

Gregory Szorc gregory.szorc at gmail.com
Sun Dec 6 05:13:14 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1449375657 28800
#      Sat Dec 05 20:20:57 2015 -0800
# Node ID 64c038f64e8b399e04d88dddd167e98506692c8d
# Parent  30a20167ae29e1874163b59a489de0cb1d859565
ui: add method to return option and all sub-options

Apparently ":" has been blessed as a generic separator for
options and sub-options. We formalize this by introducing an API
for obtaining an option and all its sub-options.

This will be used in a subsequent patch for declaring sub-options
for [paths].

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -275,16 +275,49 @@ class ui(object):
         if self.debugflag and not untrusted and self._reportuntrusted:
             for n in alternates:
                 uvalue = self._ucfg.get(section, n)
                 if uvalue is not None and uvalue != value:
                     self.debug("ignoring untrusted configuration option "
                                "%s.%s = %s\n" % (section, n, uvalue))
         return value
 
+    def configsuboptions(self, section, name, default=None, untrusted=False):
+        """Get a config option and all sub-options.
+
+        Some config options have sub-options that are declared with the
+        format "key:opt = value". This method is used to return the main
+        option and all its declared sub-options.
+
+        Returns a 2-tuple of ``(option, sub-options)``, where `sub-options``
+        is a dict of defined sub-options where keys and values are strings.
+        """
+        data = self._data(untrusted)
+        main = data.get(section, name, default)
+        if self.debugflag and not untrusted and self._reportuntrusted:
+            uvalue = self._ucfg.get(section, name)
+            if uvalue is not None and uvalue != main:
+                self.debug('ignoring untrusted configuration option '
+                           '%s.%s = %s\n' % (section, name, uvalue))
+
+        sub = {}
+        prefix = '%s:' % name
+        for k, v in data.items(section):
+            if k.startswith(prefix):
+                sub[k[len(prefix):]] = v
+
+        if self.debugflag and not untrusted and self._reportuntrusted:
+            for k, v in sub.items():
+                uvalue = self._ucfg.get(section, '%s:%s' % (name, k))
+                if uvalue is not None and uvalue != v:
+                    self.debug('ignoring untrusted configuration option '
+                               '%s:%s.%s = %s\n' % (section, name, k, uvalue))
+
+        return main, sub
+
     def configpath(self, section, name, default=None, untrusted=False):
         'get a path config item, expanded relative to repo root or config file'
         v = self.config(section, name, default, untrusted)
         if v is None:
             return None
         if not os.path.isabs(v) or "://" not in v:
             src = self.configsource(section, name, untrusted)
             if ':' in src:


More information about the Mercurial-devel mailing list