Coretex
parameter_factory.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from typing import Dict, Any
19 
20 import inflection
21 
22 from .utils import getValueParamType
23 from .base_parameter import BaseParameter
24 from .parameter_type import ParameterType
25 from .parameters import *
26 from ...secret import AWSSecret, GitSecret, CredentialsSecret
27 
28 
29 def cleanParamDict(value: Dict[str, Any]) -> Dict[str, Any]:
30  if not "data_type" in value:
31  value["data_type"] = None
32 
33  if not "value" in value:
34  value["value"] = None
35 
36  if not "required" in value:
37  value["required"] = False
38 
39  if not "description" in value:
40  value["description"] = ""
41 
42  dataName: str = value["name"]
43  dataType = value["data_type"]
44  dataValue = value["value"]
45 
46  if dataType is None and dataValue is None:
47  value["data_type"] = ParameterType.string.value
48 
49  if dataType is None and dataValue is not None:
50  value["data_type"] = getValueParamType(dataValue, dataName).value
51 
52  return value
53 
54 def create(value: Dict[str, Any]) -> BaseParameter:
55  cleanParamDict(value)
56  dataType = value.get("data_type")
57 
58  if not isinstance(dataType, str):
59  raise ValueError("\"data_type\" is not of type \"str\"")
60 
61  # Convert snake_case keys into camelCase
62  value = {
63  inflection.camelize(key, uppercase_first_letter = False): value
64  for key, value in value.items()
65  }
66 
67  parameterType = ParameterType(dataType)
68 
69  if parameterType == ParameterType.integer:
70  return IntParameter(**value)
71 
72  if parameterType == ParameterType.floatingPoint:
73  return FloatParameter(**value)
74 
75  if parameterType == ParameterType.string:
76  return StrParameter(**value)
77 
78  if parameterType == ParameterType.boolean:
79  return BoolParameter(**value)
80 
81  if parameterType == ParameterType.dataset:
82  return DatasetParameter(**value)
83 
84  if parameterType == ParameterType.model:
85  return ModelParameter(**value)
86 
87  if parameterType == ParameterType.imuVectors:
88  return IMUVectorsParameter(**value)
89 
90  if parameterType == ParameterType.enum:
91  return EnumParameter(**value)
92 
93  if parameterType == ParameterType.range:
94  return RangeParameter(**value)
95 
96  if parameterType == ParameterType.awsSecret:
97  return SecretParameter(AWSSecret, **value)
98 
99  if parameterType == ParameterType.gitSecret:
100  return SecretParameter(GitSecret, **value)
101 
102  if parameterType == ParameterType.credentialsSecret:
103  return SecretParameter(CredentialsSecret, **value)
104 
105  if parameterType == ParameterType.intList:
106  return ListIntParameter(**value)
107 
108  if parameterType == ParameterType.floatList:
109  return ListFloatParameter(**value)
110 
111  if parameterType == ParameterType.strList:
112  return ListStrParameter(**value)
113 
114  if parameterType == ParameterType.datasetList:
115  return ListDatasetParameter(**value)
116 
117  if parameterType == ParameterType.modelList:
118  return ListModelParameter(**value)
119 
120  if parameterType == ParameterType.enumList:
121  return ListEnumParameter(**value)
122 
123  if parameterType == ParameterType.awsSecretList:
124  return ListSecretParameter(AWSSecret, **value)
125 
126  if parameterType == ParameterType.gitSecretList:
127  return ListSecretParameter(GitSecret, **value)
128 
129  if parameterType == ParameterType.credentialsSecretList:
130  return ListSecretParameter(CredentialsSecret, **value)
131 
132  raise ValueError(f"Unknown parameter type \"{parameterType}\"")