Пример #1
0
    def __init__(self, workflow_name, step_name=None, dynamic_workflow_steps=None):
        """
        Initializes a workflow.

        Use to workflow name and step name to find the workflow and step in the WORKFLOWS

        If no step name is specified, the step is set to the start step of the workflow

        :param workflow_name: Name of the workflow
        :param step_name: Name of the step within the workflow, default: start step
        """
        self._workflow_name = workflow_name
        self._step_name = step_name
        self._workflow_changed = False

        if dynamic_workflow_steps:
            workflow = self._build_dynamic_workflow(dynamic_workflow_steps)
        else:
            workflow = WorkflowTreeNode.from_dict(WORKFLOWS[self._workflow_name])

        self._step = workflow if step_name is None else workflow.get_node(step_name)

        if not self._step:
            # Workflow has changed. Step name is no longer in the workflow. Set _workflow_changed flag. We should
            # run the first step instead of the next in handle_result.
            self._workflow_changed = True
            self._step = workflow
Пример #2
0
    def _build_dynamic_workflow(self, workflow_steps: list):
        """workflow_steps example:

        [
            {
                'type': 'workflow',
                'workflow': IMPORT,
                'header': {
                    'catalogue': 'gebieden',
                    'collection': 'stadsdelen',
                    'application': 'DGDialog',
                }
            },
            {
                'type': 'workflow',
                'workflow': RELATE,
                'header': {
                    'catalogue': 'gebieden',
                    'collection': 'stadsdelen',
                    'attribute': 'ligt_in_wijk'
                }
            },
        ]

        :param workflow_steps:
        :return:
        """

        workflow = None

        for i, step in enumerate(workflow_steps):
            if step['type'] == 'workflow':
                new_step = WorkflowTreeNode.from_dict(WORKFLOWS[step['workflow']])

            elif step['type'] == 'workflow_step':
                new_step = WorkflowTreeNode(
                    name=step['step_name'],
                    function=lambda msg, step_name=step['step_name']: start_step(step_name, msg)
                )

            else:
                raise NotImplementedError

            new_step.append_to_names(str(i))
            new_step.set_header_parameters(step.get('header', {}))

            if workflow:
                for leaf in workflow.get_leafs():
                    leaf.append_node(new_step)
            else:
                workflow = new_step

        return workflow
Пример #3
0
    def test_from_dict(self, mock_next_step):
        workflow = {
            START: 'step1',
            'step1': {
                'function': 'the function',
                'next': [{'step': 'stuff'}]
            },
        }

        result = WorkflowTreeNode.from_dict(workflow)
        self.assertEqual('step1', result.name)
        self.assertEqual('the function', result.function)
        self.assertEqual([mock_next_step.from_dict.return_value], result.next)

        mock_next_step.from_dict.assert_called_with(workflow, {'step': 'stuff'})

        # Should yield same result
        result2 = WorkflowTreeNode.from_dict(workflow, 'step1')
        self.assertEqual(result.name, result2.name)
        self.assertEqual(result.function, result2.function)
        self.assertEqual(result.next, result2.next)
Пример #4
0
    def test_from_dict_jumping_workflows(self, mock_next_step, mock_get_workflow):
        workflow = {
            START: 'step1',
            'step1': {
                'function': 'the function',
                'next': [{
                    'workflow': 'other_workflow',
                    'step': 'stuff'
                }]
            },
        }

        result = WorkflowTreeNode.from_dict(workflow)
        self.assertEqual('step1', result.name)
        self.assertEqual('the function', result.function)
        self.assertEqual([mock_next_step.from_dict.return_value], result.next)

        mock_next_step.from_dict.assert_called_with(mock_get_workflow.return_value, {'workflow': 'other_workflow', 'step': 'stuff'})
        mock_get_workflow.assert_called_with('other_workflow')