-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfield_group.py
56 lines (43 loc) · 1.75 KB
/
field_group.py
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
# SPDX-FileCopyrightText: © 2022-2024 Wojciech Trybus <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
from typing import TypeVar, Callable, Iterator
from .field import Field
T = TypeVar('T')
class FieldGroup:
"""
Representation of section in fields in kritarc file.
All fields in the group should be created using `field()` method.
It simplifies the field creation by auto-completing the group name.
FieldGroup holds and aggregates fields created with it.
Allows to reset all the fields at once, and register a callback to
all its fields: both existing and future ones.
"""
def __init__(self, name: str) -> None:
self.name = name
self._fields: list[Field] = []
self._callbacks: list[Callable[[], None]] = []
def field(
self,
name: str,
default: T,
parser_type: type | None = None,
local: bool = False,
) -> Field[T]:
"""Create and return a new field in the group."""
field = Field(self.name, name, default, parser_type, local)
self._fields.append(field)
for callback in self._callbacks:
field.register_callback(callback)
return field
def reset_default(self) -> None:
"""Reset values of all fields stored in this group."""
for field in self._fields:
field.reset_default()
def register_callback(self, callback: Callable[[], None]) -> None:
"""Register a callback on every past and future field in group."""
self._callbacks.append(callback)
for field in self._fields:
field.register_callback(callback)
def __iter__(self) -> Iterator[Field]:
"""Iterate over all fields in the group."""
return iter(self._fields)