Patchwork [1,of,4] configitems: handle case were the default value is not static

login
register
mail settings
Submitter Boris Feld
Date July 13, 2017, 3:05 p.m.
Message ID <afad2f9ae04d35cef516.1499958342@FB>
Download mbox | patch
Permalink /patch/22291/
State Accepted
Headers show

Comments

Boris Feld - July 13, 2017, 3:05 p.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1499895370 -7200
#      Wed Jul 12 23:36:10 2017 +0200
# Node ID afad2f9ae04d35cef5165b07a2987b0e5604924b
# Parent  68e9762a357b19ec7751683e2fb80266c0d260ce
# EXP-Topic config.register.special-case
configitems: handle case were the default value is not static

In some case, the default of one value is derived from other value. We add a
way to register them anyway and an associated devel-warning.

The registration is very naive for the moment. We might be able to have a
better way for registering each of these cases but it could be done later.

Patch

diff -r 68e9762a357b -r afad2f9ae04d mercurial/configitems.py
--- a/mercurial/configitems.py	Mon Jul 10 23:09:52 2017 +0900
+++ b/mercurial/configitems.py	Wed Jul 12 23:36:10 2017 +0200
@@ -51,6 +51,9 @@ 
         raise error.ProgrammingError(msg % (item.section, item.name))
     section[item.name] = item
 
+# special value for case where the default is derived from other values
+dynamicdefault = object()
+
 # Registering actual config items
 
 def getitemregister(configtable):
diff -r 68e9762a357b -r afad2f9ae04d mercurial/ui.py
--- a/mercurial/ui.py	Mon Jul 10 23:09:52 2017 +0900
+++ b/mercurial/ui.py	Wed Jul 12 23:36:10 2017 +0200
@@ -457,11 +457,17 @@ 
         if default is _unset:
             if item is None:
                 value = default
+            elif item.default is configitems.dynamicdefault:
+                value = None
+                msg = "config item requires an explicit default value: '%s.%s'"
+                msg %= (section, name)
+                self.develwarn(msg, 2, 'warn-config-default')
             elif callable(item.default):
                     value = item.default()
             else:
                 value = item.default
-        elif item is not None:
+        elif (item is not None
+              and item.default is not configitems.dynamicdefault):
             msg = ("specifying a default value for a registered "
                    "config item: '%s.%s' '%s'")
             msg %= (section, name, default)
diff -r 68e9762a357b -r afad2f9ae04d tests/test-devel-warnings.t
--- a/tests/test-devel-warnings.t	Mon Jul 10 23:09:52 2017 +0900
+++ b/tests/test-devel-warnings.t	Wed Jul 12 23:36:10 2017 +0200
@@ -200,7 +200,7 @@ 
   $ cat << EOF > ${TESTTMP}/buggyconfig.py
   > """A small extension that tests our developer warnings for config"""
   > 
-  > from mercurial import registrar
+  > from mercurial import registrar, configitems
   > 
   > cmdtable = {}
   > command = registrar.command(cmdtable)
@@ -209,6 +209,7 @@ 
   > configitem = registrar.configitem(configtable)
   > 
   > configitem('test', 'some', default='foo')
+  > configitem('test', 'dynamic', default=configitems.dynamicdefault)
   > # overwrite a core config
   > configitem('ui', 'quiet', default=False)
   > configitem('ui', 'interactive', default=None)
@@ -218,6 +219,8 @@ 
   >     repo.ui.config('ui', 'quiet', False)
   >     repo.ui.config('ui', 'interactive', None)
   >     repo.ui.config('test', 'some', 'foo')
+  >     repo.ui.config('test', 'dynamic', 'some-required-default')
+  >     repo.ui.config('test', 'dynamic')
   > EOF
 
   $ hg --config "extensions.buggyconfig=${TESTTMP}/buggyconfig.py" buggyconfig
@@ -226,5 +229,6 @@ 
   devel-warn: specifying a default value for a registered config item: 'ui.quiet' 'False' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
   devel-warn: specifying a default value for a registered config item: 'ui.interactive' 'None' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
   devel-warn: specifying a default value for a registered config item: 'test.some' 'foo' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
+  devel-warn: config item requires an explicit default value: 'test.dynamic' at: $TESTTMP/buggyconfig.py:* (cmdbuggyconfig) (glob)
 
   $ cd ..