annotate mercurial/configitems.py @ 52281:854e2b9bca57 stable

Added signature for changeset b267c5764cc6
author Raphaël Gomès <rgomes@octobus.net>
date Wed, 20 Nov 2024 15:38:57 +0100
parents f4733654f144
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 51292
diff changeset
8 from __future__ import annotations
32983
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
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
11 import re
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
12
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
13 from .utils import resourceutil
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
14
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
15 from . import (
34239
344fd1fe237b configitems: register the 'web.encoding' config
Boris Feld <boris.feld@octobus.net>
parents: 34238
diff changeset
16 encoding,
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
17 error,
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
18 )
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
19
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
20 try:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
21 import tomllib # pytype: disable=import-error
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
22
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
23 tomllib.load # trigger lazy import
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
24 except ModuleNotFoundError:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
25 # Python <3.11 compat
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
26 from .thirdparty import tomli as tomllib
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
28
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
29 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
30 """update config item known to the ui with the extension ones"""
35808
178aacdc25db configitems: traverse sections deterministically
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35763
diff changeset
31 for section, items in sorted(configtable.items()):
34768
2b954c9c5395 configitems: fix registration of extensions config
Boris Feld <boris.feld@octobus.net>
parents: 34759
diff changeset
32 knownitems = ui._knownconfig.setdefault(section, itemregister())
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
33 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
34 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
35 for key in sorted(knownkeys & newkeys):
50758
5d092194ac37 configitems: fix typo in devel warning about extension overrides
Raphaël Gomès <rgomes@octobus.net>
parents: 50524
diff changeset
36 msg = b"extension '%s' overwrites config item '%s.%s'"
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
37 msg %= (extname, section, key)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
38 ui.develwarn(msg, config=b'warn-config')
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
39
bf1292c057ef configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33132
diff changeset
40 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
41
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
42
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
43 class configitem:
32983
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
44 """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
45
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
46 :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
47 :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
48 :default: default value for this item,
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
49 :alias: optional list of tuples as alternatives,
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
50 :generic: this is a generic definition, match name using regular expression.
32983
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
51 """
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
52
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
53 def __init__(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
54 self,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
55 section,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
56 name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
57 default=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
58 alias=(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
59 generic=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
60 priority=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
61 experimental=False,
50760
b584dae08774 configitems: add `documentation` field
Raphaël Gomès <rgomes@octobus.net>
parents: 50759
diff changeset
62 documentation="",
50765
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50762
diff changeset
63 in_core_extension=None,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
64 ):
32983
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
65 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
66 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
67 self.default = default
50760
b584dae08774 configitems: add `documentation` field
Raphaël Gomès <rgomes@octobus.net>
parents: 50759
diff changeset
68 self.documentation = documentation
33329
e714159860fd configitems: add alias support in config
David Demelier <demelier.david@gmail.com>
parents: 33250
diff changeset
69 self.alias = list(alias)
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
70 self.generic = generic
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
71 self.priority = priority
42760
9f2189b6bf2a config: add experimental argument to the config registrar
Navaneeth Suresh <navaneeths1998@gmail.com>
parents: 42742
diff changeset
72 self.experimental = experimental
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
73 self._re = None
50765
7f8f6fe13fa9 configitems: move blackbox's config items to the new configitems.toml
Raphaël Gomès <rgomes@octobus.net>
parents: 50762
diff changeset
74 self.in_core_extension = in_core_extension
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
75 if generic:
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
76 self._re = re.compile(self.name)
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
77
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
78
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
79 class itemregister(dict):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
80 """A specialized dictionary that can handle wild-card selection"""
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
81
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
82 def __init__(self):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
83 super(itemregister, self).__init__()
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
84 self._generics = set()
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
85
51292
9c5bd485fbb6 pytype: ignore some signature mismatch in configitems
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50765
diff changeset
86 def update(self, other): # pytype: disable=signature-mismatch
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
87 super(itemregister, self).update(other)
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
88 self._generics.update(other._generics)
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
89
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
90 def __setitem__(self, key, item):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
91 super(itemregister, self).__setitem__(key, item)
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
92 if item.generic:
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
93 self._generics.add(item)
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
94
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
95 def get(self, key):
34874
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
96 baseitem = super(itemregister, self).get(key)
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
97 if baseitem is not None and not baseitem.generic:
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
98 return baseitem
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
99
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
100 # search for a matching generic item
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
101 generics = sorted(self._generics, key=(lambda x: (x.priority, x.name)))
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
102 for item in generics:
34875
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
103 # we use 'match' instead of 'search' to make the matching simpler
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
104 # for people unfamiliar with regular expression. Having the match
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
105 # rooted to the start of the string will produce less surprising
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
106 # result for user writing simple regex for sub-attribute.
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
107 #
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
108 # For example using "color\..*" match produces an unsurprising
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
109 # result, while using search could suddenly match apparently
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
110 # unrelated configuration that happens to contains "color."
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
111 # anywhere. This is a tradeoff where we favor requiring ".*" on
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
112 # some match to avoid the need to prefix most pattern with "^".
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
113 # The "^" seems more error prone.
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
114 if item._re.match(key):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
115 return item
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
116
34874
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
117 return None
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
118
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
119
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
120 def sanitize_item(item):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
121 """Apply the transformations that are encoded on top of the pure data"""
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
122
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
123 # Set the special defaults
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
124 default_type_key = "default-type"
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
125 default_type = item.pop(default_type_key, None)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
126 if default_type == "dynamic":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
127 item["default"] = dynamicdefault
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
128 elif default_type == "list_type":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
129 item["default"] = list
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
130 elif default_type == "lambda":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
131 assert isinstance(item["default"], list)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
132 default = [e.encode() for e in item["default"]]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
133 item["default"] = lambda: default
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
134 elif default_type == "lazy_module":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
135 item["default"] = lambda: encoding.encoding
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
136 else:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
137 if default_type is not None:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
138 msg = "invalid default config type %r for '%s.%s'"
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
139 msg %= (default_type, item["section"], item["name"])
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
140 raise error.ProgrammingError(msg)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
141
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
142 # config expects bytes
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
143 alias = item.get("alias")
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
144 if alias:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
145 item["alias"] = [(k.encode(), v.encode()) for (k, v) in alias]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
146 if isinstance(item.get("default"), str):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
147 item["default"] = item["default"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
148 item["section"] = item["section"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
149 item["name"] = item["name"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
150
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
151
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
152 def read_configitems_file():
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
153 """Returns the deserialized TOML structure from the configitems file"""
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
154 with resourceutil.open_resource(b"mercurial", b"configitems.toml") as fp:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
155 return tomllib.load(fp)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
156
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
157
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
158 def configitems_from_toml(items):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
159 """Register the configitems from the *deserialized* toml file"""
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
160 for item in items["items"]:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
161 sanitize_item(item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
162 coreconfigitem(**item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
163
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
164 templates = items["templates"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
165
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
166 for application in items["template-applications"]:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
167 template_items = templates[application["template"]]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
168
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
169 for template_item in template_items:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
170 item = template_item.copy()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
171 prefix = application.get("prefix", "")
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
172 item["section"] = application["section"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
173 if prefix:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
174 item["name"] = f'{prefix}.{item["suffix"]}'
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
175 else:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
176 item["name"] = item["suffix"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
177
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
178 sanitize_item(item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
179 item.pop("suffix", None)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
180 coreconfigitem(**item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
181
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
182
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
183 def import_configitems_from_file():
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
184 as_toml = read_configitems_file()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
185 configitems_from_toml(as_toml)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
186
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
187
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
188 coreitems = {}
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
189
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
190
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
191 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
192 item = configitem(*args, **kwargs)
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
193 section = configtable.setdefault(item.section, itemregister())
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
194 if item.name in section:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
195 msg = b"duplicated config item registration for '%s.%s'"
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
196 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
197 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
198
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
199
33471
d74141ccfd8b configitems: handle case were the default value is not static
Boris Feld <boris.feld@octobus.net>
parents: 33329
diff changeset
200 # special value for case where the default is derived from other values
d74141ccfd8b configitems: handle case were the default value is not static
Boris Feld <boris.feld@octobus.net>
parents: 33329
diff changeset
201 dynamicdefault = object()
d74141ccfd8b configitems: handle case were the default value is not static
Boris Feld <boris.feld@octobus.net>
parents: 33329
diff changeset
202
32986
2529e2ae9f4c configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32984
diff changeset
203 # Registering actual config items
2529e2ae9f4c configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32984
diff changeset
204
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
205
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
206 def getitemregister(configtable):
34917
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
207 f = functools.partial(_register, configtable)
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
208 # export pseudo enum as configitem.*
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
209 f.dynamicdefault = dynamicdefault
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
210 return f
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
211
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
212
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
213 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
214
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
215 import_configitems_from_file()