Patchwork [3,of,6,V2] ui: add hasconfig method to check if a config item is set

login
register
mail settings
Submitter Mathias De Maré
Date April 7, 2015, 7 p.m.
Message ID <29f1d521db03dea5d14a.1428433249@mathias-Latitude-E6540>
Download mbox | patch
Permalink /patch/8545/
State Changes Requested
Headers show

Comments

Mathias De Maré - April 7, 2015, 7 p.m.
# HG changeset patch
# User Mathias De Maré <mathias.demare@gmail.com>
# Date 1428348883 -7200
#      Mon Apr 06 21:34:43 2015 +0200
# Node ID 29f1d521db03dea5d14aa4d3cbd6705e038849d5
# Parent  411bfd6b77b418249d42817816b3a2526863b0b0
ui: add hasconfig method to check if a config item is set
Matt Mackall - April 8, 2015, 7:20 p.m.
On Tue, 2015-04-07 at 21:00 +0200, mathias.demare@gmail.com wrote:
> # HG changeset patch
> # User Mathias De Maré <mathias.demare@gmail.com>
> # Date 1428348883 -7200
> #      Mon Apr 06 21:34:43 2015 +0200
> # Node ID 29f1d521db03dea5d14aa4d3cbd6705e038849d5
> # Parent  411bfd6b77b418249d42817816b3a2526863b0b0
> ui: add hasconfig method to check if a config item is set
> 
> diff -r 411bfd6b77b4 -r 29f1d521db03 mercurial/ui.py
> --- a/mercurial/ui.py	Mon Apr 06 21:32:27 2015 +0200
> +++ b/mercurial/ui.py	Mon Apr 06 21:34:43 2015 +0200
> @@ -456,6 +456,11 @@
>          '''tell whether section exists in config.'''
>          return section in self._data(untrusted)
>  
> +    def hasconfig(self, section, config, untrusted=False):
> +        '''tell whether config item exists in config.'''
> +        return self.has_section(section, untrusted) \
> +                    and config in self._data(untrusted)[section]
> +

A better way to do this is:

if ui.config("foo", "bar", None) != None

Patch

diff -r 411bfd6b77b4 -r 29f1d521db03 mercurial/ui.py
--- a/mercurial/ui.py	Mon Apr 06 21:32:27 2015 +0200
+++ b/mercurial/ui.py	Mon Apr 06 21:34:43 2015 +0200
@@ -456,6 +456,11 @@ 
         '''tell whether section exists in config.'''
         return section in self._data(untrusted)
 
+    def hasconfig(self, section, config, untrusted=False):
+        '''tell whether config item exists in config.'''
+        return self.has_section(section, untrusted) \
+                    and config in self._data(untrusted)[section]
+
     def configitems(self, section, untrusted=False):
         items = self._data(untrusted).items(section)
         if self.debugflag and not untrusted and self._reportuntrusted: