18 from typing
import Dict, Optional, Any, Tuple, List, TypeVar, Generic, Union
19 from abc
import ABC, abstractmethod
23 from .parameter_type
import ParameterType
24 from ...project
import ProjectType
30 class BaseParameter(ABC, Generic[T]):
37 dataType: Union[str, ParameterType],
42 if isinstance(dataType, str):
43 dataType = ParameterType(dataType)
46 self.description = description
48 self.dataType = dataType
49 self.required = required
54 def types(self) -> List[type]:
57 def makeExceptionMessage(self) -> str:
58 expected = self.dataType.value
59 received = self.generateTypeDescription()
61 return f
"Parameter \"{self.name}\" has invalid type. Expected \"{expected}\", got \"{received}\""
63 def validate(self) -> Tuple[bool, Optional[str]]:
64 if not self.required
and self.value
is None:
69 if isinstance(self.value, bool)
and int
in self.types
and not bool
in self.types:
72 if not any(isinstance(self.value, dataType)
for dataType
in self.types):
77 def generateTypeDescription(self) -> str:
78 if not isinstance(self.value, list)
or self.value
is None:
79 return type(self.value).__name__
81 elementTypes =
", ".join({type(value).__name__
for value
in self.value})
82 return f
"list[{elementTypes}]"
84 def parseValue(self, type_: ProjectType) -> Optional[Any]:
87 def overrideValue(self, value: Optional[Any]) -> Optional[Any]:
90 def encode(self) -> Dict[str, Any]:
93 "description": self.description,
95 "data_type": self.dataType.value,
96 "required": self.required
100 def validateParameters(parameters: List[BaseParameter], verbose: bool =
True) -> Dict[str, Any]:
101 parameterValidationResults: Dict[str, bool] = {}
103 for parameter
in parameters:
104 isValid, message = parameter.validate()
107 message = parameter.makeExceptionMessage()
110 logging.getLogger(
"coretexpylib").fatal(f
">> [Coretex] {message}")
112 parameterValidationResults[parameter.name] = isValid
114 return parameterValidationResults