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
19from typing import List
20
21from src.beans.base_bean import BaseBean
22from src.beans.event_scope import EventScope
23from src.beans.frame_node import FrameNode
24from src.utils.log_wrapper import log_info
25from src.utils.value_parser import pack_string_until_next_keyword
26
27
28class EventProcedures(BaseBean):
29    event_scopes = []
30
31    def __init__(self, input_str):
32        super().__init__()
33        self.event_scopes = []
34        self.parse_event_scopes(input_str)
35        self.check_parse_result()
36
37    def parse_event_scopes(self, input_str):
38        spliced_lines = input_str.split('\n')
39        start_keyword = 'finger:'
40        end_keywords = ['finger:', 'event tree =>']
41        current_index = 0
42        while current_index < len(spliced_lines):
43            current_line = spliced_lines[current_index]
44            if current_line.find('finger:') != -1:
45                packed_result = pack_string_until_next_keyword(spliced_lines, current_index, start_keyword,
46                                                               end_keywords)
47                packed_str = packed_result[0]
48                if packed_str is None or len(packed_str) == 0:
49                    current_index += 1
50                    continue
51                current_index = packed_result[1]
52                event_scope = EventScope(packed_str)
53                if event_scope.is_succeed():
54                    self.event_scopes.append(event_scope)
55            else:
56                current_index += 1
57
58    def update_event_nodes_with_frame_nodes(self, frame_nodes: List[FrameNode]):
59        for scope in self.event_scopes:
60            scope.update_event_nodes_with_frame_nodes(frame_nodes)
61
62    def check_parse_result(self):
63        if self.event_scopes is None or len(self.event_scopes) == 0:
64            self.parse_failed()
65        else:
66            self.parse_succeed()
67
68    def to_string(self):
69        result_str = 'event procedures: '
70        for scope in self.event_scopes:
71            result_str += '\n' + scope.to_string()
72        return result_str
73
74    def dump(self):
75        log_info(self.to_string())
76