1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2024 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19# 定义所有关键词
20# 结构: 每个event tree 按照如下结构解析
21# 2: event tree =>
22#  touch points:
23#   id: 0, point: Offset (278.00, 551.00), screenPoint: Offset (278.00, 551.00), type: TouchDown, timestamp:
24#   2017-08-25 15:00:22.244, isInjected: 0
25#   id: 0, point: Offset (278.00, 551.00), screenPoint: Offset (278.00, 551.00), type: TouchUp, timestamp: 2017-08-25
26#   15:00:22.295, isInjected: 0
27#  hittest:
28#   nodeId: 0, parentId: -1, tag: root, monopolizeEvents: 0, isHit: 1, hitTestMode: 0, responseRegion: RectT (0.00,
29#   0.00) - [720.00 x 1280.00]
30#   nodeId: 1, parentId: 0, tag: stage, monopolizeEvents: 0, isHit: 1, hitTestMode: 0, responseRegion: RectT (0.00,
31#   0.00) - [720.00 x 1280.00]
32#  event procedures:
33#   finger:0
34#    frameNodeId: 84, type: TouchEventActuator, depth: 0, id: 0xf072b240, parentId: 0x0
35#     stateHistory:
36#      procedure: HandleTouchDown, timestamp: 2017-08-25 15:00:22.247
37#      procedure: HandleTouchUp, timestamp: 2017-08-25 15:00:22.295
38#   finger:1
39#    frameNodeId: 84, type: TouchEventActuator, depth: 0, id: 0xf072b240, parentId: 0x0
40#     stateHistory:
41#      procedure: HandleTouchDown, timestamp: 2017-08-25 15:00:22.247
42#      procedure: HandleTouchUp, timestamp: 2017-08-25 15:00:22.295
43
44
45from typing import Dict, Any
46
47
48class KeyValueSample:
49    key = ""
50    value = ""
51    value_separator_count = 0
52
53    def __init__(self, key_and_value, force_no_space=False):
54        if force_no_space:
55            texts = key_and_value.split(":")
56        else:
57            texts = key_and_value.split(": ")
58        if len(texts) != 2:
59            if len(texts) == 1:
60                self.key = texts[0]
61                self.value = ""
62            else:
63                self.key = ""
64                self.value = ""
65        else:
66            self.key = texts[0] + (": " if not force_no_space else ":")
67            self.value = texts[1]
68        self.value_separator_count = self.value.count(", ")
69
70
71# level 1 keywords
72keywords_dict = {
73    "EventTreeDumpInfo": " EventTreeDumpInfo: ",
74    "event tree": "event tree =>",
75}
76
77# touch point keywords
78#  touch points:
79#   id: 0, point: Offset (278.00, 551.00), screenPoint: Offset (278.00, 551.00), type: TouchDown, timestamp:
80#   2017-08-25 15:00:22.244, isInjected: 0
81#   id: 0, point: Offset (278.00, 551.00), screenPoint: Offset (278.00, 551.00), type: TouchUp, timestamp: 2017-08-25
82#   15:00:22.295, isInjected: 0
83touch_point_keyword = {
84    "touch points": KeyValueSample("touch points:"),
85    "id": KeyValueSample("id: 0"),
86    "point": KeyValueSample("point: Offset (278.00, 551.00)"),
87    "screenPoint": KeyValueSample("screenPoint: Offset (278.00, 551.00)"),
88    "type": KeyValueSample("type: TouchDown,"),
89    "timestamp": KeyValueSample("timestamp: 2017-08-25 15:00:22.244"),
90    "isInjected": KeyValueSample("isInjected: 0"),
91}
92
93#   nodeId: 0, parentId: -1, tag: root, monopolizeEvents: 0, isHit: 1, hitTestMode: 0, responseRegion: RectT (0.00,
94#   0.00) - [720.00 x 1280.00]
95hittest_node_keyword = {
96    "hittest": KeyValueSample("hittest:"),
97    "nodeId": KeyValueSample("nodeId: 0"),
98    "parentId": KeyValueSample("parentId: -1"),
99    "tag": KeyValueSample("tag: root"),
100    "comId": KeyValueSample("comId: PageDesktopLayout"),
101    "monopolizeEvents": KeyValueSample("monopolizeEvents: 0"),
102    "isHit": KeyValueSample("isHit: 1"),
103    "hitTestMode": KeyValueSample("hitTestMode: 0"),
104    "responseRegion": KeyValueSample("responseRegion: RectT (0.00, 0.00) - [720.00 x 1280.00]"),
105}
106
107# event procedure keywords
108#  event procedures:
109#   finger:0
110#    frameNodeId: 84, type: TouchEventActuator, depth: 0, id: 0xf072b240, parentId: 0xf072b240
111#     stateHistory:
112#      procedure: HandleTouchDown, timestamp: 2017-08-25 15:00:22.247
113#      procedure: HandleTouchUp, timestamp: 2017-08-25 15:00:22.295
114#   finger:1
115#    frameNodeId: 84, type: TouchEventActuator, depth: 0, id: 0xf072b240, parentId: 0xf072b240
116#     stateHistory:
117#      procedure: HandleTouchDown, timestamp: 2017-08-25 15:00:22.247
118#      procedure: HandleTouchUp, timestamp: 2017-08-25 15:00:22.295
119event_procedure_keyword = {
120    "event procedures": KeyValueSample("event procedures:"),
121    "finger": KeyValueSample("finger:0", True),
122    "frameNodeId": KeyValueSample("frameNodeId: 84"),
123    "type": KeyValueSample("type: TouchEventActuator"),
124    "depth": KeyValueSample("depth: 0"),
125    "id": KeyValueSample("id: 0xf072b240"),
126    "parentId": KeyValueSample("parentId: 0xf072b240"),
127    "stateHistory": KeyValueSample("stateHistory:"),
128    "procedure": KeyValueSample("procedure: HandleTouchDown"),
129    "state": KeyValueSample("state: READY"),
130    "disposal": KeyValueSample("disposal: NONE"),
131    "timestamp": KeyValueSample("timestamp: 2017-08-25 15:00:22.247"),
132    "duration": KeyValueSample("duration: 500"),
133    "isForDrag": KeyValueSample("isForDrag: 0"),
134    "repeat": KeyValueSample("repeat: 0"),
135    "customInfo": KeyValueSample("customInfo: "),
136    "direction": KeyValueSample("direction: 3"),
137    "distance": KeyValueSample("distance: 7.5"),
138    "fingers": KeyValueSample("fingers: 1")
139}
140
141
142def get_dict_value(input_dict: Dict[str, Any], key: str) -> Any:
143    if not isinstance(input_dict, dict):
144        raise ValueError("input_dict must be a dictionary")
145    return input_dict.get(key)
146
147
148def get_sample_key(input_dict: Dict[str, KeyValueSample], name: str) -> str:
149    sample: KeyValueSample = get_dict_value(input_dict, name)
150    if sample is None:
151        return ''
152    return sample.key
153
154
155def get_sample_value(input_dict: Dict[str, KeyValueSample], name: str) -> str:
156    sample: KeyValueSample = get_dict_value(input_dict, name)
157    if sample is None:
158        return ''
159    return sample.value
160
161
162def get_sample_separator_count(input_dict: Dict[str, KeyValueSample], name: str) -> int:
163    sample: KeyValueSample = get_dict_value(input_dict, name)
164    if sample is None:
165        return 0
166    return sample.value_separator_count
167