1#!/usr/bin/env python3 2# -- coding: utf-8 -- 3# 4# Copyright (c) 2024-2025 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18from functools import cached_property 19from pathlib import Path 20from typing import Any, cast 21 22from runner import utils 23from runner.common_exceptions import InvalidConfiguration 24from runner.logger import Log 25from runner.options.macros import Macros, ParameterNotFound 26from runner.options.options import IOptions 27 28_LOGGER = Log.get_logger(__file__) 29 30 31class CollectionsOptions(IOptions): 32 __TEST_ROOT = "test-root" 33 __LIST_ROOT = "list-root" 34 __PARAMETERS = "parameters" 35 __GENERATOR_CLASS = "generator-class" 36 __GENERATOR_SCRIPT = "generator-script" 37 __GENERATOR_OPTIONS = "generator-options" 38 __EXCLUDE = "exclude" 39 40 def __init__(self, name: str, args: dict[str, Any], parent: IOptions): # type: ignore[explicit-any] 41 super().__init__(None) 42 self.__name = name 43 self._parent: IOptions = parent 44 self.__args: dict[str, Any] = args # type: ignore[explicit-any] 45 self.__test_root = self.__get_arg(self.__TEST_ROOT) 46 self.__list_root = self.__get_arg(self.__LIST_ROOT) 47 self.__exclude: list[str] = args[self.__EXCLUDE] if args and self.__EXCLUDE in args else [] 48 self.__parameters: dict[str, Any] = ( # type: ignore[explicit-any] 49 args)[self.__PARAMETERS] if args and self.__PARAMETERS in args else {} 50 self.__expand_macros_in_parameters() 51 52 def __str__(self) -> str: 53 return f"{self.__name}: {self._to_str(indent=3)}" 54 55 @cached_property 56 def name(self) -> str: 57 return self.__name 58 59 @cached_property 60 def test_root(self) -> Path: 61 return Path(self.__test_root) 62 63 @cached_property 64 def list_root(self) -> Path: 65 return Path(self.__list_root) 66 67 @cached_property 68 def generator_class(self) -> str | None: 69 return self.__get_from_args(self.__GENERATOR_CLASS) 70 71 @cached_property 72 def generator_script(self) -> str | None: 73 return self.__get_from_args(self.__GENERATOR_SCRIPT) 74 75 @cached_property 76 def generator_options(self) -> list[str]: 77 default_options: list[str] = [] 78 return cast(list[str], self.__get_from_args(self.__GENERATOR_CLASS, default_options)) 79 80 @cached_property 81 def exclude(self) -> list[str]: 82 return self.__exclude 83 84 @cached_property 85 def parameters(self) -> dict[str, Any]: # type: ignore[explicit-any] 86 return self.__parameters 87 88 def get_parameter(self, key: str, default: Any | None = None) -> Any | None: # type: ignore[explicit-any] 89 return self.__parameters.get(key, default) 90 91 def __get_from_args(self, key: str, default_value: Any | None = None) -> Any | None: # type: ignore[explicit-any] 92 return self.__args[key] if self.__args and key in self.__args else default_value 93 94 def __get_arg(self, prop_name_minused: str) -> str: 95 result = "" 96 prop_name_underscored = utils.convert_minus(prop_name_minused) 97 if self.__args and prop_name_minused in self.__args: 98 result = cast(str, Macros.correct_macro(self.__args[prop_name_minused], self._parent)) 99 elif prop_name_underscored in self._parent.properties(): 100 result = cast(str, self._parent.get_value(prop_name_underscored)) 101 result = cast(str, Macros.correct_macro(result, self._parent)) 102 elif self.__PARAMETERS in self._parent.properties(): 103 params = cast(dict, self._parent.get_value(self.__PARAMETERS)) 104 if prop_name_minused in params: 105 result = cast(str, Macros.correct_macro(params[prop_name_minused], self._parent)) 106 else: 107 raise InvalidConfiguration(f"Unknown key {prop_name_minused}") 108 return result 109 110 def __expand_macros_in_parameters(self) -> None: 111 for param_name, param_value in self.__parameters.items(): 112 if isinstance(param_value, str): 113 try: 114 self.__parameters[param_name] = Macros.correct_macro(param_value, self) 115 except ParameterNotFound: 116 pass 117