'Export the workflow contents to a dictionary ready for JSON-ification and export.'

def _workflow_to_dict_export(self, trans, stored=None, workflow=None):

    if (workflow is None):
        assert (stored is not None)
        workflow = stored.latest_workflow
    annotation_str = ''
    if (stored is not None):
        workflow_annotation = self.get_item_annotation_obj(trans.sa_session, trans.user, stored)
        if workflow_annotation:
            annotation_str = workflow_annotation.annotation
    data = {}
    data['a_galaxy_workflow'] = 'true'
    data['format-version'] = '0.1'
    data['name'] = workflow.name
    data['annotation'] = annotation_str
    if (workflow.uuid is not None):
        data['uuid'] = str(workflow.uuid)
    data['steps'] = {}
    for step in workflow.steps:
        module = module_factory.from_workflow_step(trans, step)
        if (not module):
            return None
        step_annotation = self.get_item_annotation_obj(trans.sa_session, trans.user, step)
        annotation_str = ''
        if step_annotation:
            annotation_str = step_annotation.annotation
        content_id = module.get_content_id()
        if (module.type == 'tool'):
            tool_state = module.get_state(nested=False)
        else:
            tool_state = module.state.inputs
        step_dict = {'id': step.order_index, 'type': module.type, 'content_id': content_id, 'tool_id': content_id, 'tool_version': step.tool_version, 'name': module.get_name(), 'tool_state': json.dumps(tool_state), 'errors': module.get_errors(), 'uuid': str(step.uuid), 'label': (step.label or None), 'annotation': annotation_str}
        if (module.type == 'tool'):
            if module.tool.tool_shed_repository:
                tsr = module.tool.tool_shed_repository
                step_dict['tool_shed_repository'] = {'name': tsr.name, 'owner': tsr.owner, 'changeset_revision': tsr.changeset_revision, 'tool_shed': tsr.tool_shed}
            pja_dict = {}
            for pja in step.post_job_actions:
                pja_dict[(pja.action_type + pja.output_name)] = dict(action_type=pja.action_type, output_name=pja.output_name, action_arguments=pja.action_arguments)
            step_dict['post_job_actions'] = pja_dict
        if (module.type == 'subworkflow'):
            del step_dict['content_id']
            del step_dict['errors']
            del step_dict['tool_version']
            del step_dict['tool_state']
            subworkflow = step.subworkflow
            subworkflow_as_dict = self._workflow_to_dict_export(trans, stored=None, workflow=subworkflow)
            step_dict['subworkflow'] = subworkflow_as_dict
        input_dicts = []
        step_state = (module.state.inputs or {})
        if (('name' in step_state) and (module.type != 'tool')):
            name = step_state.get('name')
            input_dicts.append({'name': name, 'description': annotation_str})
        for (name, val) in step_state.items():
            input_type = type(val)
            if (input_type == RuntimeValue):
                input_dicts.append({'name': name, 'description': ('runtime    parameter    for    tool    %s' % module.get_name())})
            elif (input_type == dict):
                for partval in val.values():
                    if (type(partval) == RuntimeValue):
                        input_dicts.append({'name': name, 'description': ('runtime    parameter    for    tool    %s' % module.get_name())})
        step_dict['inputs'] = input_dicts
        workflow_outputs_dicts = []
        for workflow_output in step.unique_workflow_outputs:
            workflow_output_dict = dict(output_name=workflow_output.output_name, label=workflow_output.label, uuid=(str(workflow_output.uuid) if (workflow_output.uuid is not None) else None))
            workflow_outputs_dicts.append(workflow_output_dict)
        step_dict['workflow_outputs'] = workflow_outputs_dicts
        step_dict['outputs'] = []
        if (type(module) is ToolModule):
            for output in module.get_data_outputs():
                step_dict['outputs'].append({'name': output['name'], 'type': output['extensions'][0]})
        input_connections = step.input_connections
        if ((step.type is None) or (step.type == 'tool')):
            data_input_names = {}
            def callback(input, prefixed_name, **kwargs):
                if (isinstance(input, DataToolParameter) or isinstance(input, DataCollectionToolParameter)):
                    data_input_names[prefixed_name] = True
            module.check_and_update_state()
            visit_input_values(module.tool.inputs, module.state.inputs, callback)
            input_connections = [conn for conn in input_connections if ((conn.input_name in data_input_names) or conn.non_data_connection)]
        input_conn_dict = {}
        unique_input_names = set([conn.input_name for conn in input_connections])
        for input_name in unique_input_names:
            input_conn_dicts = []
            for conn in input_connections:
                if (conn.input_name != input_name):
                    continue
                input_conn = dict(id=conn.output_step.order_index, output_name=conn.output_name)
                if (conn.input_subworkflow_step is not None):
                    subworkflow_step_id = conn.input_subworkflow_step.order_index
                    input_conn['input_subworkflow_step_id'] = subworkflow_step_id
                input_conn_dicts.append(input_conn)
            input_conn_dict[input_name] = input_conn_dicts
        for (input_name, input_conn) in dict(input_conn_dict).iteritems():
            if (len(input_conn) == 1):
                input_conn_dict[input_name] = input_conn[0]
        step_dict['input_connections'] = input_conn_dict
        step_dict['position'] = step.position
        data['steps'][step.order_index] = step_dict
    return data
