mercurial/configitems.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Sun, 25 Feb 2024 20:40:37 +0100
changeset 51460 cebd96dee99a
parent 51292 9c5bd485fbb6
child 51863 f4733654f144
permissions -rw-r--r--
branchcache: move the filename to a class attribute This prepare the introduction of more variant of cache.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
     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
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
    11
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
    12
from .utils import resourceutil
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
    13
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
    14
from . import (
34239
344fd1fe237b configitems: register the 'web.encoding' config
Boris Feld <boris.feld@octobus.net>
parents: 34238
diff changeset
    15
    encoding,
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
    16
    error,
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
    17
)
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
    18
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
    19
try:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
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: 50760
diff changeset
    21
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
    22
    tomllib.load  # trigger lazy import
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
    23
except ModuleNotFoundError:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
    24
    # Python <3.11 compat
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
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: 50760
diff changeset
    26
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
    27
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
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
    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())
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
    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: 33132
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: 33132
diff changeset
    34
        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
    35
            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
    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')
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
    38
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
        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
    40
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
    41
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    42
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
    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.
32983
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,
50760
b584dae08774 configitems: add `documentation` field
Raphaël Gomès <rgomes@octobus.net>
parents: 50759
diff changeset
    61
        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
    62
        in_core_extension=None,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
    63
    ):
32983
0d757af1ea67 configitems: add a basic class to hold config item information
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    64
        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
    65
        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
    66
        self.default = default
50760
b584dae08774 configitems: add `documentation` field
Raphaël Gomès <rgomes@octobus.net>
parents: 50759
diff changeset
    67
        self.documentation = documentation
33329
e714159860fd configitems: add alias support in config
David Demelier <demelier.david@gmail.com>
parents: 33250
diff changeset
    68
        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
    69
        self.generic = generic
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    70
        self.priority = priority
42760
9f2189b6bf2a config: add experimental argument to the config registrar
Navaneeth Suresh <navaneeths1998@gmail.com>
parents: 42742
diff changeset
    71
        self.experimental = experimental
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    72
        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
    73
        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
    74
        if generic:
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    75
            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
    76
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
    77
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    78
class itemregister(dict):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    79
    """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
    80
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    81
    def __init__(self):
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    82
        super(itemregister, self).__init__()
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    83
        self._generics = set()
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    84
51292
9c5bd485fbb6 pytype: ignore some signature mismatch in configitems
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50765
diff changeset
    85
    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
    86
        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
    87
        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
    88
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    89
    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
    90
        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
    91
        if item.generic:
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    92
            self._generics.add(item)
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    93
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    94
    def get(self, key):
34874
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
    95
        baseitem = super(itemregister, self).get(key)
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
    96
        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
    97
            return baseitem
34662
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    98
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
    99
        # 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
   100
        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
   101
        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
   102
            # 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
   103
            # 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
   104
            # 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
   105
            # 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
   106
            #
4f0d4bc63b8a configitems: document the choice of using 'match' instead of 'search'
Boris Feld <boris.feld@octobus.net>
parents: 34874
diff changeset
   107
            # 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
   108
            # 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
   109
            # 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
   110
            # 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
   111
            # 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
   112
            # 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
   113
            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
   114
                return item
181d913b17e6 configitems: allow for the registration of "generic" config item
Boris Feld <boris.feld@octobus.net>
parents: 34653
diff changeset
   115
34874
e3fbf8e3fef2 configitems: do not directly match generic items
Boris Feld <boris.feld@octobus.net>
parents: 34872
diff changeset
   116
        return None
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
   117
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
   118
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   119
def sanitize_item(item):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   120
    """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
   121
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   122
    # Set the special defaults
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   123
    default_type_key = "default-type"
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   124
    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
   125
    if default_type == "dynamic":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   126
        item["default"] = dynamicdefault
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   127
    elif default_type == "list_type":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   128
        item["default"] = list
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   129
    elif default_type == "lambda":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   130
        assert isinstance(item["default"], list)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   131
        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
   132
        item["default"] = lambda: default
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   133
    elif default_type == "lazy_module":
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   134
        item["default"] = lambda: encoding.encoding
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   135
    else:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   136
        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
   137
            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
   138
            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
   139
            raise error.ProgrammingError(msg)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   140
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   141
    # config expects bytes
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   142
    alias = item.get("alias")
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   143
    if alias:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   144
        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
   145
    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
   146
        item["default"] = item["default"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   147
    item["section"] = item["section"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   148
    item["name"] = item["name"].encode()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   149
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
def read_configitems_file():
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   152
    """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
   153
    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
   154
        return tomllib.load(fp)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   155
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
def configitems_from_toml(items):
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   158
    """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
   159
    for item in items["items"]:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   160
        sanitize_item(item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   161
        coreconfigitem(**item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   162
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   163
    templates = items["templates"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   164
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   165
    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
   166
        template_items = templates[application["template"]]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   167
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   168
        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
   169
            item = template_item.copy()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   170
            prefix = application.get("prefix", "")
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   171
            item["section"] = application["section"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   172
            if prefix:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   173
                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
   174
            else:
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   175
                item["name"] = item["suffix"]
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   176
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   177
            sanitize_item(item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   178
            item.pop("suffix", None)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   179
            coreconfigitem(**item)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   180
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
def import_configitems_from_file():
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   183
    as_toml = read_configitems_file()
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   184
    configitems_from_toml(as_toml)
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   185
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   186
32984
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
   187
coreitems = {}
6d983e8af49c configitems: introduce a central registry for config option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32983
diff changeset
   188
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
   189
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
   190
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
   191
    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
   192
    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
   193
    if item.name in section:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   194
        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
   195
        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
   196
    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
   197
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
   198
33471
d74141ccfd8b configitems: handle case were the default value is not static
Boris Feld <boris.feld@octobus.net>
parents: 33329
diff changeset
   199
# 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
   200
dynamicdefault = object()
d74141ccfd8b configitems: handle case were the default value is not static
Boris Feld <boris.feld@octobus.net>
parents: 33329
diff changeset
   201
32986
2529e2ae9f4c configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32984
diff changeset
   202
# Registering actual config items
2529e2ae9f4c configitems: register 'ui.quiet' as first example
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32984
diff changeset
   203
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
   204
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
   205
def getitemregister(configtable):
34917
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
   206
    f = functools.partial(_register, configtable)
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
   207
    # export pseudo enum as configitem.*
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
   208
    f.dynamicdefault = dynamicdefault
ee9243715c59 registrar: host "dynamicdefault" constant by configitem object
Yuya Nishihara <yuya@tcha.org>
parents: 34916
diff changeset
   209
    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
   210
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43030
diff changeset
   211
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
   212
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
   213
50762
c51b178b0b7e configitems: declare items in a TOML file
Raphaël Gomès <rgomes@octobus.net>
parents: 50760
diff changeset
   214
import_configitems_from_file()