|
12 | 12 |
|
13 | 13 |
|
14 | 14 | def get_extra_params_fields(extra_params_validation_schema: dict) -> dict: |
15 | | - """ Loops over the "extra_params" section of a validation_schema dict and creates a dictionary of |
16 | | - field to aeonlib field_class to place into the template |
| 15 | + """Loops over the "extra_params" section of a validation_schema dict and creates a dictionary of |
| 16 | + field to aeonlib field_class to place into the template |
17 | 17 | """ |
18 | 18 | fields = {} |
19 | 19 | for field, properties in extra_params_validation_schema.items(): |
20 | | - field_class = '' |
| 20 | + field_class = "" |
21 | 21 | # If a set of allowed values is present, use that to make a Literal unless this is a boolean variable |
22 | | - if 'allowed' in properties and properties.get('type') != 'boolean': |
23 | | - allowed_values = [f'"{val}"' if properties['type'] == 'string' else val for val in properties['allowed']] |
| 22 | + if "allowed" in properties and properties.get("type") != "boolean": |
| 23 | + allowed_values = [ |
| 24 | + f'"{val}"' if properties["type"] == "string" else val |
| 25 | + for val in properties["allowed"] |
| 26 | + ] |
24 | 27 | field_class += f"Literal[{', '.join(allowed_values)}]" |
25 | 28 | else: |
26 | 29 | # Otherwise form an Annotated field based on its datatype, with min/max validation if present |
27 | 30 | field_class += "Annotated[" |
28 | | - match properties['type']: |
29 | | - case 'string': |
| 31 | + match properties["type"]: |
| 32 | + case "string": |
30 | 33 | field_class += "str" |
31 | | - case 'integer': |
| 34 | + case "integer": |
32 | 35 | field_class += "int" |
33 | | - case 'float': |
| 36 | + case "float": |
34 | 37 | field_class += "float" |
35 | | - case 'boolean': |
| 38 | + case "boolean": |
36 | 39 | field_class += "bool" |
37 | | - if 'min' in properties: |
| 40 | + if "min" in properties: |
38 | 41 | field_class += f", Ge({properties['min']})" |
39 | | - if 'max' in properties: |
| 42 | + if "max" in properties: |
40 | 43 | field_class += f", Le({properties['max']})" |
41 | 44 | # Add description to Annotated field. Annotated fields must have at least 2 properties. |
42 | | - field_class += f', "{properties.get('description', "")}"]' |
43 | | - if not properties.get('required', False) and 'default' not in properties: |
| 45 | + field_class += f', "{properties.get("description", "")}"]' |
| 46 | + if not properties.get("required", False) and "default" not in properties: |
44 | 47 | # The field is considered optional if it doesn't have a default or required is set to True |
45 | 48 | field_class += " | None = None" |
46 | | - elif 'default' in properties: |
| 49 | + elif "default" in properties: |
47 | 50 | # If a default value is present, provide it |
48 | | - default = f'"{properties['default']}"' if properties['type'] == 'string' else properties['default'] |
| 51 | + default = ( |
| 52 | + f'"{properties["default"]}"' |
| 53 | + if properties["type"] == "string" |
| 54 | + else properties["default"] |
| 55 | + ) |
49 | 56 | field_class += f" = {default}" |
50 | 57 | fields[field] = field_class |
51 | 58 | return fields |
@@ -124,8 +131,17 @@ def generate_instrument_configs(ins_s: str, facility: str) -> str: |
124 | 131 | k.rstrip("s"): v |
125 | 132 | for k, v in ins["optical_elements"].items() |
126 | 133 | }, |
127 | | - "configuration_extra_params": get_extra_params_fields(ins['validation_schema'].get('extra_params', {}).get('schema', {})), |
128 | | - "instrument_config_extra_params": get_extra_params_fields(ins['validation_schema'].get('instrument_configs', {}).get('schema', {}).get('schema', {}).get('extra_params', {}).get('schema', {})) |
| 134 | + "configuration_extra_params": get_extra_params_fields( |
| 135 | + ins["validation_schema"].get("extra_params", {}).get("schema", {}) |
| 136 | + ), |
| 137 | + "instrument_config_extra_params": get_extra_params_fields( |
| 138 | + ins["validation_schema"] |
| 139 | + .get("instrument_configs", {}) |
| 140 | + .get("schema", {}) |
| 141 | + .get("schema", {}) |
| 142 | + .get("extra_params", {}) |
| 143 | + .get("schema", {}) |
| 144 | + ), |
129 | 145 | } |
130 | 146 | ) |
131 | 147 |
|
|
0 commit comments