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
20import os
21
22from src.beans.dump_result import DumpResult
23from src.keywords import keywords_dict, get_dict_value
24from src.utils.log_wrapper import log_error
25
26
27class ContentParser:
28    file_path = ""
29    parse_result = None
30
31    def __init__(self, path):
32        self.file_path = path
33
34    def is_succeed(self):
35        return self.parse_result is not None
36
37    def do_parse(self):
38        file_content = self.load_file()
39        if file_content is None or file_content == '':
40            log_error('file is not exist')
41            return None
42        event_tree_count = self.pre_check_event_tree_count(file_content)
43        if event_tree_count == 0:
44            log_error('NO event tree found')
45            return None
46        self.parse_result = DumpResult(file_content)
47        return self.parse_result
48
49    def load_file(self):
50        if os.path.exists(self.file_path):
51            with open(self.file_path, 'r') as f:
52                return f.read()
53        else:
54            return None
55
56    def pre_check_event_tree_count(self, file_content):
57        event_tree_count = 0
58        for line in file_content.split('\n'):
59            index = line.find(get_dict_value(keywords_dict, 'event tree'))
60            if index != -1:
61                event_tree_count += 1
62        return event_tree_count
63