|
1 | 1 | # --------------------------------------------------------------------------------------
|
2 |
| -# Copyright (c) 2013-2023, Nucleic Development Team. |
| 2 | +# Copyright (c) 2013-2024, Nucleic Development Team. |
3 | 3 | #
|
4 | 4 | # Distributed under the terms of the Modified BSD License.
|
5 | 5 | #
|
6 | 6 | # The full license is in the file LICENSE, distributed with this software.
|
7 | 7 | # --------------------------------------------------------------------------------------
|
| 8 | +from typing import Tuple as TTuple |
| 9 | + |
8 | 10 | from .catom import DefaultValue, Member, Validate
|
9 | 11 | from .instance import Instance
|
10 | 12 | from .typing_utils import extract_types, is_optional
|
@@ -78,3 +80,79 @@ def clone(self):
|
78 | 80 | else:
|
79 | 81 | clone.item = None
|
80 | 82 | return clone
|
| 83 | + |
| 84 | + |
| 85 | +class FixedTuple(Member): |
| 86 | + """A member which allows tuple values with a fixed number of items. |
| 87 | +
|
| 88 | + Items are always validated and can be of different types. |
| 89 | + Assignment will create a copy of the original tuple before validating the |
| 90 | + items, since validation may change the item values. |
| 91 | +
|
| 92 | + """ |
| 93 | + |
| 94 | + #: Members used to validate each element of the tuple. |
| 95 | + items: TTuple[Member, ...] |
| 96 | + |
| 97 | + __slots__ = ("items",) |
| 98 | + |
| 99 | + def __init__(self, *items, default=None): |
| 100 | + """Initialize a Tuple. |
| 101 | +
|
| 102 | + Parameters |
| 103 | + ---------- |
| 104 | + items : Member, type, or tuple of types |
| 105 | + A member to use for validating the types of items allowed in |
| 106 | + the tuple. This can also be a type object or a tuple of types, |
| 107 | + in which case it will be wrapped with an Instance member. |
| 108 | +
|
| 109 | + default : tuple, optional |
| 110 | + The default tuple of values. |
| 111 | +
|
| 112 | + """ |
| 113 | + mitems = [] |
| 114 | + for i in items: |
| 115 | + if not isinstance(i, Member): |
| 116 | + opt, types = is_optional(extract_types(i)) |
| 117 | + i = Instance(types, optional=opt) |
| 118 | + mitems.append(i) |
| 119 | + |
| 120 | + self.items = mitems |
| 121 | + |
| 122 | + if default is None: |
| 123 | + self.set_default_value_mode(DefaultValue.NonOptional, None) |
| 124 | + else: |
| 125 | + self.set_default_value_mode(DefaultValue.Static, default) |
| 126 | + self.set_validate_mode(Validate.FixedTuple, tuple(mitems)) |
| 127 | + |
| 128 | + def set_name(self, name): |
| 129 | + """Set the name of the member. |
| 130 | +
|
| 131 | + This method ensures that the item member name is also updated. |
| 132 | +
|
| 133 | + """ |
| 134 | + super().set_name(name) |
| 135 | + for i, item in enumerate(self.items): |
| 136 | + item.set_name(name + f"|item_{i}") |
| 137 | + |
| 138 | + def set_index(self, index): |
| 139 | + """Assign the index to this member. |
| 140 | +
|
| 141 | + This method ensures that the item member index is also updated. |
| 142 | +
|
| 143 | + """ |
| 144 | + super().set_index(index) |
| 145 | + for item in self.items: |
| 146 | + item.set_index(index) |
| 147 | + |
| 148 | + def clone(self): |
| 149 | + """Create a clone of the tuple. |
| 150 | +
|
| 151 | + This will clone the internal tuple item if one is in use. |
| 152 | +
|
| 153 | + """ |
| 154 | + clone = super().clone() |
| 155 | + clone.items = items_clone = tuple(i.clone() for i in self.items) |
| 156 | + mode, _ = self.validate_mode |
| 157 | + clone.set_validate_mode(mode, items_clone) |
| 158 | + return clone |
0 commit comments