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.procedure_step import ProcedureStep
21from src.utils.log_wrapper import log_info
22
23
24class StateHistory(BaseBean):
25    event_procedures = []
26    original_str = ''
27
28    def __init__(self, input_str):
29        super().__init__()
30        self.original_str = input_str
31        self.event_procedures = []
32        self.splice_event_procedure(input_str)
33        self.check_parse_result()
34
35    def splice_event_procedure(self, input_str):
36        for line in input_str.split('\n'):
37            if line.find('procedure: ') == -1:
38                continue
39            event_procedure = ProcedureStep(line)
40            if event_procedure is not None:
41                self.event_procedures.append(event_procedure)
42
43    def check_parse_result(self):
44        if self.event_procedures is None or len(self.event_procedures) == 0:
45            self.parse_failed()
46        else:
47            self.parse_succeed()
48
49    def get_detailed_summary_string(self):
50        result_str = ''
51        for procedure_step in self.event_procedures:
52            result_str += procedure_step.get_detailed_summary_string() + '\n'
53        result_str.rstrip('\n')
54        return result_str
55
56    def to_string(self):
57        result_str = '  stateHistory:'
58        for event_procedure in self.event_procedures:
59            result_str += '\n' + event_procedure.to_string()
60        return result_str
61
62    def dump(self):
63        log_info(self.to_string())
64