# HG changeset patch # User Pierre-Yves David # Date 1497700100 -7200 # Node ID c467d13334ee0829f32933a85a8a7ed21a32d436 # Parent c2ca511c4771e6dfdb660f0eb62fbe98bbec187e 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). diff -r c2ca511c4771 -r c467d13334ee mercurial/configitems.py --- 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 diff -r c2ca511c4771 -r c467d13334ee mercurial/extensions.py --- 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'), diff -r c2ca511c4771 -r c467d13334ee mercurial/registrar.py --- 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 diff -r c2ca511c4771 -r c467d13334ee tests/test-extension.t --- 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")