annotate mercurial/configitems.py @ 50801:c51b178b0b7e

configitems: declare items in a TOML file Mercurial ships with Rust code that also needs to read from the config. Having a way of presenting `configitems` to both Python and Rust is needed to prevent duplication, drift, and have the appropriate devel warnings. Abstracting away from Python means choosing a config format. No single format is perfect, and I have yet to come across a developer that doesn't hate all of them in some way. Since we have a strict no-dependencies policy for Mercurial, we either need to use whatever comes with Python, vendor a library, or implement a custom format ourselves. Python stdlib means using JSON, which doesn't support comments and isn't great for humans, or `configparser` which is an obscure, untyped format that nobody uses and doesn't have a commonplace Rust parser. Implementing a custom format is error-prone, tedious and subject to the same issues as picking an existing format. Vendoring opens us to the vast array of common config formats. The ones being picked for most modern software are YAML and TOML. YAML is older and common in the Python community, but TOML is much simpler and less error-prone. I would much rather be responsible for the <1000 lines of `tomli`, on top of TOML being the choice of the Rust community, with robust crates for reading it. The structure of `configitems.toml` is explained inline.
author Raphaël Gomès <rgomes@octobus.net>
date Mon, 23 Jan 2023 18:08:11 +0100
parents b584dae08774
children 7f8f6fe13fa9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33001
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
33138
c2ca511c4771 configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33073
diff changeset
9 import functools
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
10 import re
33138
c2ca511c4771 configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33073
diff changeset
11
50801
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
12 from .utils import resourceutil
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
13
33002
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
14 from . import (
34246
344fd1fe237b configitems: register the 'web.encoding' config
Boris Feld <boris.feld@octobus.net>
parents: 34245
diff changeset
15 encoding,
33002
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
16 error,
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
17 )
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
18
50801
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
19 try:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
20 import tomllib # pytype: disable=import-error
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
21
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
22 tomllib.load # trigger lazy import
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
23 except ModuleNotFoundError:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
24 # Python <3.11 compat
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
25 from .thirdparty import tomli as tomllib
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
26
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
27
33139
c467d13334ee configitems: add an official API for extensions to register config item
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33138
diff changeset
28 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: 33138
diff changeset
29 """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
30 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
31 knownitems = ui._knownconfig.setdefault(section, itemregister())
33140
bf1292c057ef configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33139
diff changeset
32 knownkeys = set(knownitems)
bf1292c057ef configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33139
diff changeset
33 newkeys = set(items)
bf1292c057ef configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33139
diff changeset
34 for key in sorted(knownkeys & newkeys):
50797
5d092194ac37 configitems: fix typo in devel warning about extension overrides
Raphaël Gomès <rgomes@octobus.net>
parents: 50558
diff changeset
35 msg = b"extension '%s' overwrites config item '%s.%s'"
33140
bf1292c057ef configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33139
diff changeset
36 msg %= (extname, section, key)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
37 ui.develwarn(msg, config=b'warn-config')
33140
bf1292c057ef configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33139
diff changeset
38
bf1292c057ef configitems: add a devel warning for extensions items overiding core one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33139
diff changeset
39 knownitems.update(items)
33139
c467d13334ee configitems: add an official API for extensions to register config item
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33138
diff changeset
40
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
41
49037
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48966
diff changeset
42 class configitem:
33001
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
43 """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
44
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
45 :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
46 :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
47 :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
48 :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
49 :generic: this is a generic definition, match name using regular expression.
33001
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
50 """
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
51
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
52 def __init__(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
53 self,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
54 section,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
55 name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
56 default=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
57 alias=(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
58 generic=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
59 priority=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
60 experimental=False,
50799
b584dae08774 configitems: add `documentation` field
Raphaël Gomès <rgomes@octobus.net>
parents: 50798
diff changeset
61 documentation="",
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
62 ):
33001
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
63 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
64 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
65 self.default = default
50799
b584dae08774 configitems: add `documentation` field
Raphaël Gomès <rgomes@octobus.net>
parents: 50798
diff changeset
66 self.documentation = documentation
33329
e714159860fd configitems: add alias support in config
David Demelier <demelier.david@gmail.com>
parents: 33250
diff changeset
67 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
68 self.generic = generic
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
69 self.priority = priority
42776
9f2189b6bf2a config: add experimental argument to the config registrar
Navaneeth Suresh <navaneeths1998@gmail.com>
parents: 42758
diff changeset
70 self.experimental = experimental
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
71 self._re = None
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
72 if generic:
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
73 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
74
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
75
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
76 class itemregister(dict):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
77 """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
78
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
79 def __init__(self):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
80 super(itemregister, self).__init__()
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
81 self._generics = set()
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
82
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
83 def update(self, other):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
84 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
85 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
86
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
87 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
88 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
89 if item.generic:
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
90 self._generics.add(item)
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
91
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
92 def get(self, key):
34874
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
93 baseitem = super(itemregister, self).get(key)
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
94 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
95 return baseitem
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
96
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
97 # 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
98 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
99 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
100 # 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
101 # 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
102 # 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
103 # 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
104 #
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
105 # 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
106 # 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
107 # 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
108 # 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
109 # 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
110 # 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
111 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
112 return item
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
113
34874
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
114 return None
33002
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
115
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
116
50801
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
117 def sanitize_item(item):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
118 """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: 50799
diff changeset
119
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
120 # Set the special defaults
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
121 default_type_key = "default-type"
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
122 default_type = item.pop(default_type_key, None)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
123 if default_type == "dynamic":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
124 item["default"] = dynamicdefault
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
125 elif default_type == "list_type":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
126 item["default"] = list
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
127 elif default_type == "lambda":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
128 assert isinstance(item["default"], list)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
129 default = [e.encode() for e in item["default"]]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
130 item["default"] = lambda: default
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
131 elif default_type == "lazy_module":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
132 item["default"] = lambda: encoding.encoding
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
133 else:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
134 if default_type is not None:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
135 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: 50799
diff changeset
136 msg %= (default_type, item["section"], item["name"])
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
137 raise error.ProgrammingError(msg)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
138
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
139 # config expects bytes
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
140 alias = item.get("alias")
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
141 if alias:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
142 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: 50799
diff changeset
143 if isinstance(item.get("default"), str):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
144 item["default"] = item["default"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
145 item["section"] = item["section"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
146 item["name"] = item["name"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
147
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
148
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
149 def read_configitems_file():
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
150 """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: 50799
diff changeset
151 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: 50799
diff changeset
152 return tomllib.load(fp)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
153
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
154
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
155 def configitems_from_toml(items):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
156 """Register the configitems from the *deserialized* toml file"""
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
157 for item in items["items"]:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
158 sanitize_item(item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
159 coreconfigitem(**item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
160
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
161 templates = items["templates"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
162
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
163 for application in items["template-applications"]:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
164 template_items = templates[application["template"]]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
165
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
166 for template_item in template_items:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
167 item = template_item.copy()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
168 prefix = application.get("prefix", "")
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
169 item["section"] = application["section"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
170 if prefix:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
171 item["name"] = f'{prefix}.{item["suffix"]}'
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
172 else:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
173 item["name"] = item["suffix"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
174
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
175 sanitize_item(item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
176 item.pop("suffix", None)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
177 coreconfigitem(**item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
178
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
179
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
180 def import_configitems_from_file():
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
181 as_toml = read_configitems_file()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
182 configitems_from_toml(as_toml)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
183
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
184
33002
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
185 coreitems = {}
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
186
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
187
33138
c2ca511c4771 configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33073
diff changeset
188 def _register(configtable, *args, **kwargs):
33002
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
189 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
190 section = configtable.setdefault(item.section, itemregister())
33002
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
191 if item.name in section:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
192 msg = b"duplicated config item registration for '%s.%s'"
33002
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33001
diff changeset
193 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: 33001
diff changeset
194 section[item.name] = item
33004
2529e2ae9f4c configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33002
diff changeset
195
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
196
33471
d74141ccfd8b configitems: handle case were the default value is not static
Boris Feld <boris.feld@octobus.net>
parents: 33329
diff changeset
197 # 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
198 dynamicdefault = object()
d74141ccfd8b configitems: handle case were the default value is not static
Boris Feld <boris.feld@octobus.net>
parents: 33329
diff changeset
199
33004
2529e2ae9f4c configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33002
diff changeset
200 # Registering actual config items
2529e2ae9f4c configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33002
diff changeset
201
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
202
33138
c2ca511c4771 configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33073
diff changeset
203 def getitemregister(configtable):
34917
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
204 f = functools.partial(_register, configtable)
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
205 # export pseudo enum as configitem.*
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
206 f.dynamicdefault = dynamicdefault
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
207 return f
33138
c2ca511c4771 configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33073
diff changeset
208
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
209
33138
c2ca511c4771 configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33073
diff changeset
210 coreconfigitem = getitemregister(coreitems)
c2ca511c4771 configitems: extract the logic to build a registrar on any configtable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33073
diff changeset
211
50801
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50799
diff changeset
212 import_configitems_from_file()