|
| 1 | +def _operation(name, location, **kwargs): |
| 2 | + return {"operation": name, "location": location, "params": dict(**kwargs)} |
| 3 | + |
| 4 | + |
| 5 | +_noop = object() |
| 6 | + |
| 7 | + |
| 8 | +def validate_slice(obj): |
| 9 | + if isinstance(obj, slice): |
| 10 | + raise TypeError("a slice is not a valid index for patch") |
| 11 | + |
| 12 | + |
| 13 | +class Patch: |
| 14 | + """ |
| 15 | + Patch a callback output value |
| 16 | +
|
| 17 | + Act like a proxy of the output prop value on the frontend. |
| 18 | +
|
| 19 | + Supported prop types: Dictionaries and lists. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, location=None, parent=None): |
| 23 | + if location is not None: |
| 24 | + self._location = location |
| 25 | + else: |
| 26 | + # pylint: disable=consider-using-ternary |
| 27 | + self._location = (parent and parent._location) or [] |
| 28 | + if parent is not None: |
| 29 | + self._operations = parent._operations |
| 30 | + else: |
| 31 | + self._operations = [] |
| 32 | + |
| 33 | + def __getitem__(self, item): |
| 34 | + validate_slice(item) |
| 35 | + return Patch(location=self._location + [item], parent=self) |
| 36 | + |
| 37 | + def __getattr__(self, item): |
| 38 | + if item == "tolist": |
| 39 | + # to_json fix |
| 40 | + raise AttributeError |
| 41 | + if item == "_location": |
| 42 | + return self._location |
| 43 | + if item == "_operations": |
| 44 | + return self._operations |
| 45 | + return self.__getitem__(item) |
| 46 | + |
| 47 | + def __setattr__(self, key, value): |
| 48 | + if key in ("_location", "_operations"): |
| 49 | + self.__dict__[key] = value |
| 50 | + else: |
| 51 | + self.__setitem__(key, value) |
| 52 | + |
| 53 | + def __delattr__(self, item): |
| 54 | + self.__delitem__(item) |
| 55 | + |
| 56 | + def __setitem__(self, key, value): |
| 57 | + validate_slice(key) |
| 58 | + if value is _noop: |
| 59 | + # The += set themselves. |
| 60 | + return |
| 61 | + self._operations.append( |
| 62 | + _operation( |
| 63 | + "Assign", |
| 64 | + self._location + [key], |
| 65 | + value=value, |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + def __delitem__(self, key): |
| 70 | + validate_slice(key) |
| 71 | + self._operations.append(_operation("Delete", self._location + [key])) |
| 72 | + |
| 73 | + def __iadd__(self, other): |
| 74 | + if isinstance(other, (list, tuple)): |
| 75 | + self.extend(other) |
| 76 | + else: |
| 77 | + self._operations.append(_operation("Add", self._location, value=other)) |
| 78 | + return _noop |
| 79 | + |
| 80 | + def __isub__(self, other): |
| 81 | + self._operations.append(_operation("Sub", self._location, value=other)) |
| 82 | + return _noop |
| 83 | + |
| 84 | + def __imul__(self, other): |
| 85 | + self._operations.append(_operation("Mul", self._location, value=other)) |
| 86 | + return _noop |
| 87 | + |
| 88 | + def __itruediv__(self, other): |
| 89 | + self._operations.append(_operation("Div", self._location, value=other)) |
| 90 | + return _noop |
| 91 | + |
| 92 | + def __ior__(self, other): |
| 93 | + self.update(E=other) |
| 94 | + return _noop |
| 95 | + |
| 96 | + def append(self, item): |
| 97 | + """Add the item to the end of a list""" |
| 98 | + self._operations.append(_operation("Append", self._location, value=item)) |
| 99 | + |
| 100 | + def prepend(self, item): |
| 101 | + """Add the item to the start of a list""" |
| 102 | + self._operations.append(_operation("Prepend", self._location, value=item)) |
| 103 | + |
| 104 | + def insert(self, index, item): |
| 105 | + """Add the item at the index of a list""" |
| 106 | + self._operations.append( |
| 107 | + _operation("Insert", self._location, value=item, index=index) |
| 108 | + ) |
| 109 | + |
| 110 | + def clear(self): |
| 111 | + """Remove all items in a list""" |
| 112 | + self._operations.append(_operation("Clear", self._location)) |
| 113 | + |
| 114 | + def reverse(self): |
| 115 | + """Reversal of the order of items in a list""" |
| 116 | + self._operations.append(_operation("Reverse", self._location)) |
| 117 | + |
| 118 | + def extend(self, item): |
| 119 | + """Add all the items to the end of a list""" |
| 120 | + if not isinstance(item, (list, tuple)): |
| 121 | + raise TypeError(f"{item} should be a list or tuple") |
| 122 | + self._operations.append(_operation("Extend", self._location, value=item)) |
| 123 | + |
| 124 | + def remove(self, item): |
| 125 | + """filter the item out of a list on the frontend""" |
| 126 | + self._operations.append(_operation("Remove", self._location, value=item)) |
| 127 | + |
| 128 | + def update(self, E=None, **F): |
| 129 | + """Merge a dict or keyword arguments with another dictionary""" |
| 130 | + value = E or {} |
| 131 | + value.update(F) |
| 132 | + self._operations.append(_operation("Merge", self._location, value=value)) |
| 133 | + |
| 134 | + # pylint: disable=no-self-use |
| 135 | + def sort(self): |
| 136 | + raise KeyError("sort is reserved for future use, use brackets to access this key on your object") |
| 137 | + |
| 138 | + def to_plotly_json(self): |
| 139 | + return { |
| 140 | + "__dash_patch_update": "__dash_patch_update", |
| 141 | + "operations": self._operations, |
| 142 | + } |
0 commit comments