1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2022 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 19import json 20import os 21import stat 22from _core.error import ErrorMessage 23from _core.exception import ParamError 24from _core.logger import platform_logger 25from _core.plugin import Config 26 27__all__ = ["JsonParser"] 28LOG = platform_logger("JsonParser") 29 30 31class JsonParser: 32 """ 33 This class parses json files or string, sample: 34 { 35 "description": "Config for lite cpp test cases", 36 "environment": [ 37 { 38 "type": "device", 39 "label": "ipcamera" 40 } 41 ], 42 "kits": [ 43 { 44 "type": "MountKit", 45 "nfs": "NfsServer", 46 "bin_file": "CppTestLite/KvStoreTest.bin" 47 } 48 ], 49 "driver": { 50 "type": "CppTestLite", 51 "xml-output": false, 52 "rerun": false 53 } 54 } 55 """ 56 57 def __init__(self, path_or_content): 58 """Instantiate the class using the manifest file denoted by path or 59 content 60 """ 61 self.config = Config() 62 self._do_parse(path_or_content) 63 64 def _do_parse(self, path_or_content): 65 try: 66 if path_or_content.find("{") != -1: 67 json_content = json.loads( 68 path_or_content, encoding="utf-8") 69 else: 70 if not os.path.exists(path_or_content): 71 raise ParamError(ErrorMessage.Common.Code_0101027.format(path_or_content)) 72 73 flags = os.O_RDONLY 74 modes = stat.S_IWUSR | stat.S_IRUSR 75 with os.fdopen(os.open(path_or_content, flags, modes), 76 "r", encoding="utf-8") as file_content: 77 json_content = json.load(file_content) 78 except (TypeError, ValueError, AttributeError) as error: 79 raise ParamError(ErrorMessage.Common.Code_0101028.format( 80 path_or_content, error), error_no="00111") from error 81 self._check_config(json_content) 82 # set self.config 83 self.config = Config() 84 self.config.description = json_content.get("description", "") 85 self.config.kits = json_content.get("kits", []) 86 self.config.environment = json_content.get("environment", []) 87 self.config.driver = json_content.get("driver", {}) 88 self.config.module_subsystem = json_content.get("subsystem", "") 89 self.config.module_part = json_content.get("part", "") 90 self.config.test_suite_name = json_content.get("testSuiteName", "") 91 self.config.test_case_list = json_content.get("testCaseList", []) 92 93 def _check_config(self, json_content): 94 for kit in json_content.get("kits", []): 95 self._check_type_key_exist("kits", kit) 96 for device in json_content.get("environment", []): 97 self._check_type_key_exist("environment", device) 98 if json_content.get("driver", {}): 99 self._check_type_key_exist("driver", json_content.get("driver")) 100 101 @classmethod 102 def _check_type_key_exist(cls, key, value): 103 if not isinstance(value, dict): 104 raise ParamError(ErrorMessage.Common.Code_0101029.format(value, key)) 105 if "type" not in value.keys(): 106 raise ParamError(ErrorMessage.Common.Code_0101030.format(value, key)) 107 108 def get_config(self): 109 return self.config 110 111 def get_description(self): 112 return getattr(self.config, "description", "") 113 114 def get_kits(self): 115 return getattr(self.config, "kits", []) 116 117 def get_environment(self): 118 return getattr(self.config, "environment", []) 119 120 def get_driver(self): 121 return getattr(self.config, "driver", {}) 122 123 def get_module_subsystem(self): 124 return getattr(self.config, "module_subsystem", "zzzzzzzz") 125 126 def get_module_part(self): 127 return getattr(self.config, "module_part", "") 128 129 def get_driver_type(self): 130 driver = getattr(self.config, "driver", {}) 131 return driver.get("type", "") if driver else "" 132 133 def get_test_suite_name(self): 134 return getattr(self.config, "testSuiteName", "AppTest") 135 136 def get_test_case_list(self): 137 return getattr(self.config, "test_case_list", []) 138