18 from typing
import Any, List, Optional, Tuple, Dict
22 from ..base_list_parameter
import BaseListParameter
23 from ..utils
import validateEnumStructure
24 from ....project
import ProjectType
27 class ListEnumParameter(BaseListParameter[Dict[str, Any]]):
30 def types(self) -> List[type]:
34 def listTypes(self) -> List[type]:
37 def validate(self) -> Tuple[bool, Optional[str]]:
38 isValid, message = validateEnumStructure(self.name, self.value, self.required)
40 return isValid, message
43 value: Dict[str, Any] = self.value
45 selected = value[
"selected"]
46 options = value[
"options"]
48 if selected
is None and not self.required:
51 if not isinstance(selected, list):
52 return False, f
"Enum list parameter \"{self.name}.selected\" has invalid type. Expected \"list[int]\", got \"{type(selected).__name__}\""
54 if not all(type(element)
is int
for element
in selected):
55 elementTypes =
", ".join({type(element).__name__
for element
in selected})
56 return False, f
"Enum list parameter \"{self.name}.selected\" has invalid type. Expected \"list[int]\", got \"list[{elementTypes}]\""
58 invalidIndxCount = len([element
for element
in selected
if element >= len(options)
or element < 0])
59 if invalidIndxCount > 0:
60 return False, f
"Enum list parameter \"{self.name}.selected\" has out of range values"
64 def parseValue(self, type_: ProjectType) -> Optional[Any]:
65 if self.value
is None:
68 selected: Optional[List[int]] = self.value[
"selected"]
69 options: List[str] = self.value[
"options"]
74 return [options[value]
for value
in selected]
76 def overrideValue(self, values: Optional[Any]) -> Optional[Any]:
77 if values
is None or self.value
is None:
81 parsedValue: Dict[str, Any] = self.value
82 parsedValue[
"selected"] = []
84 for value
in values.split(
" "):
85 parsedValue[
"selected"].append(int(value))
88 except ValueError
as e:
89 logging.getLogger(
"coretexpylib").warning(f
">> [Coretex] Failed to override list[enum] parameter \"{self.name}\". | {e}")