Patchwork D2622: ui: adding a generic method to read config items with an arbitrary type

login
register
mail settings
Submitter phabricator
Date March 3, 2018, 11:54 p.m.
Message ID <differential-rev-PHID-DREV-aejjpa6cbmkiavdimnra-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/28883/
State New
Headers show

Comments

phabricator - March 3, 2018, 11:54 p.m.
rdamazio created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This can be used for reading many config options of various types where the
  type of each is known in some other structure (e.g. options matching command
  flags).

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2622

AFFECTED FILES
  mercurial/ui.py

CHANGE DETAILS




To: rdamazio, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -545,6 +545,16 @@ 
 
         return main, sub
 
+    def configtyped(self, section, name, itemtype, default=_unset, untrusted=False):
+        """Get a config item as the given type."""
+        if itemtype is type(False) or itemtype is type(None):
+            return self.configbool(section, name, default, untrusted)
+        if itemtype is type(1):
+            return self.configint(section, name, default, untrusted)
+        if itemtype is type([]):
+            return self.configlist(section, name, default, untrusted)
+        return self.config(section, name, default, untrusted)
+
     def configpath(self, section, name, default=_unset, untrusted=False):
         'get a path config item, expanded relative to repo root or config file'
         v = self.config(section, name, default, untrusted)