| 1 |
from typing import Any, List, Union, Iterable, Optional |
| 2 |
|
| 3 |
try: |
| 4 |
from typing import SupportsIndex |
| 5 |
except ImportError: |
| 6 |
from typing_extensions import SupportsIndex |
| 7 |
|
| 8 |
import bonsai |
| 9 |
|
| 10 |
|
| 11 |
class LDAPValueList(list): |
| 12 |
""" |
| 13 |
Modified list that tracks added and deleted values. It also contains |
| 14 |
only unique elements. The elements are compared to their lower-cased |
| 15 |
string representations. |
| 16 |
|
| 17 |
A new LDAPValueList can be created optionally from an existing |
| 18 |
sequence object. |
| 19 |
|
| 20 |
:param items: a sequence object. |
| 21 |
:raises ValueError: if `items` has a non-unique element. |
| 22 |
""" |
| 23 |
|
| 24 |
__slots__ = ("__deleted", "__added", "__status") |
| 25 |
|
| 26 |
def __init__(self, items: Optional[Iterable[Any]] = None) -> None: |
| 27 |
super().__init__() |
| 28 |
self.__added = [] |
| 29 |
self.__deleted = [] |
| 30 |
self.__status = 0 |
| 31 |
if items: |
| 32 |
for item in items: |
| 33 |
self.append(item) |
| 34 |
|
| 35 |
@staticmethod |
| 36 |
def __balance(lst1: List[str], lst2: List[str], value: Any) -> None: |
| 37 |
""" |
| 38 |
Balancing between the two list (__added, __deleted), |
| 39 |
make sure the same element is not in both lists. |
| 40 |
""" |
| 41 |
try: |
| 42 |
lst1.remove(value) |
| 43 |
except ValueError: |
| 44 |
lst2.append(value) |
| 45 |
|
| 46 |
def _append_unchecked(self, value: Any) -> None: |
| 47 |
super().append(value) |
| 48 |
|
| 49 |
def _remove_unchecked(self, value: Any) -> None: |
| 50 |
try: |
| 51 |
super().remove(value) |
| 52 |
except ValueError: |
| 53 |
pass |
| 54 |
|
| 55 |
@property |
| 56 |
def _status_dict(self) -> dict: |
| 57 |
return { |
| 58 |
"@status": self.__status, |
| 59 |
"@added": self.__added.copy(), |
| 60 |
"@deleted": self.__deleted.copy(), |
| 61 |
} |
| 62 |
|
| 63 |
def __contains__(self, item: Any) -> bool: |
| 64 |
return bonsai.utils._unique_contains(self, item)[0] |
| 65 |
|
| 66 |
def __delitem__(self, idx: Union[SupportsIndex, slice]) -> None: |
| 67 |
old_value = super().__getitem__(idx) |
| 68 |
if isinstance(idx, slice): |
| 69 |
for item in old_value: |
| 70 |
self.__balance(self.__added, self.__deleted, item) |
| 71 |
else: |
| 72 |
self.__balance(self.__added, self.__deleted, old_value) |
| 73 |
super().__delitem__(idx) |
| 74 |
|
| 75 |
def __mul__(self, value: Any) -> "LDAPValueList": |
| 76 |
raise TypeError("Cannot multiple LDAPValueList.") |
| 77 |
|
| 78 |
def __add__(self, other: Iterable) -> "LDAPValueList": |
| 79 |
if not isinstance(other, (list, LDAPValueList)): |
| 80 |
raise TypeError("Can only concatenate list and LDAPValueList.") |
| 81 |
new_list = self.copy() |
| 82 |
new_list.extend(other) |
| 83 |
return new_list |
| 84 |
|
| 85 |
def __iadd__(self, other: Iterable) -> "LDAPValueList": |
| 86 |
if not isinstance(other, (list, LDAPValueList)): |
| 87 |
raise TypeError("Can only concatenate list and LDAPValueList.") |
| 88 |
self.extend(other) |
| 89 |
return self |
| 90 |
|
| 91 |
def __setitem__(self, idx: Union[SupportsIndex, slice], value: Any) -> None: |
| 92 |
old_value = self[idx] |
| 93 |
if isinstance(idx, slice): |
| 94 |
for item in value: |
| 95 |
if item in self: |
| 96 |
raise ValueError("%r is already in the list." % item) |
| 97 |
for item in old_value: |
| 98 |
self.__balance(self.__added, self.__deleted, item) |
| 99 |
for item in value: |
| 100 |
self.__balance(self.__deleted, self.__added, item) |
| 101 |
else: |
| 102 |
if value in self: |
| 103 |
raise ValueError("%r is already in the list." % value) |
| 104 |
self.__balance(self.__added, self.__deleted, old_value) |
| 105 |
self.__balance(self.__deleted, self.__added, value) |
| 106 |
super().__setitem__(idx, value) |
| 107 |
|
| 108 |
def append(self, item: Any) -> None: |
| 109 |
""" |
| 110 |
Add a unique item to the end of the LDAPValueList. |
| 111 |
|
| 112 |
:param item: New item. |
| 113 |
:raises ValueError: if the `item` is not unique. |
| 114 |
""" |
| 115 |
|
| 116 |
if item in self: |
| 117 |
raise ValueError("%r is already in the list." % item) |
| 118 |
self.__balance(self.__deleted, self.__added, item) |
| 119 |
self.__status = 1 |
| 120 |
super().append(item) |
| 121 |
|
| 122 |
def extend(self, items: Iterable[Any]) -> None: |
| 123 |
""" |
| 124 |
Extend the LDAPValueList by appending all the items in the given |
| 125 |
list. All element in `items` must be unique and also not |
| 126 |
represented in the LDAPValueList. |
| 127 |
|
| 128 |
:param items: List of new items. |
| 129 |
:raises ValueError: if any of the items is already in the list. |
| 130 |
""" |
| 131 |
for item in items: |
| 132 |
if item in self: |
| 133 |
raise ValueError("%r is already in the list." % item) |
| 134 |
for item in items: |
| 135 |
self.__balance(self.__deleted, self.__added, item) |
| 136 |
self.__status = 1 |
| 137 |
super().extend(items) |
| 138 |
|
| 139 |
def insert(self, idx: SupportsIndex, value: Any) -> None: |
| 140 |
""" |
| 141 |
Insert a unique item at a given position. |
| 142 |
|
| 143 |
:param int idx: the position. |
| 144 |
:param value: the new item. |
| 145 |
:raises ValueError: if the `item` is not unique. |
| 146 |
""" |
| 147 |
if value in self: |
| 148 |
raise ValueError("%r is already in the list." % value) |
| 149 |
self.__balance(self.__deleted, self.__added, value) |
| 150 |
self.__status = 1 |
| 151 |
super().insert(idx, value) |
| 152 |
|
| 153 |
def remove(self, value: Any) -> None: |
| 154 |
""" |
| 155 |
Remove the first item from the LDAPValueList whose value is `value`. |
| 156 |
|
| 157 |
:param value: the item to be removed. |
| 158 |
:raises ValueError: if `value` is not int the list. |
| 159 |
""" |
| 160 |
contain, obj = bonsai.utils._unique_contains(self, value) |
| 161 |
if not contain: |
| 162 |
raise ValueError("%r is not in the list." % value) |
| 163 |
super().remove(obj) |
| 164 |
self.__status = 1 |
| 165 |
self.__balance(self.__added, self.__deleted, obj) |
| 166 |
|
| 167 |
def pop(self, idx: SupportsIndex = -1) -> Any: |
| 168 |
""" |
| 169 |
Remove the item at the given position in the LDAPValueList, and |
| 170 |
return it. If no index is specified, pop() removes and returns the |
| 171 |
last item in the list. |
| 172 |
|
| 173 |
:param int idx: optional index. |
| 174 |
""" |
| 175 |
value = super().pop(idx) |
| 176 |
self.__balance(self.__added, self.__deleted, value) |
| 177 |
self.__status = 1 |
| 178 |
return value |
| 179 |
|
| 180 |
def clear(self) -> None: |
| 181 |
"""Remove all items from the LDAPValueList.""" |
| 182 |
del self[:] |
| 183 |
|
| 184 |
def copy(self) -> "LDAPValueList": |
| 185 |
""" |
| 186 |
Return a shallow copy of the LDAPValueList. This includes |
| 187 |
the status and the previously added and deleted items. |
| 188 |
|
| 189 |
:rtype: LDAPValueList |
| 190 |
:return: The copy of the LDAPValueList. |
| 191 |
""" |
| 192 |
new_list = LDAPValueList() |
| 193 |
for item in self: |
| 194 |
new_list._append_unchecked(item) |
| 195 |
new_list.__added = self.__added.copy() |
| 196 |
new_list.__deleted = self.__deleted.copy() |
| 197 |
new_list.__status = self.__status |
| 198 |
return new_list |
| 199 |
|
| 200 |
@property |
| 201 |
def added(self) -> List[str]: |
| 202 |
"""List of the added values.""" |
| 203 |
return self.__added |
| 204 |
|
| 205 |
@property |
| 206 |
def deleted(self) -> List[str]: |
| 207 |
"""List of the deleted values.""" |
| 208 |
return self.__deleted |
| 209 |
|
| 210 |
@property |
| 211 |
def status(self) -> int: |
| 212 |
""" |
| 213 |
The status of the LDAPValueList. The status can be: |
| 214 |
- 0: unchanged. |
| 215 |
- 1: added or deleted item to list. |
| 216 |
- 2: replaced the entire list. |
| 217 |
""" |
| 218 |
return self.__status |
| 219 |
|
| 220 |
@status.setter |
| 221 |
def status(self, value: int) -> None: |
| 222 |
if not isinstance(value, int): |
| 223 |
raise TypeError("Status must be int.") |
| 224 |
if value > -1 and value < 3: |
| 225 |
self.__status = value |
| 226 |
else: |
| 227 |
raise ValueError("Status must be between 0 and 2.") |