<s> import json <EOL> class PipelineDefinitionError ( Exception ) : <EOL> def __init__ ( self , msg , definition ) : <EOL> full_msg = ( <EOL> "<STR_LIT>" % msg ) <EOL> super ( PipelineDefinitionError , self ) . __init__ ( full_msg ) <EOL> self . msg = msg <EOL> self . definition = definition <EOL> def api_to_definition ( definition ) : <EOL> if '<STR_LIT>' in definition : <EOL> definition [ '<STR_LIT>' ] = _api_to_objects_definition ( <EOL> definition . pop ( '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' in definition : <EOL> definition [ '<STR_LIT>' ] = _api_to_parameters_definition ( <EOL> definition . pop ( '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' in definition : <EOL> definition [ '<STR_LIT>' ] = _api_to_values_definition ( <EOL> definition . pop ( '<STR_LIT>' ) ) <EOL> return definition <EOL> def definition_to_api_objects ( definition ) : <EOL> if '<STR_LIT>' not in definition : <EOL> raise PipelineDefinitionError ( '<STR_LIT>' , definition ) <EOL> api_elements = [ ] <EOL> for element in definition [ '<STR_LIT>' ] : <EOL> try : <EOL> element_id = element . pop ( '<STR_LIT:id>' ) <EOL> except KeyError : <EOL> raise PipelineDefinitionError ( '<STR_LIT>' % <EOL> json . dumps ( element ) , definition ) <EOL> api_object = { '<STR_LIT:id>' : element_id } <EOL> name = element . pop ( '<STR_LIT:name>' , element_id ) <EOL> api_object [ '<STR_LIT:name>' ] = name <EOL> fields = [ ] <EOL> for key , value in sorted ( element . items ( ) ) : <EOL> fields . extend ( _parse_each_field ( key , value ) ) <EOL> api_object [ '<STR_LIT>' ] = fields <EOL> api_elements . append ( api_object ) <EOL> return api_elements <EOL> def definition_to_api_parameters ( definition ) : <EOL> if '<STR_LIT>' not in definition : <EOL> return None <EOL> parameter_objects = [ ] <EOL> for element in definition [ '<STR_LIT>' ] : <EOL> try : <EOL> parameter_id = element . pop ( '<STR_LIT:id>' ) <EOL> except KeyError : <EOL> raise PipelineDefinitionError ( '<STR_LIT>' % <EOL> json . dumps ( element ) , definition ) <EOL> parameter_object = { '<STR_LIT:id>' : parameter_id } <EOL> attributes = [ ] <EOL> for key , value in sorted ( element . items ( ) ) : <EOL> attributes . extend ( _parse_each_field ( key , value ) ) <EOL> parameter_object [ '<STR_LIT>' ] = attributes <EOL> parameter_objects . append ( parameter_object ) <EOL> return parameter_objects <EOL> def definition_to_parameter_values ( definition ) : <EOL> if '<STR_LIT>' not in definition : <EOL> return None <EOL> parameter_values = [ ] <EOL> for key in definition [ '<STR_LIT>' ] : <EOL> parameter_values . extend ( <EOL> _convert_single_parameter_value ( key , definition [ '<STR_LIT>' ] [ key ] ) ) <EOL> return parameter_values <EOL> def _parse_each_field ( key , value ) : <EOL> values = [ ] <EOL> if isinstance ( value , list ) : <EOL> for item in value : <EOL> values . append ( _convert_single_field ( key , item ) ) <EOL> else : <EOL> values . append ( _convert_single_field ( key , value ) ) <EOL> return values <EOL> def _convert_single_field ( key , value ) : <EOL> field = { '<STR_LIT:key>' : key } <EOL> if isinstance ( value , dict ) and list ( value . keys ( ) ) == [ '<STR_LIT>' ] : <EOL> field [ '<STR_LIT>' ] = value [ '<STR_LIT>' ] <EOL> else : <EOL> field [ '<STR_LIT>' ] = value <EOL> return field <EOL> def _convert_single_parameter_value ( key , values ) : <EOL> parameter_values = [ ] <EOL> if isinstance ( values , list ) : <EOL> for each_value in values : <EOL> parameter_value = { '<STR_LIT:id>' : key , '<STR_LIT>' : each_value } <EOL> parameter_values . append ( parameter_value ) <EOL> else : <EOL> parameter_value = { '<STR_LIT:id>' : key , '<STR_LIT>' : values } <EOL> parameter_values . append ( parameter_value ) <EOL> return parameter_values <EOL> def _api_to_objects_definition ( api_response ) : <EOL> pipeline_objects = [ ] <EOL> for element in api_response : <EOL> current = { <EOL> '<STR_LIT:id>' : element [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:name>' : element [ '<STR_LIT:name>' ] <EOL> } <EOL> for field in element [ '<STR_LIT>' ] : <EOL> key = field [ '<STR_LIT:key>' ] <EOL> if '<STR_LIT>' in field : <EOL> value = field [ '<STR_LIT>' ] <EOL> else : <EOL> value = { '<STR_LIT>' : field [ '<STR_LIT>' ] } <EOL> _add_value ( key , value , current ) <EOL> pipeline_objects . append ( current ) <EOL> return pipeline_objects <EOL> def _api_to_parameters_definition ( api_response ) : <EOL> parameter_objects = [ ] <EOL> for element in api_response : <EOL> current = { <EOL> '<STR_LIT:id>' : element [ '<STR_LIT:id>' ] <EOL> } <EOL> for attribute in element [ '<STR_LIT>' ] : <EOL> _add_value ( attribute [ '<STR_LIT:key>' ] , attribute [ '<STR_LIT>' ] , current ) <EOL> parameter_objects . append ( current ) <EOL> return parameter_objects <EOL> def _api_to_values_definition ( api_response ) : <EOL> pipeline_values = { } <EOL> for element in api_response : <EOL> _add_value ( element [ '<STR_LIT:id>' ] , element [ '<STR_LIT>' ] , pipeline_values ) <EOL> return pipeline_values <EOL> def _add_value ( key , value , current_map ) : <EOL> if key not in current_map : <EOL> current_map [ key ] = value <EOL> elif isinstance ( current_map [ key ] , list ) : <EOL> current_map [ key ] . append ( value ) <EOL> else : <EOL> converted_list = [ current_map [ key ] , value ] <EOL> current_map [ key ] = converted_list </s>
