Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/config/config_parser.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-08-21 15:37 +0800

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3# ---------------------------------------------------------------------------- 

4# Copyright (c) 2025 Huawei Technologies Co., Ltd. 

5# 

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 

19import os 

20from typing import NamedTuple 

21 

22from params import ParamDict 

23from common import log_debug, log_warning, log_error 

24from common import FileOperate as f 

25from common import get_project_conf 

26from common.const import RetCode 

27 

28__all__ = ["AsysConfigParser"] 

29 

30 

31class IniConfItem(NamedTuple): 

32 para_name: str 

33 conf_val_map: dict 

34 default_key: str 

35 

36 

37ASYS_INI_VALUE_MAP = { 

38 "graph": IniConfItem( 

39 "graph", {"TRUE": "1", "FALSE": "0"}, "TRUE" 

40 ), 

41 "ops": IniConfItem( 

42 "ops", {"TRUE": "1", "FALSE": "0"}, "TRUE" 

43 ), 

44 "dump_ge_graph": IniConfItem( 

45 "DUMP_GE_GRAPH", {"1": "1", "2": "2", "3": "3"}, "2" 

46 ), 

47 "dump_graph_level": IniConfItem( 

48 "DUMP_GRAPH_LEVEL", {"1": "1", "2": "2", "3": "3"}, "2" 

49 ), 

50 "log_level": IniConfItem( 

51 "ASCEND_GLOBAL_LOG_LEVEL", {"DEBUG": "0", "INFO": "1", "WARNING": "2", "ERROR": "3", "NULL": "4"}, "INFO" 

52 ), 

53 "log_event_enable": IniConfItem( 

54 "ASCEND_GLOBAL_EVENT_ENABLE", {"FALSE": "0", "TRUE": "1"}, "TRUE" 

55 ), 

56 "log_print_to_stdout": IniConfItem( 

57 "ASCEND_SLOG_PRINT_TO_STDOUT", {"FALSE": "0", "TRUE": "1"}, "FALSE" 

58 ), 

59} 

60 

61 

62class AsysConfigParser: 

63 

64 def __init__(self): 

65 self.dep_file_path = os.path.join(get_project_conf(), "dependent_package.csv") 

66 self.ini_file_path = os.path.join(get_project_conf(), "asys.ini") 

67 

68 def __parse_deps(self): 

69 dep_info = f.read_file(self.dep_file_path) 

70 if not dep_info: 

71 log_error(f"Read the dependencies file: {self.dep_file_path} failed.") 

72 return RetCode.READ_FILE_FAILED 

73 

74 ParamDict().set_deps(dep_info) 

75 return RetCode.SUCCESS 

76 

77 def __parse_ini(self): 

78 ini_parser = f.read_file(self.ini_file_path) 

79 if not ini_parser: 

80 log_error(f"Read the config file: {self.ini_file_path} failed.") 

81 return RetCode.READ_FILE_FAILED 

82 

83 command = ParamDict().get_command() 

84 if command not in ini_parser.sections(): 

85 log_debug(f"No ini conf items for command: {command}.") 

86 return RetCode.SUCCESS 

87 k_v_pairs = ini_parser.items(command) 

88 for conf_k, conf_v in k_v_pairs: 

89 info_item = ASYS_INI_VALUE_MAP.get(conf_k) 

90 if info_item is None: 

91 log_warning(f"ini conf item: {conf_k} is not available.") 

92 continue 

93 

94 ini_name = info_item.para_name 

95 ini_value = info_item.conf_val_map.get(conf_v) 

96 if ini_value is None: 

97 ini_value = info_item.conf_val_map.get(info_item.default_key) 

98 log_warning(f"ini conf item {ini_name} value error: {conf_v} is not in available range, " 

99 f"use default value: {info_item.default_key}.") 

100 ParamDict().set_ini(ini_name, ini_value) 

101 

102 return RetCode.SUCCESS 

103 

104 def parse(self): 

105 if self.__parse_deps() != RetCode.SUCCESS: 

106 return False 

107 if self.__parse_ini() != RetCode.SUCCESS: 

108 return False 

109 return True