author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Wed, 28 Jun 2017 13:25:33 +0200 | |
changeset 33159 | 0224820688ac |
parent 33158 | cb7140e230c8 |
child 33160 | 12aaade1f617 |
permissions | -rw-r--r-- |
32983
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
1 |
# configitems.py - centralized declaration of configuration option |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
2 |
# |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
3 |
# Copyright 2017 Pierre-Yves David <pierre-yves.david@octobus.net> |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
4 |
# |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
7 |
|
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
8 |
from __future__ import absolute_import |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
9 |
|
33131
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
10 |
import functools |
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
11 |
|
32984
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
12 |
from . import ( |
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
13 |
error, |
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
14 |
) |
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
15 |
|
33132
c467d13334ee
configitems: add an official API for extensions to register config item
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33131
diff
changeset
|
16 |
def loadconfigtable(ui, extname, configtable): |
c467d13334ee
configitems: add an official API for extensions to register config item
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33131
diff
changeset
|
17 |
"""update config item known to the ui with the extension ones""" |
c467d13334ee
configitems: add an official API for extensions to register config item
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33131
diff
changeset
|
18 |
for section, items in configtable.items(): |
33133
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
19 |
knownitems = ui._knownconfig.setdefault(section, {}) |
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
20 |
knownkeys = set(knownitems) |
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
21 |
newkeys = set(items) |
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
22 |
for key in sorted(knownkeys & newkeys): |
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
23 |
msg = "extension '%s' overwrite config item '%s.%s'" |
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
24 |
msg %= (extname, section, key) |
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
25 |
ui.develwarn(msg, config='warn-config') |
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
26 |
|
bf1292c057ef
configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33132
diff
changeset
|
27 |
knownitems.update(items) |
33132
c467d13334ee
configitems: add an official API for extensions to register config item
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33131
diff
changeset
|
28 |
|
32983
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
29 |
class configitem(object): |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
30 |
"""represent a known config item |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
31 |
|
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
32 |
:section: the official config section where to find this item, |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
33 |
:name: the official name within the section, |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
34 |
:default: default value for this item, |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
35 |
""" |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
36 |
|
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
37 |
def __init__(self, section, name, default=None): |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
38 |
self.section = section |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
39 |
self.name = name |
0d757af1ea67
configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
40 |
self.default = default |
32984
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
41 |
|
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
42 |
coreitems = {} |
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
43 |
|
33131
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
44 |
def _register(configtable, *args, **kwargs): |
32984
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
45 |
item = configitem(*args, **kwargs) |
33131
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
46 |
section = configtable.setdefault(item.section, {}) |
32984
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
47 |
if item.name in section: |
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
48 |
msg = "duplicated config item registration for '%s.%s'" |
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
49 |
raise error.ProgrammingError(msg % (item.section, item.name)) |
6d983e8af49c
configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32983
diff
changeset
|
50 |
section[item.name] = item |
32986
2529e2ae9f4c
configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32984
diff
changeset
|
51 |
|
2529e2ae9f4c
configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32984
diff
changeset
|
52 |
# Registering actual config items |
2529e2ae9f4c
configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32984
diff
changeset
|
53 |
|
33131
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
54 |
def getitemregister(configtable): |
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
55 |
return functools.partial(_register, configtable) |
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
56 |
|
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
57 |
coreconfigitem = getitemregister(coreitems) |
c2ca511c4771
configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33061
diff
changeset
|
58 |
|
33158
cb7140e230c8
config: register the 'devel.all-warnings' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33150
diff
changeset
|
59 |
coreconfigitem('devel', 'all-warnings', |
cb7140e230c8
config: register the 'devel.all-warnings' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33150
diff
changeset
|
60 |
default=False, |
cb7140e230c8
config: register the 'devel.all-warnings' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33150
diff
changeset
|
61 |
) |
33159
0224820688ac
config: register the 'devel.bundle2.debug' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33158
diff
changeset
|
62 |
coreconfigitem('devel', 'bundle2.debug', |
0224820688ac
config: register the 'devel.bundle2.debug' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33158
diff
changeset
|
63 |
default=False, |
0224820688ac
config: register the 'devel.bundle2.debug' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33158
diff
changeset
|
64 |
) |
32990
1d5d7e2b7ab5
configitems: register 'patch.fuzz' as first example for 'configint'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32986
diff
changeset
|
65 |
coreconfigitem('patch', 'fuzz', |
1d5d7e2b7ab5
configitems: register 'patch.fuzz' as first example for 'configint'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32986
diff
changeset
|
66 |
default=2, |
1d5d7e2b7ab5
configitems: register 'patch.fuzz' as first example for 'configint'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32986
diff
changeset
|
67 |
) |
32991
03608e8d09e9
configitems: register 'ui.clonebundleprefers' as example for 'configlist'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32990
diff
changeset
|
68 |
coreconfigitem('ui', 'clonebundleprefers', |
33150
77e666f943a6
configitems: support callable as a default value
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33133
diff
changeset
|
69 |
default=list, |
32991
03608e8d09e9
configitems: register 'ui.clonebundleprefers' as example for 'configlist'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32990
diff
changeset
|
70 |
) |
33061
c41cbe98822c
configitems: register 'ui.interactive'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32991
diff
changeset
|
71 |
coreconfigitem('ui', 'interactive', |
c41cbe98822c
configitems: register 'ui.interactive'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32991
diff
changeset
|
72 |
default=None, |
c41cbe98822c
configitems: register 'ui.interactive'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32991
diff
changeset
|
73 |
) |
32986
2529e2ae9f4c
configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32984
diff
changeset
|
74 |
coreconfigitem('ui', 'quiet', |
2529e2ae9f4c
configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32984
diff
changeset
|
75 |
default=False, |
2529e2ae9f4c
configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32984
diff
changeset
|
76 |
) |