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 src.beans.base_bean import BaseBean
20from src.beans.event_tree import EventTree
21from src.keywords import keywords_dict, get_dict_value
22from src.utils.log_wrapper import log_info
23from src.utils.value_parser import pack_string_until_next_keyword
24
25
26class DumpResult(BaseBean):
27    event_trees = []
28
29    def __init__(self, input_str):
30        super().__init__()
31        self.event_trees = []
32        self.parse_event_trees(input_str)
33        self.check_parse_result()
34        if self.is_succeed():
35            self.update_tree_info()
36        log_info('parse result: ' + self.to_string())
37
38    def parse_event_trees(self, input_str):
39        if input_str is None or len(input_str) == 0:
40            return
41        spliced_lines = input_str.split('\n')
42        current_index = 0
43        start_keyword = get_dict_value(keywords_dict, 'event tree')
44        end_keywords = [start_keyword]
45        while current_index < len(spliced_lines):
46            line = spliced_lines[current_index]
47            if line.find(start_keyword) == -1:
48                # not found, try next line
49                current_index += 1
50                continue
51            # found, pack one event tree str
52            packed_result = pack_string_until_next_keyword(spliced_lines, current_index, start_keyword, end_keywords)
53            if packed_result is None:
54                # parse end
55                return
56            packed_str = packed_result[0]
57            current_index = packed_result[1]
58            if packed_result is None or packed_str is None or len(packed_str) == 0:
59                self.parse_failed()
60                return
61            event_tree = EventTree(packed_str)
62            if event_tree.is_succeed():
63                self.event_trees.append(event_tree)
64
65    def update_tree_info(self):
66        for event_tree in self.event_trees:
67            event_tree.update_event_nodes()
68
69    def get_tree_count(self):
70        return len(self.event_trees)
71
72    def check_parse_result(self):
73        if self.event_trees is None or len(self.event_trees) == 0:
74            self.parse_failed()
75        else:
76            self.parse_succeed()
77
78    def to_string(self):
79        result_str = 'DumpResult:'
80        index = 0
81        for event_tree in self.event_trees:
82            result_str += '\nevent tree ' + str(event_tree.tree_id) + '\n' + event_tree.to_string()
83            index += 1
84        return result_str
85
86    def dump(self):
87        log_info(self.to_string())
88