aboutsummaryrefslogtreecommitdiff
path: root/tools/cru-py/cru/config.py
blob: 0f6f0d04c300fdfa17ca95674230d26e53e50ccb (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from __future__ import annotations

from typing import Any, TypeVar, Generic

from ._error import CruException
from .list import CruUniqueKeyList
from .value import (
    INTEGER_VALUE_TYPE,
    TEXT_VALUE_TYPE,
    CruValueTypeError,
    ValueGeneratorBase,
    ValueType,
)

_T = TypeVar("_T")


class CruConfigError(CruException):
    def __init__(self, message: str, item: ConfigItem, *args, **kwargs):
        super().__init__(message, *args, **kwargs)
        self._item = item

    @property
    def item(self) -> ConfigItem:
        return self._item


class ConfigItem(Generic[_T]):
    def __init__(
        self,
        name: str,
        description: str,
        value_type: ValueType[_T],
        value: _T | None = None,
        /,
        default: ValueGeneratorBase[_T] | _T | None = None,
    ) -> None:
        self._name = name
        self._description = description
        self._value_type = value_type
        self._value = value
        self._default = default

    @property
    def name(self) -> str:
        return self._name

    @property
    def description(self) -> str:
        return self._description

    @property
    def value_type(self) -> ValueType[_T]:
        return self._value_type

    @property
    def is_set(self) -> bool:
        return self._value is not None

    @property
    def value(self) -> _T:
        if self._value is None:
            raise CruConfigError(
                "Config value is not set.",
                self,
                user_message=f"Config item {self.name} is not set.",
            )
        return self._value

    @property
    def value_str(self) -> str:
        return self.value_type.convert_value_to_str(self.value)

    def set_value(self, v: _T | str, allow_convert_from_str=False):
        if allow_convert_from_str:
            self._value = self.value_type.check_value_or_try_convert_from_str(v)
        else:
            self._value = self.value_type.check_value(v)

    def reset(self):
        self._value = None

    @property
    def default(self) -> ValueGeneratorBase[_T] | _T | None:
        return self._default

    @property
    def can_generate_default(self) -> bool:
        return self.default is not None

    def generate_default_value(self) -> _T:
        if self.default is None:
            raise CruConfigError(
                "Config item does not support default value generation.", self
            )
        elif isinstance(self.default, ValueGeneratorBase):
            v = self.default.generate()
        else:
            v = self.default
        try:
            self.value_type.check_value(v)
            return v
        except CruValueTypeError as e:
            raise CruConfigError(
                "Config value generator returns an invalid value.", self
            ) from e

    def copy(self) -> "ConfigItem":
        return ConfigItem(
            self.name,
            self.description,
            self.value_type,
            self.value,
            self.default,
        )

    @property
    def description_str(self) -> str:
        return f"{self.name} ({self.value_type.name}): {self.description}"


class Configuration(CruUniqueKeyList[ConfigItem[Any], str]):
    def __init__(self):
        super().__init__(lambda c: c.name)

    def get_set_items(self) -> list[ConfigItem[Any]]:
        return [item for item in self if item.is_set]

    def get_unset_items(self) -> list[ConfigItem[Any]]:
        return [item for item in self if not item.is_set]

    @property
    def all_set(self) -> bool:
        return len(self.get_unset_items()) == 0

    @property
    def all_not_set(self) -> bool:
        return len(self.get_set_items()) == 0

    def add_text_config(
        self,
        name: str,
        description: str,
        value: str | None = None,
        default: ValueGeneratorBase[str] | str | None = None,
    ) -> ConfigItem[str]:
        item = ConfigItem(name, description, TEXT_VALUE_TYPE, value, default)
        self.add(item)
        return item

    def add_int_config(
        self,
        name: str,
        description: str,
        value: int | None = None,
        default: ValueGeneratorBase[int] | int | None = None,
    ) -> ConfigItem[int]:
        item = ConfigItem(name, description, INTEGER_VALUE_TYPE, value, default)
        self.add(item)
        return item

    def set_config_item(
        self,
        name: str,
        value: Any | str,
        allow_convert_from_str=True,
    ) -> None:
        item = self.get(name)
        item.set_value(
            value,
            allow_convert_from_str=allow_convert_from_str,
        )

    def reset_all(self) -> None:
        for item in self:
            item.reset()

    def to_dict(self) -> dict[str, Any]:
        return {item.name: item.value for item in self}

    def to_str_dict(self) -> dict[str, str]:
        return {
            item.name: item.value_type.convert_value_to_str(item.value) for item in self
        }

    def set_value_dict(
        self,
        value_dict: dict[str, Any],
        allow_convert_from_str: bool = False,
    ) -> None:
        for name, value in value_dict.items():
            item = self.get(name)
            item.set_value(
                value,
                allow_convert_from_str=allow_convert_from_str,
            )