|
1 |
| -from dataclasses import dataclass |
2 |
| -from typing import Type, Dict |
| 1 | +from enum import Enum |
| 2 | +from typing import Type, Dict, get_type_hints |
3 | 3 |
|
4 |
| -import pydantic |
| 4 | +from pydantic import BaseModel |
5 | 5 |
|
6 |
| -from . import events_received_objs |
| 6 | +from . import mixins |
7 | 7 |
|
8 | 8 |
|
9 |
| -@dataclass |
10 |
| -class EventRoutingObj: |
11 |
| - handler_name: str |
12 |
| - obj: Type[pydantic.BaseModel] |
13 |
| - |
| 9 | +class EventRoutingObjTypes(Enum): |
| 10 | + ACTION = "ACTION" |
| 11 | + PLUGIN = "PLUGIN" |
14 | 12 |
|
15 |
| -ACTION_EVENT_ROUTING_MAP: Dict[str, EventRoutingObj] = { |
16 |
| - "didReceiveSettings": EventRoutingObj( |
17 |
| - obj=events_received_objs.DidReceiveSettings, |
18 |
| - handler_name="on_did_receive_settings", |
19 |
| - ), |
20 |
| - "keyDown": EventRoutingObj( |
21 |
| - obj=events_received_objs.KeyDown, |
22 |
| - handler_name="on_key_down", |
23 |
| - ), |
24 |
| - "keyUp": EventRoutingObj( |
25 |
| - obj=events_received_objs.KeyUp, |
26 |
| - handler_name="on_key_up", |
27 |
| - ), |
28 |
| - "touchTap": EventRoutingObj( |
29 |
| - obj=events_received_objs.TouchTap, |
30 |
| - handler_name="on_touch_tap", |
31 |
| - ), |
32 |
| - "dialDown": EventRoutingObj( |
33 |
| - obj=events_received_objs.DialDown, |
34 |
| - handler_name="on_dial_down", |
35 |
| - ), |
36 |
| - "dialUp": EventRoutingObj( |
37 |
| - obj=events_received_objs.DialUp, |
38 |
| - handler_name="on_dial_up", |
39 |
| - ), |
40 |
| - "dialPress": EventRoutingObj( |
41 |
| - obj=events_received_objs.DialPress, |
42 |
| - handler_name="on_dial_press", |
43 |
| - ), |
44 |
| - "dialRotate": EventRoutingObj( |
45 |
| - obj=events_received_objs.DialRotate, |
46 |
| - handler_name="on_dial_rotate", |
47 |
| - ), |
48 |
| - "willAppear": EventRoutingObj( |
49 |
| - obj=events_received_objs.WillAppear, |
50 |
| - handler_name="on_will_appear", |
51 |
| - ), |
52 |
| - "willDisappear": EventRoutingObj( |
53 |
| - obj=events_received_objs.WillDisappear, |
54 |
| - handler_name="on_will_disappear", |
55 |
| - ), |
56 |
| - "titleParametersDidChange": EventRoutingObj( |
57 |
| - obj=events_received_objs.TitleParametersDidChange, |
58 |
| - handler_name="on_title_parameters_did_change", |
59 |
| - ), |
60 |
| - "propertyInspectorDidAppear": EventRoutingObj( |
61 |
| - obj=events_received_objs.PropertyInspectorDidAppear, |
62 |
| - handler_name="on_property_inspector_did_appear", |
63 |
| - ), |
64 |
| - "propertyInspectorDidDisappear": EventRoutingObj( |
65 |
| - obj=events_received_objs.PropertyInspectorDidDisappear, |
66 |
| - handler_name="on_property_inspector_did_disappear", |
67 |
| - ), |
68 |
| - "sendToPlugin": EventRoutingObj( |
69 |
| - obj=events_received_objs.SendToPlugin, |
70 |
| - handler_name="on_send_to_plugin", |
71 |
| - ), |
72 |
| - "sendToPropertyInspector": EventRoutingObj( |
73 |
| - obj=events_received_objs.SendToPropertyInspector, |
74 |
| - handler_name="on_send_to_property_inspector", |
75 |
| - ), |
76 |
| -} |
77 | 13 |
|
78 |
| -PLUGIN_EVENT_ROUTING_MAP: Dict[str, EventRoutingObj] = { |
79 |
| - "didReceiveGlobalSettings": EventRoutingObj( |
80 |
| - obj=events_received_objs.DidReceiveGlobalSettings, |
81 |
| - handler_name="on_did_receive_global_settings", |
82 |
| - ), |
83 |
| - "deviceDidConnect": EventRoutingObj( |
84 |
| - obj=events_received_objs.DeviceDidConnect, |
85 |
| - handler_name="on_device_did_connect", |
86 |
| - ), |
87 |
| - "deviceDidDisconnect": EventRoutingObj( |
88 |
| - obj=events_received_objs.DeviceDidDisconnect, |
89 |
| - handler_name="on_device_did_disconnect", |
90 |
| - ), |
91 |
| - "applicationDidLaunch": EventRoutingObj( |
92 |
| - obj=events_received_objs.ApplicationDidLaunch, |
93 |
| - handler_name="on_application_did_launch", |
94 |
| - ), |
95 |
| - "applicationDidTerminate": EventRoutingObj( |
96 |
| - obj=events_received_objs.ApplicationDidTerminate, |
97 |
| - handler_name="on_application_did_terminate", |
98 |
| - ), |
99 |
| - "systemDidWakeUp": EventRoutingObj( |
100 |
| - obj=events_received_objs.SystemDidWakeUp, |
101 |
| - handler_name="on_system_did_wake_up", |
102 |
| - ), |
103 |
| -} |
104 |
| - |
105 |
| -EVENT_ROUTING_MAP: Dict[str, EventRoutingObj] = { |
106 |
| - **ACTION_EVENT_ROUTING_MAP, |
107 |
| - **PLUGIN_EVENT_ROUTING_MAP, |
108 |
| -} |
| 14 | +class EventRoutingObj(BaseModel): |
| 15 | + handler_name: str |
| 16 | + obj_type: Type[BaseModel] |
| 17 | + type: EventRoutingObjTypes |
| 18 | + |
| 19 | + |
| 20 | +EVENT_ROUTING_MAP: Dict[str, EventRoutingObj] = {} |
| 21 | + |
| 22 | + |
| 23 | +def fill_routing_map( |
| 24 | + routing_map: Dict[str, EventRoutingObj], |
| 25 | + event_handler_mixin: mixins.BaseEventHandlerMixin, |
| 26 | + event_routing_obj_type: EventRoutingObjTypes, |
| 27 | +) -> None: |
| 28 | + event_handler_mixin_dict = dict(event_handler_mixin.__dict__) |
| 29 | + for attr, value in event_handler_mixin_dict.items(): |
| 30 | + attr: str |
| 31 | + if not attr.startswith("on_"): |
| 32 | + continue |
| 33 | + assert callable(value) |
| 34 | + |
| 35 | + handler_type_hints = get_type_hints(value) |
| 36 | + obj_type = handler_type_hints["obj"] |
| 37 | + obj = obj_type.construct() |
| 38 | + event_name = obj.event |
| 39 | + handler_name = value.__name__ |
| 40 | + routing_map[event_name] = EventRoutingObj( |
| 41 | + handler_name=handler_name, |
| 42 | + obj_type=obj_type, |
| 43 | + type=event_routing_obj_type, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def fill_event_routing_map() -> None: |
| 48 | + for event_handler_mixin, event_routing_obj_type in ( |
| 49 | + (mixins.ActionEventHandlersMixin, EventRoutingObjTypes.ACTION), |
| 50 | + (mixins.PluginEventHandlersMixin, EventRoutingObjTypes.PLUGIN), |
| 51 | + ): |
| 52 | + fill_routing_map( |
| 53 | + routing_map=EVENT_ROUTING_MAP, |
| 54 | + event_handler_mixin=event_handler_mixin, |
| 55 | + event_routing_obj_type=event_routing_obj_type, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +fill_event_routing_map() |
0 commit comments