[PATCH 1 of 2 v3] config: add hasconfig method and supporting plumbing

Bryan O'Sullivan bos at serpentine.com
Fri Jan 8 16:50:31 UTC 2016


# HG changeset patch
# User Bryan O'Sullivan <bos at serpentine.com>
# Date 1452224703 28800
#      Thu Jan 07 19:45:03 2016 -0800
# Node ID 8b71747150c066ba5244adf88edff406c30db94c
# Parent  c1a5661327730550ddee47125ef090fbebcaeb11
config: add hasconfig method and supporting plumbing

The ui.config method makes it impossible to distinguish between a
config value that was never supplied and one that is empty.  You
might hope that you could write something like this:

  unspecified = object()
  val = ui.config('section', 'thingy', default=unspecified)

Sadly, this will happily return unspecified regardless of whether no
value was provided or an empty value was specified.

We add the hasconfig method to make it possible to distinguish the
two cases.

diff --git a/mercurial/config.py b/mercurial/config.py
--- a/mercurial/config.py
+++ b/mercurial/config.py
@@ -30,6 +30,8 @@ class config(object):
         return config(self)
     def __contains__(self, section):
         return section in self._data
+    def hasitem(self, section, item):
+        return item in self._data.get(section, {})
     def __getitem__(self, section):
         return self._data.get(section, {})
     def __iter__(self):
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -281,6 +281,9 @@ class ui(object):
                                "%s.%s = %s\n" % (section, n, uvalue))
         return value
 
+    def hasconfig(self, section, name, untrusted=False):
+        return self._data(untrusted).hasitem(section, name)
+
     def configsuboptions(self, section, name, default=None, untrusted=False):
         """Get a config option and all sub-options.
 


More information about the Mercurial-devel mailing list