Ejemplo n.º 1
0
    def text(self):
        message = self._event['message']['text']
        if self.is_log_message:
            message = u'{0}: {1}'.format(self.log_level, message)
        elif (self.event_type in ('task_rescheduled', 'task_failed')):
            causes = self._event['context'].get('task_error_causes', [])
            if causes:
                multiple_causes = len(causes) > 1
                causes_out = StringIO()
                if multiple_causes:
                    causes_out.write('Causes (most recent cause last):\n')
                for cause in causes:
                    if multiple_causes:
                        causes_out.write('{0}\n'.format('-' * 32))
                    causes_out.write(cause.get('traceback', ''))

                message = u'{0}\n{1}'.format(message, causes_out.getvalue())
        return message
Ejemplo n.º 2
0
def _merge_and_validate_execution_parameters(workflow,
                                             workflow_name,
                                             execution_parameters=None,
                                             allow_custom_parameters=False):

    merged_parameters = {}
    workflow_parameters = workflow.get('parameters', {})
    execution_parameters = execution_parameters or {}

    missing_mandatory_parameters = set()

    allowed_types = {
        'integer': int,
        'float': float,
        'string': (text_type, bytes),
        'boolean': bool
    }
    wrong_types = {}

    for name, param in workflow_parameters.items():

        if 'type' in param and name in execution_parameters:

            # check if need to convert from string
            if (isinstance(execution_parameters[name], (text_type, bytes))
                    and param['type'] in allowed_types
                    and param['type'] != 'string'):
                execution_parameters[name] = \
                    _try_convert_from_str(
                        execution_parameters[name],
                        allowed_types[param['type']])

            # validate type
            if not isinstance(execution_parameters[name],
                              allowed_types.get(param['type'], object)):
                wrong_types[name] = param['type']

        if 'default' not in param:
            if name not in execution_parameters:
                missing_mandatory_parameters.add(name)
                continue
            merged_parameters[name] = execution_parameters[name]
        else:
            merged_parameters[name] = execution_parameters[name] if \
                name in execution_parameters else param['default']

    if missing_mandatory_parameters:
        raise ValueError('Workflow "{0}" must be provided with the following '
                         'parameters to execute: {1}'.format(
                             workflow_name,
                             ','.join(missing_mandatory_parameters)))

    if wrong_types:
        error_message = StringIO()
        for param_name, param_type in wrong_types.items():
            error_message.write('Parameter "{0}" must be of type {1}\n'.format(
                param_name, param_type))
        raise ValueError(error_message.getvalue())

    custom_parameters = dict((k, v) for (k, v) in execution_parameters.items()
                             if k not in workflow_parameters)

    if not allow_custom_parameters and custom_parameters:
        raise ValueError(
            'Workflow "{0}" does not have the following parameters '
            'declared: {1}. Remove these parameters or use '
            'the flag for allowing custom parameters'.format(
                workflow_name, ','.join(custom_parameters)))

    merged_parameters.update(custom_parameters)
    return merged_parameters