|
| 1 | +from abc import ABC |
| 2 | +from typing import Any, Literal |
| 3 | + |
| 4 | +from .util.assertions import assert_is_instance_of |
| 5 | +from .util.assertions import assert_is_none |
| 6 | +from .util.assertions import assert_is_one_of |
| 7 | + |
| 8 | + |
| 9 | +Link = Literal["component"] | Literal["container"] | Literal["app"] |
| 10 | + |
| 11 | + |
| 12 | +class Channel(ABC): |
| 13 | + """Base class for `Input`, `State`, and `Output`. |
| 14 | + Instances are used as argument passed to |
| 15 | + the `layout` and `callback` decorators. |
| 16 | + """ |
| 17 | + |
| 18 | + # noinspection PyShadowingBuiltins |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + link: Link | None = None, |
| 22 | + id: str | None = None, |
| 23 | + property: str | None = None, |
| 24 | + ): |
| 25 | + self.link = link |
| 26 | + self.id = id |
| 27 | + self.property = property |
| 28 | + |
| 29 | + def to_dict(self) -> dict[str, Any]: |
| 30 | + d = { |
| 31 | + k: v |
| 32 | + for k, v in self.__dict__.items() |
| 33 | + if not k.startswith("_") and v is not None |
| 34 | + } |
| 35 | + if self.no_trigger: |
| 36 | + d |= dict(noTrigger=True) |
| 37 | + return d |
| 38 | + |
| 39 | + @property |
| 40 | + def no_trigger(self): |
| 41 | + return isinstance(self, State) |
| 42 | + |
| 43 | + |
| 44 | +class Input(Channel): |
| 45 | + """An input value read from component state. |
| 46 | + A component state change may trigger callback invocation. |
| 47 | + """ |
| 48 | + |
| 49 | + # noinspection PyShadowingBuiltins |
| 50 | + def __init__( |
| 51 | + self, |
| 52 | + id: str | None = None, |
| 53 | + property: str | None = None, |
| 54 | + source: Link | None = None, |
| 55 | + ): |
| 56 | + link, id, property = _validate_input_params(source, id, property) |
| 57 | + super().__init__(link=link, id=id, property=property) |
| 58 | + |
| 59 | + |
| 60 | +class State(Input): |
| 61 | + """An input value read from component state. |
| 62 | + Does not trigger callback invocation. |
| 63 | + """ |
| 64 | + |
| 65 | + # noinspection PyShadowingBuiltins |
| 66 | + def __init__( |
| 67 | + self, |
| 68 | + id: str | None = None, |
| 69 | + property: str | None = None, |
| 70 | + source: Link | None = None, |
| 71 | + ): |
| 72 | + super().__init__(id=id, property=property, source=source) |
| 73 | + |
| 74 | + |
| 75 | +class Output(Channel): |
| 76 | + """Callback output.""" |
| 77 | + |
| 78 | + # noinspection PyShadowingBuiltins |
| 79 | + def __init__( |
| 80 | + self, |
| 81 | + id: str | None = None, |
| 82 | + property: str | None = None, |
| 83 | + target: Link | None = None, |
| 84 | + ): |
| 85 | + target, id, property = _validate_output_params(target, id, property) |
| 86 | + super().__init__(link=target, id=id, property=property) |
| 87 | + |
| 88 | + |
| 89 | +NoneType = type(None) |
| 90 | + |
| 91 | + |
| 92 | +# noinspection PyShadowingBuiltins |
| 93 | +def _validate_input_params( |
| 94 | + source: Link | None, id: str | None, property: str | None |
| 95 | +) -> tuple[Link, str | None, str | None]: |
| 96 | + return _validate_params("source", source, id, property) |
| 97 | + |
| 98 | + |
| 99 | +# noinspection PyShadowingBuiltins |
| 100 | +def _validate_output_params( |
| 101 | + target: Link | None, id: str | None, property: str | None |
| 102 | +) -> tuple[Link, str | None, str | None]: |
| 103 | + return _validate_params("target", target, id, property) |
| 104 | + |
| 105 | + |
| 106 | +# noinspection PyShadowingBuiltins |
| 107 | +def _validate_params( |
| 108 | + link_name: str, link: Link | None, id: str | None, property: str | None |
| 109 | +) -> tuple[Link, str | None, str | None]: |
| 110 | + assert_is_one_of(link_name, link, ("component", "container", "app", None)) |
| 111 | + if not link or link == "component": |
| 112 | + assert_is_instance_of("id", id, (str, NoneType)) |
| 113 | + assert_is_instance_of("property", id, (str, NoneType)) |
| 114 | + link = link or "component" |
| 115 | + if property is None and id is not None: |
| 116 | + property = "value" |
| 117 | + else: |
| 118 | + assert_is_none("id", id) |
| 119 | + assert_is_instance_of("property", property, str) |
| 120 | + # noinspection PyTypeChecker |
| 121 | + return link, id, property |
0 commit comments