changeset 27252:dccbebcff075

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].
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 05 Dec 2015 20:20:57 -0800
parents d9bfe6289acf
children f43988e5954c
files mercurial/ui.py
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/ui.py	Fri Dec 04 17:46:56 2015 -0800
+++ b/mercurial/ui.py	Sat Dec 05 20:20:57 2015 -0800
@@ -280,6 +280,39 @@
                                "%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)