cirro.models.form_specification
1from typing import Dict, Any, List 2 3import jsonschema 4from cirro_api_client.v1.models import FormSchema 5 6 7def _get_fields_in_schema(schema: Dict, parent_path='') -> List['Parameter']: 8 # This breaks for more advanced json schema usages such as the if/then 9 fields = [] 10 11 for [field_key, field_spec] in schema.items(): 12 field_path = field_key if parent_path == '' else f'{parent_path}.{field_key}' 13 field = Parameter(field_key, field_spec, field_path) 14 fields.append(field) 15 16 if 'properties' in field_spec: 17 fields.extend(_get_fields_in_schema(field_spec['properties'], field_path)) 18 19 return fields 20 21 22class ParameterSpecification: 23 """ 24 Used to describe parameters used in a process (uses JSONSchema) 25 """ 26 def __init__(self, form_schema: FormSchema): 27 self._form_spec_raw: Dict = form_schema.form.additional_properties 28 self._form_spec_ui: Dict = form_schema.ui.additional_properties 29 self.form_spec = _get_fields_in_schema(self._form_spec_raw.get('properties') or {}) 30 31 def validate_params(self, params: Dict): 32 """ 33 Validates that the given parameters conforms to the specification 34 """ 35 try: 36 jsonschema.validate(instance=params, schema=self._form_spec_raw) 37 except jsonschema.ValidationError as e: 38 raise RuntimeError(f'Parameter at {e.json_path} error: {e.message}') from e 39 40 def print(self): 41 """ 42 Prints out a user-friendly view of the parameters 43 """ 44 print("Parameters:") 45 for field in self.form_spec: 46 tab_prefix = '\t' * (field.path.count('.') + 1) 47 print(tab_prefix + str(field)) 48 49 50class Parameter: 51 def __init__(self, key: str, spec: Dict[str, Any], path: str): 52 self.spec = spec 53 self.key = key 54 self.path = path 55 56 @property 57 def is_group(self): 58 return self.spec.get('type') == 'object' 59 60 def __str__(self): 61 display_value = '' 62 additional_data = '' 63 64 if self.spec.get('title'): 65 display_value += f'{self.spec.get("title")}' 66 additional_data += f'key={self.key}, ' 67 else: 68 display_value += self.key 69 70 for prop in ['default', 'type', 'enum', 'description']: 71 if prop_value := self.spec.get(prop): 72 additional_data += f'{prop}={prop_value}, ' 73 74 if additional_data and not self.is_group: 75 display_value += f' ({additional_data.rstrip(", ")})' 76 77 if self.is_group: 78 display_value += ' (Group)' 79 return display_value
class
ParameterSpecification:
23class ParameterSpecification: 24 """ 25 Used to describe parameters used in a process (uses JSONSchema) 26 """ 27 def __init__(self, form_schema: FormSchema): 28 self._form_spec_raw: Dict = form_schema.form.additional_properties 29 self._form_spec_ui: Dict = form_schema.ui.additional_properties 30 self.form_spec = _get_fields_in_schema(self._form_spec_raw.get('properties') or {}) 31 32 def validate_params(self, params: Dict): 33 """ 34 Validates that the given parameters conforms to the specification 35 """ 36 try: 37 jsonschema.validate(instance=params, schema=self._form_spec_raw) 38 except jsonschema.ValidationError as e: 39 raise RuntimeError(f'Parameter at {e.json_path} error: {e.message}') from e 40 41 def print(self): 42 """ 43 Prints out a user-friendly view of the parameters 44 """ 45 print("Parameters:") 46 for field in self.form_spec: 47 tab_prefix = '\t' * (field.path.count('.') + 1) 48 print(tab_prefix + str(field))
Used to describe parameters used in a process (uses JSONSchema)
ParameterSpecification(form_schema: cirro_api_client.v1.models.FormSchema)
def
validate_params(self, params: Dict):
32 def validate_params(self, params: Dict): 33 """ 34 Validates that the given parameters conforms to the specification 35 """ 36 try: 37 jsonschema.validate(instance=params, schema=self._form_spec_raw) 38 except jsonschema.ValidationError as e: 39 raise RuntimeError(f'Parameter at {e.json_path} error: {e.message}') from e
Validates that the given parameters conforms to the specification
class
Parameter:
51class Parameter: 52 def __init__(self, key: str, spec: Dict[str, Any], path: str): 53 self.spec = spec 54 self.key = key 55 self.path = path 56 57 @property 58 def is_group(self): 59 return self.spec.get('type') == 'object' 60 61 def __str__(self): 62 display_value = '' 63 additional_data = '' 64 65 if self.spec.get('title'): 66 display_value += f'{self.spec.get("title")}' 67 additional_data += f'key={self.key}, ' 68 else: 69 display_value += self.key 70 71 for prop in ['default', 'type', 'enum', 'description']: 72 if prop_value := self.spec.get(prop): 73 additional_data += f'{prop}={prop_value}, ' 74 75 if additional_data and not self.is_group: 76 display_value += f' ({additional_data.rstrip(", ")})' 77 78 if self.is_group: 79 display_value += ' (Group)' 80 return display_value