[PATCH 2 of 6] configitems: add an official API for extensions to register config item
Pierre-Yves David
pierre-yves.david at ens-lyon.org
Thu Jun 22 07:34:31 UTC 2017
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at octobus.net>
# Date 1497700100 -7200
# Sat Jun 17 13:48:20 2017 +0200
# Node ID 630aa32cf23b88fa9ab2d7ffac94ecea1307b4c0
# Parent 1b9be14a52e0789ffbaae58252f0b753fe213f86
# EXP-Topic config.register
# Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
# hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r 630aa32cf23b
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 --git a/mercurial/configitems.py b/mercurial/configitems.py
--- a/mercurial/configitems.py
+++ b/mercurial/configitems.py
@@ -13,6 +13,11 @@ from . import (
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 --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -25,6 +25,7 @@ from . import (
cmdutil,
color,
commands,
+ configitems,
demandimport,
encoding,
error,
@@ -736,6 +737,7 @@ def _checkshellalias(lui, ui, args):
extraloaders = [
('cmdtable', commands, 'loadcmdtable'),
('colortable', color, 'loadcolortable'),
+ ('configtable', configitems, 'loadconfigtable'),
('filesetpredicate', fileset, 'loadpredicate'),
('revsetpredicate', revset, 'loadpredicate'),
('templatefilter', templatefilters, 'loadfilter'),
diff --git a/mercurial/registrar.py b/mercurial/registrar.py
--- a/mercurial/registrar.py
+++ b/mercurial/registrar.py
@@ -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 --git a/tests/test-extension.t b/tests/test-extension.t
--- a/tests/test-extension.t
+++ b/tests/test-extension.t
@@ -5,6 +5,9 @@ Test basic extension support
> 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 @@ Test basic extension support
> ui.flush()
> @command('foo', [], 'hg foo')
> def foo(ui, *args, **kwargs):
- > ui.write("Foo\\n")
+ > foo = ui.config('tests', 'foo')
+ > ui.write(foo)
+ > ui.write("\\n")
> @command('bar', [], 'hg bar', norepo=True)
> def bar(ui, *args, **kwargs):
> ui.write("Bar\\n")
More information about the Mercurial-devel
mailing list