configitems: add an official API for extensions to register config item
authorPierre-Yves David <pierre-yves.david@octobus.net>
Sat, 17 Jun 2017 13:48:20 +0200
changeset 33132 c467d13334ee
parent 33131 c2ca511c4771
child 33133 bf1292c057ef
configitems: add an official API for extensions to register config item Extensions can have a 'configtable' mapping and use 'registrar.configitem(table)' to retrieve the registration function. This behave in the same way as the other way for extensions to register new items (commands, colors, etc).
mercurial/configitems.py
mercurial/extensions.py
mercurial/registrar.py
tests/test-extension.t
--- a/mercurial/configitems.py	Sat Jun 17 13:38:53 2017 +0200
+++ b/mercurial/configitems.py	Sat Jun 17 13:48:20 2017 +0200
@@ -13,6 +13,11 @@
     error,
 )
 
+def loadconfigtable(ui, extname, configtable):
+    """update config item known to the ui with the extension ones"""
+    for section, items in configtable.items():
+        ui._knownconfig.setdefault(section, {}).update(items)
+
 class configitem(object):
     """represent a known config item
 
--- a/mercurial/extensions.py	Sat Jun 17 13:38:53 2017 +0200
+++ b/mercurial/extensions.py	Sat Jun 17 13:48:20 2017 +0200
@@ -18,6 +18,7 @@
 
 from . import (
     cmdutil,
+    configitems,
     encoding,
     error,
     pycompat,
@@ -263,6 +264,7 @@
     extraloaders = [
         ('cmdtable', commands, 'loadcmdtable'),
         ('colortable', color, 'loadcolortable'),
+        ('configtable', configitems, 'loadconfigtable'),
         ('filesetpredicate', fileset, 'loadpredicate'),
         ('revsetpredicate', revset, 'loadpredicate'),
         ('templatefilter', templatefilters, 'loadfilter'),
--- a/mercurial/registrar.py	Sat Jun 17 13:38:53 2017 +0200
+++ b/mercurial/registrar.py	Sat Jun 17 13:48:20 2017 +0200
@@ -8,11 +8,19 @@
 from __future__ import absolute_import
 
 from . import (
+    configitems,
     error,
     pycompat,
     util,
 )
 
+# unlike the other registered items, config options are neither functions or
+# classes. Registering the option is just small function call.
+#
+# We still add the official API to the registrar module for consistency with
+# the other items extensions want might to register.
+configitem = configitems.getitemregister
+
 class _funcregistrarbase(object):
     """Base of decorator to register a function for specific purpose
 
--- a/tests/test-extension.t	Sat Jun 17 13:38:53 2017 +0200
+++ b/tests/test-extension.t	Sat Jun 17 13:48:20 2017 +0200
@@ -5,6 +5,9 @@
   > from mercurial import commands, registrar
   > cmdtable = {}
   > command = registrar.command(cmdtable)
+  > configtable = {}
+  > configitem = registrar.configitem(configtable)
+  > configitem('tests', 'foo', default="Foo")
   > def uisetup(ui):
   >     ui.write("uisetup called\\n")
   >     ui.flush()
@@ -14,7 +17,9 @@
   >     ui.flush()
   > @command(b'foo', [], 'hg foo')
   > def foo(ui, *args, **kwargs):
-  >     ui.write("Foo\\n")
+  >     foo = ui.config('tests', 'foo')
+  >     ui.write(foo)
+  >     ui.write("\\n")
   > @command(b'bar', [], 'hg bar', norepo=True)
   > def bar(ui, *args, **kwargs):
   >     ui.write("Bar\\n")