File: typed_state.py

package info (click to toggle)
python-trame-server 3.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 480 kB
  • sloc: python: 4,075; javascript: 5; sh: 4; makefile: 3
file content (652 lines) | stat: -rw-r--r-- 23,157 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import inspect
from abc import ABC, abstractmethod
from dataclasses import MISSING, Field, fields, is_dataclass
from datetime import date, datetime, time, timezone
from decimal import Decimal
from enum import Enum
from pathlib import Path
from types import UnionType
from typing import (
    Any,
    Callable,
    Generic,
    Iterable,
    Type,
    TypeVar,
    Union,
    cast,
    get_args,
    get_origin,
    get_type_hints,
)
from uuid import UUID

from trame_server.state import State

T = TypeVar("T")
V = TypeVar("V")


class _SerializationFailure:
    """
    Simple class to handle encoding / decoding failures
    """

    def __init__(self, reason: str = ""):
        self.reason = reason

    def __eq__(self, other):
        return isinstance(other, _SerializationFailure)


class IStateEncoderDecoder(ABC):
    """
    State to/from primitive type encoding/decoding interface.
    """

    _failure = _SerializationFailure()

    @abstractmethod
    def encode(self, obj):
        pass

    @abstractmethod
    def decode(self, obj, obj_type: type):
        pass

    @staticmethod
    def failed_serialization(reason: str = "") -> _SerializationFailure:
        return _SerializationFailure(reason)

    @classmethod
    def is_serialization_success(cls, value):
        return not isinstance(value, _SerializationFailure)


class DefaultEncoderDecoder(IStateEncoderDecoder):
    """
    Default primitive type encoding/decoding.
    """

    def encode(self, obj):
        if isinstance(obj, UUID):
            return str(obj)
        if isinstance(obj, Enum):
            return obj.value
        if isinstance(obj, Decimal):
            return str(obj)
        if isinstance(obj, datetime):
            return obj.astimezone(timezone.utc).isoformat()
        if isinstance(obj, date):
            return obj.isoformat()
        if isinstance(obj, time):
            return obj.isoformat()
        if isinstance(obj, Path):
            return obj.as_posix()
        return obj

    def decode(self, obj, obj_type: type):
        if obj is None:
            return None
        if isinstance(obj, obj_type):
            return obj
        if issubclass(obj_type, datetime):
            return obj_type.fromisoformat(obj)
        if issubclass(obj_type, date):
            return obj_type.fromisoformat(obj)
        if issubclass(obj_type, time):
            return obj_type.fromisoformat(obj)
        # UUID, Decimal, Enum conversion use obj_type(obj) decoding
        return obj_type(obj)


class CollectionEncoderDecoder(IStateEncoderDecoder):
    """
    Encoding/decoding for lists, tuples, dicts and type unions. Delegates to an encoder list for contained types.
    Expects the encoder in its encoder list to return self.failed_serialization when encoding / decoding a specific
    type is not possible.
    If the delegate encoder raises an error, the error will be caught and considered as failed_serialization.
    Encoder will continue to the following encoder if the previous one wasn't able to encode / decode it.

    :param encoders: List of encoders to use when encoding/decoding lists and dicts.
    """

    def __init__(self, encoders: list[IStateEncoderDecoder] | None = None):
        self._encoders = encoders or [DefaultEncoderDecoder()]

    def encode(self, obj):
        if is_dataclass(obj):
            return {
                field.name: self.encode(getattr(obj, field.name))
                for field in fields(obj)
            }

        if isinstance(obj, dict):
            return {self.encode(key): self.encode(value) for key, value in obj.items()}

        if self._is_iterable(obj):
            return type(obj)(self.encode(value) for value in obj)

        for encoder in self._encoders:
            val = self._try_serialize(encoder.encode, obj)
            if self.is_serialization_success(val):
                return val

        _error_msg = f"Failed to encode object {obj}. No appropriate encoder in {self._encoders}."
        raise TypeError(_error_msg)

    @classmethod
    def _is_iterable(cls, obj):
        return isinstance(obj, list) or isinstance(obj, tuple)

    def _try_serialize(self, f, *args):
        try:
            return f(*args)
        except Exception as e:
            return self.failed_serialization(str(e))

    def decode(self, obj, obj_type: type):
        val = self._try_decode(obj, obj_type)
        if self.is_serialization_success(val):
            return val

        _error_msg = f"Failed to decode object {obj} of type {obj_type}. No appropriate decoder in {self._encoders}."
        raise TypeError(_error_msg)

    def _try_decode(self, obj, obj_type: type):
        for decode in self._decode_strategies():
            val = decode(obj, obj_type)
            if self.is_serialization_success(val):
                return val
        return self.failed_serialization()

    def _decode_strategies(self) -> list[Callable[[Any, type], Any]]:
        return [
            self._decode_dataclass,
            self._decode_union,
            self._decode_dict,
            self._decode_iterable,
            self._delegate_decode,
        ]

    def _delegate_decode(self, obj, obj_type: type):
        for encoder in self._encoders:
            val = self._try_serialize(encoder.decode, obj, obj_type)
            if self.is_serialization_success(val):
                return val
        return self.failed_serialization()

    def _decode_dict(self, obj, obj_type: type):
        if not isinstance(obj, dict):
            return self.failed_serialization()

        key_type, value_type = get_args(obj_type)
        return {
            self.decode(key, key_type): self.decode(value, value_type)
            for key, value in obj.items()
        }

    def _decode_iterable(self, obj, obj_type: type):
        if not self._is_iterable(obj):
            return self.failed_serialization()

        value_type = get_args(obj_type)[0]
        return obj_type(self.decode(value, value_type) for value in obj)

    def _decode_union(self, obj, obj_type: type):
        if not self._is_union_type(obj_type):
            return self.failed_serialization()

        for sub_union_type in get_args(obj_type):
            val = self._try_decode(obj, sub_union_type)
            if self.is_serialization_success(val):
                return val
        return self.failed_serialization()

    def _decode_dataclass(self, obj, obj_type: type):
        if not is_dataclass(obj_type):
            return self.failed_serialization()

        field_types = get_type_hints(obj_type)
        decoded_dict = {
            field.name: self._try_decode(obj.get(field.name), field_types[field.name])
            for field in fields(obj_type)
        }
        return obj_type(**decoded_dict)

    @classmethod
    def _is_union_type(cls, obj_type: type):
        return get_origin(obj_type) is Union or isinstance(obj_type, UnionType)


class _ProxyField:
    """
    Descriptor for proxy state fields to an equivalent dataclass field.
    If the dataclass provides default, or a default factory, the associated state will be initialized to the given
    encoded state value.

    :param state: Trame State which will be mutated / read from.
    :param state_id: Associated trame string id where the data will be pushed / read from.
    :param name: Name of the source field.
    :param field_type: Type of the source field.
    :param default: Default value of the source field.
    :param default_factory: Default factory of the source field.
    :param state_encoder: Encoder/decoder class for the proxy.
    """

    def __init__(
        self,
        *,
        state: State,
        state_id: str,
        name: str,
        field_type: type,
        default,
        default_factory,
        state_encoder: IStateEncoderDecoder,
    ):
        self._state = state
        self._state_id = state_id
        self._name = name
        self._default = default
        self._encoder = state_encoder
        self._type = field_type

        # Set the default value to trame state if needed
        default_value = default
        if default_value == MISSING and default_factory != MISSING:
            default_value = default_factory()
        if default_value != MISSING:
            self._state.setdefault(self._state_id, self._encoder.encode(default_value))

    def __get__(self, instance, owner):
        return self.get_value()

    def __set__(self, instance, value):
        self.set_value(value)

    def get_value(self):
        value = self._state[self._state_id]
        return self._encoder.decode(value, self._type)

    def set_value(self, value):
        self._state[self._state_id] = self._encoder.encode(value)


class _NameField:
    """
    Descriptor for fields to state id string equivalent.

    :param state_id: Associated trame string id where the data will be pushed / read from.
    """

    def __init__(self, state_id: str):
        self._state_id = state_id

    def __get__(self, instance, owner):
        return self._state_id


class TypedState(Generic[T]):
    """
    Helper to have access to, mutate, and be notified of state changes using a strongly typed dataclass interface.

    TypedState provides a type-safe wrapper around the trame State object, allowing to:
    - Access and modify state using dataclass field names with full type hints
    - Bind change callbacks to specific fields or combinations of fields
    - Automatically handle encoding/decoding of complex types (enums, UUIDs, dates, etc.)
    - Use namespaces to avoid conflicts between different state objects
    """

    _STATE_PROXY_DATACLASS_TYPE = "__state_proxy_dataclass_type"
    _STATE_PROXY_FIELD_DICT = "__state_proxy_field_dict"
    _STATE_PROXY_STATE_ID = "__state_proxy_state_id"

    def __init__(
        self,
        state: State,
        dataclass_type: Type[T],
        *,
        namespace="",
        encoders: list[IStateEncoderDecoder] | None = None,
        encoder: IStateEncoderDecoder | None = None,
        data: T | None = None,
        name: T | None = None,
    ):
        self._encoder = encoder or CollectionEncoderDecoder(encoders)
        self.state = state
        self.data = data or self._create_state_proxy(
            dataclass_type=dataclass_type,
            state=state,
            namespace=namespace,
            encoder=self._encoder,
        )

        self.name = name or self._create_state_names_proxy(
            dataclass_type=dataclass_type,
            namespace=namespace,
        )

    def bind_changes(
        self, change_dict: dict[Any | list[Any] | tuple[Any], Callable]
    ) -> None:
        """
        Binds a typed state key change to the given input callback.
        Calls are strongly typed and will call the passed callback only with the input keys and not the full trame
        state.

        Binding is compatible with nested dataclass types.

        :param change_dict: Dict containing the key to callback mapping to bind.
        """
        for key, callback in change_dict.items():
            self.bind_typed_state_change(key, callback, self.state, self.data)

    def encode(self, value: Any) -> Any:
        """
        Encodes the input value with the typed_state state encoder.
        """
        return self._encoder.encode(value)

    def get_dataclass(self) -> T:
        """
        :return: Current content of the typed state as dataclass.
        """
        return self.as_dataclass(self.data)

    def set_dataclass(self, data: T) -> None:
        """
        Set the content of the typed state from the input dataclass.
        Dataclass instance needs to match the dataclass type the typed state was constructed from.
        :param data: Instance of dataclass matching the typed state type.
        """
        self.from_dataclass(self.data, data)

    @classmethod
    def _create_state_proxy(
        cls,
        dataclass_type: Type[T],
        state: State,
        *,
        namespace="",
        encoder: IStateEncoderDecoder | None = None,
    ) -> T:
        """
        Returns a State proxy with the same field structure as the input dataclass and for each field returning a proxy
        to the associated state value.

        :param dataclass_type: Type of dataclass for which the proxy will be created.
        :param state: Trame state which will be mutated/read from by the proxy.
        :param namespace: Optional namespace for the trame state. All proxy field access will be using this
            namespace prefix.
        :param encoder: Optional encoder/decoder from dataclass field to trame state field. If not encoder is
            provided, will use a default encoder/decoder.
        """
        encoder = encoder or CollectionEncoderDecoder(None)

        def handler(state_id: str, field: Field, field_type: type):
            return _ProxyField(
                state=state,
                state_id=state_id,
                name=field.name,
                default=field.default,
                default_factory=field.default_factory,
                field_type=field_type,
                state_encoder=encoder,
            )

        return cls._build_proxy_cls(dataclass_type, namespace, handler, "__Proxy")

    @classmethod
    def _create_state_names_proxy(cls, dataclass_type: Type[T], *, namespace="") -> T:
        """
        Returns a State proxy with the same field structure as the input dataclass and for each field returning the
        fully qualified state id name associated with a dataclass leaf.

        :param dataclass_type: Type of dataclass for which the proxy will be created.
        :param namespace: Optional namespace for the trame state. All proxy field access will be using this
            namespace prefix.
        """

        def handler(state_id: str, _field: Field, _field_type: type):
            return _NameField(state_id=state_id)

        return cls._build_proxy_cls(dataclass_type, namespace, handler, "__ProxyName")

    @classmethod
    def _build_proxy_cls(
        cls,
        dataclass_type: Type[T],
        prefix: str,
        handler: Callable[[str, Field, type], Any],
        cls_suffix: str,
        proxy_field_dict: dict | None = None,
    ) -> T:
        """
        Parses the input dataclass_type fields and construct a dataclass proxy based on its Field
        hierarchy. Visits each Field and nested dataclass recursively and forwards proxy field creation to the handler
        callable.

        :param dataclass_type: Type of the dataclass for which the proxy will be created
        :param prefix: State id prefix for each field created from the dataclass.
        :param handler: Callable which is responsible for creating the proxy attached in place of each Field.
        :param cls_suffix: Suffix attached to the created Proxy class type.
        :param proxy_field_dict: Dict of proxy fields at the parent level. Leave as None when starting recursion.
        :return: Created Proxy instance.
        """
        namespace = {}
        class_name = dataclass_type.__name__
        inner_field_dict = {}
        prefix = f"{prefix}__{class_name}" if prefix else class_name

        # Use type hints instead of field.type to avoid lazy evaluation of field.type when used in files containing
        # from __future__ import annotations header.
        field_types = get_type_hints(dataclass_type)
        for f in fields(dataclass_type):
            state_id = f"{prefix}__{f.name}"
            f_type = field_types[f.name]
            if is_dataclass(f_type):
                field = cls._build_proxy_cls(
                    f_type, state_id, handler, cls_suffix, inner_field_dict
                )
            else:
                field = handler(state_id, f, f_type)

            inner_field_dict[cls.get_state_id(field, state_id)] = field
            namespace[f.name] = field

        if proxy_field_dict is not None:
            proxy_field_dict.update(**inner_field_dict)

        # Add dataclass type, fields and state id to the proxy class
        namespace[cls._STATE_PROXY_DATACLASS_TYPE] = dataclass_type
        namespace[cls._STATE_PROXY_FIELD_DICT] = inner_field_dict
        namespace[cls._STATE_PROXY_STATE_ID] = prefix
        proxy_cls = type(f"{class_name}{cls_suffix}", (), namespace)

        # Create the proxy instance and add instance to the accessible proxy fields
        proxy_instance = proxy_cls()
        inner_field_dict[prefix] = proxy_instance
        return cast(T, proxy_instance)

    @classmethod
    def _get_proxy_dataclass_type(cls, instance: T) -> Type[T] | None:
        """
        :return: dataclass type attached to the input proxy instance.
        """
        return getattr(instance, cls._STATE_PROXY_DATACLASS_TYPE, None)

    @classmethod
    def _get_proxy_dataclass_type_or_raise(cls, instance: T) -> Type[T]:
        """
        :return: dataclass type attached to the proxy instance
        :raises: RuntimeError if the input instance is not a proxy.
        """
        kls = cls._get_proxy_dataclass_type(instance)
        if kls is None:
            _error_msg = (
                f"Expected an instance of type __Proxy got {type(instance).__name__}."
            )
            raise RuntimeError(_error_msg)
        return kls

    @classmethod
    def is_proxy_class(cls, instance: T) -> bool:
        """
        :return: True if the input instance is a state proxy type. False otherwise.
        """
        return cls._get_proxy_dataclass_type(instance) is not None

    @classmethod
    def is_name_proxy_class(cls, instance: T) -> bool:
        return cls.is_proxy_class(instance) and type(instance).__name__.endswith(
            "__ProxyName"
        )

    @classmethod
    def is_data_proxy_class(cls, instance: T) -> bool:
        return cls.is_proxy_class(instance) and type(instance).__name__.endswith(
            "__Proxy"
        )

    @classmethod
    def get_state_id(cls, instance: T, default: str = "") -> str:
        """
        :return: State id string attached to the input state proxy or default if the input is not a state proxy
            instance.
        """
        return getattr(instance, cls._STATE_PROXY_STATE_ID, default)

    @classmethod
    def as_dataclass(cls, instance: T) -> T:
        """
        Converts the input state proxy instance to dataclass.
        """
        dataclass_type = cls._get_proxy_dataclass_type_or_raise(instance)

        kwargs = {}
        for f in fields(dataclass_type):
            attr = getattr(instance, f.name)
            if cls.is_proxy_class(attr):
                kwargs[f.name] = cls.as_dataclass(attr)
            else:
                kwargs[f.name] = attr

        return dataclass_type(**kwargs)

    @classmethod
    def from_dataclass(cls, instance: T, dataclass_obj: T) -> None:
        """
        Populate the state proxy instance from the values of the given dataclass object.
        """
        dataclass_type = cls._get_proxy_dataclass_type_or_raise(instance)

        if not isinstance(dataclass_obj, dataclass_type):
            _error_msg = f"Expected instance of {dataclass_type.__name__}, got {type(dataclass_obj).__name__}"
            raise TypeError(_error_msg)

        for f in fields(dataclass_type):
            attr = getattr(instance, f.name)
            value = getattr(dataclass_obj, f.name)

            if cls.is_proxy_class(attr):
                cls.from_dataclass(attr, value)
            else:
                setattr(instance, f.name, value)

    @classmethod
    def get_field_proxy_dict(cls, instance: T) -> dict[str, _ProxyField]:
        """
        :returns: State ID to ProxyField as saved in the input instance.
        """
        cls._get_proxy_dataclass_type_or_raise(instance)
        return getattr(instance, cls._STATE_PROXY_FIELD_DICT, {})

    @classmethod
    def get_reactive_state_id_keys(cls, keys: Iterable[Any]) -> list[str]:
        """
        returns the list of state ids to react to on change from the input.

        :param keys: tuple of either str or dataclass proxy.
        :return: list of keys
        """
        react_keys = []
        for key in keys:
            if cls.is_proxy_class(key):
                react_keys.extend(list(cls.get_field_proxy_dict(key).keys()))
            else:
                react_keys.append(key)
        return react_keys

    @classmethod
    def get_value_state_keys(cls, keys: Iterable[Any]) -> list[str]:
        """
        returns the list of keys in the proxy to return when the state modified a given key.

        :param keys: tuple of either str or dataclass proxy.
        :return: list of keys present in the proxy instance field dict
        """

        return [cls.get_state_id(key, key) for key in keys]

    @classmethod
    def bind_typed_state_change(
        cls,
        keys: Any | list[Any] | tuple[Any],
        callback: Callable,
        state: State,
        data: T,
    ) -> None:
        """
        Bind state id changes to callbacks.
        Callbacks are called with converted state values found with input keys.
        """
        state_id_to_field_dict = cls.get_field_proxy_dict(data)

        if isinstance(keys, list):
            keys = tuple(keys)

        if not isinstance(keys, tuple):
            keys = (keys,)

        value_keys = cls.get_value_state_keys(keys)

        # On state change, get strongly typed values from the typed state and call the callback with the given values
        def _on_state_change(**_):
            values = [state_id_to_field_dict[k] for k in value_keys]
            values = [v if cls.is_proxy_class(v) else v.get_value() for v in values]
            return callback(*values)

        # Define an async variant for state change in case the bound method is async
        async def _on_state_change_async(**_):
            await _on_state_change()

        # Use state change closure to bind to direct or async version depending on the bound callback
        state.change(*cls.get_reactive_state_id_keys(keys))(
            _on_state_change_async
            if inspect.iscoroutinefunction(callback)
            else _on_state_change
        )

    def get_sub_state(self, sub_name: V) -> "TypedState[V]":
        """
        Create a TypedState based on a sub nested dataclass name proxy.
        The created TypedState will have the same state ids as the ones present in the full TypedState.
        This method can be used to simplify the connection of a "full" TypedState to sub UI components such as buttons
        or sliders.

        :returns: New typed state based on the given input sub name.
        """
        if not self.is_name_proxy_class(sub_name):
            _error_msg = f"Sub state creation should be called with a Name Proxy instance. Got: {type(sub_name)}"
            raise RuntimeError(_error_msg)

        state_id = self.get_state_id(sub_name)
        sub_data = self.get_field_proxy_dict(self.data)[state_id]
        return TypedState(
            self.state,
            self._get_proxy_dataclass_type(sub_name),
            encoder=self._encoder,
            data=sub_data,
            name=sub_name,
        )