11from abc import ABC
2- from typing import Any
2+ from typing import Any , Literal
3+
4+ from .util .assertions import assert_is_one_of
5+ from .util .assertions import assert_is_instance_of
6+
7+
8+ Source = Literal ["component" ] | Literal ["container" ] | Literal ["app" ]
9+ Target = Literal ["component" ] | Literal ["container" ] | Literal ["app" ]
10+ NoneType = type (None )
11+
12+
13+ # noinspection PyShadowingBuiltins
14+ def _validate_input (
15+ source : str | None , id : str | None , property : str | None
16+ ) -> tuple [str , str | None , str | None ]:
17+ return _validate_kind ("source" , source , id , property )
18+
19+
20+ # noinspection PyShadowingBuiltins
21+ def _validate_output (
22+ target : str | None , id : str | None , property : str | None
23+ ) -> tuple [str , str | None , str | None ]:
24+ return _validate_kind ("target" , target , id , property )
25+
26+
27+ # noinspection PyShadowingBuiltins
28+ def _validate_kind (
29+ kind_name : str , kind : str | None , id : str | None , property : str | None
30+ ) -> tuple [str , str | None , str | None ]:
31+ assert_is_one_of (kind_name , kind , ("component" , "container" , "app" , None ))
32+ if not kind or kind == "component" :
33+ assert_is_instance_of ("id" , id , (str , NoneType ))
34+ assert_is_instance_of ("property" , id , (str , NoneType ))
35+ kind = kind or "component"
36+ if property is None and id is not None :
37+ property = "value"
38+ else :
39+ assert_is_instance_of ("id" , id , NoneType )
40+ assert_is_instance_of ("property" , property , str )
41+ return kind , id , property
342
443
544class InputOutput (ABC ):
@@ -13,12 +52,14 @@ def __init__(
1352 self .property = property
1453
1554 def to_dict (self ) -> dict [str , Any ]:
16- d = {"type" : self .__class__ .__name__ }
17- d .update ({
18- k : v
19- for k , v in self .__dict__ .items ()
20- if not k .startswith ("_" ) and v is not None
21- })
55+ d = {"class" : self .__class__ .__name__ }
56+ d .update (
57+ {
58+ k : v
59+ for k , v in self .__dict__ .items ()
60+ if not k .startswith ("_" ) and v is not None
61+ }
62+ )
2263 return d
2364
2465
@@ -28,41 +69,42 @@ class Input(InputOutput):
2869 """
2970
3071 # noinspection PyShadowingBuiltins
31- def __init__ (self , id : str , property : str = "value" ):
32- super ().__init__ (id , property )
72+ def __init__ (
73+ self ,
74+ id : str | None = None ,
75+ property : str | None = None ,
76+ source : Source | None = None ,
77+ ):
78+ source , id , property = _validate_input (source , id , property )
79+ super ().__init__ (id = id , property = property )
80+ self .source = source
3381
3482
35- class State (InputOutput ):
83+ class State (Input ):
3684 """An input value read from component state.
3785 Does not trigger callback invocation.
3886 """
3987
4088 # noinspection PyShadowingBuiltins
41- def __init__ (self , id : str , property : str = "value" ):
42- super ().__init__ (id , property )
89+ def __init__ (
90+ self ,
91+ id : str | None = None ,
92+ property : str | None = None ,
93+ source : Source | None = None ,
94+ ):
95+ super ().__init__ (id = id , property = property , source = source )
4396
4497
4598class Output (InputOutput ):
4699 """Callback output."""
47100
48101 # noinspection PyShadowingBuiltins
49- def __init__ (self , id : str , property : str = "value" ):
50- super ().__init__ (id , property )
51-
52-
53- class AppInput (InputOutput ):
54- """An input value read from application state.
55- An application state change may trigger callback invocation.
56- """
57-
58- # noinspection PyShadowingBuiltins
59- def __init__ (self , property : str ):
60- super ().__init__ (property = property )
61-
62-
63- class AppOutput (InputOutput ):
64- """An output written to application state."""
65-
66- # noinspection PyShadowingBuiltins
67- def __init__ (self , property : str ):
68- super ().__init__ (property = property )
102+ def __init__ (
103+ self ,
104+ id : str | None = None ,
105+ property : str | None = None ,
106+ target : Target | None = None ,
107+ ):
108+ target , id , property = _validate_output (target , id , property )
109+ super ().__init__ (id = id , property = property )
110+ self .target = target
0 commit comments