summaryrefslogtreecommitdiffstats
path: root/SCons/Variables/EnumVariable.py
blob: a576af513e28a39ebf74c6370cf6060b78d529fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# MIT License
#
# Copyright The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Variable type for enumeration Variables.

Enumeration variables allow selection of one from a specified set of values.

Usage example::

    opts = Variables()
    opts.Add(
        EnumVariable(
            'debug',
            help='debug output and symbols',
            default='no',
            allowed_values=('yes', 'no', 'full'),
            map={},
            ignorecase=2,
        )
    )
    env = Environment(variables=opts)
    if env['debug'] == 'full':
        ...
"""

from __future__ import annotations

from typing import Callable

import SCons.Errors

__all__ = ['EnumVariable',]


def _validator(key, val, env, vals) -> None:
    """Validate that val is in vals.

    Usable as the base for :class:`EnumVariable` validators.
    """
    if val not in vals:
        msg = (
            f"Invalid value for enum variable {key!r}: {val!r}. "
            f"Valid values are: {vals}"
        )
        raise SCons.Errors.UserError(msg) from None


# lint: W0622: Redefining built-in 'help' (redefined-builtin)
# lint:  W0622: Redefining built-in 'map' (redefined-builtin)
def EnumVariable(
    key,
    help: str,
    default: str,
    allowed_values: list[str],
    map: dict | None = None,
    ignorecase: int = 0,
) -> tuple[str, str, str, Callable, Callable]:
    """Return a tuple describing an enumaration SCons Variable.

    An Enum Variable is an abstraction that allows choosing one
    value from a provided list of possibilities (*allowed_values*).
    The value of *ignorecase* defines the behavior of the
    validator and converter: if ``0``, the validator/converter are
    case-sensitive; if ``1``, the validator/converter are case-insensitive;
    if ``2``, the validator/converter are case-insensitive and the
    converted value will always be lower-case.

    Arguments:
       key: the name of the variable.
       default: default value, passed directly through to the return tuple.
       help: descriptive part of the help text,
          will have the allowed values automatically appended.
       allowed_values: the values for the choice.
       map: optional dictionary which may be used for converting the
          input value into canonical values (e.g. for aliases).
       ignorecase: defines the behavior of the validator and converter.

    Returns:
       A tuple including an appropriate converter and validator.
       The result is usable as input to :meth:`~SCons.Variables.Variables.Add`.
       and :meth:`~SCons.Variables.Variables.AddVariables`.
    """
    # these are all inner functions so they can access EnumVariable locals.
    def validator_rcase(key, val, env):
        """Case-respecting validator."""
        return _validator(key, val, env, allowed_values)

    def validator_icase(key, val, env):
        """Case-ignoring validator."""
        return _validator(key, val.lower(), env, allowed_values)

    def converter_rcase(val):
        """Case-respecting converter."""
        return map.get(val, val)

    def converter_icase(val):
        """Case-ignoring converter."""
        return map.get(val.lower(), val)

    def converter_lcase(val):
        """Case-lowering converter."""
        return map.get(val.lower(), val).lower()

    if map is None:
        map = {}
    help = f"{help} ({'|'.join(allowed_values)})"

    # define validator
    if ignorecase:
        validator = validator_icase
    else:
        validator = validator_rcase

    # define converter
    if ignorecase == 2:
        converter = converter_lcase
    elif ignorecase == 1:
        converter = converter_icase
    else:
        converter = converter_rcase

    return key, help, default, validator, converter

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: