예제 #1
0
    def test_resolve_dict_from_string(self):
        input = '${dict}'

        result = vars.resolve_variables(input, self.variables)

        assert type(result) is dict
        assert result['entry'] == 'value'
예제 #2
0
    def run_task(self, task_block):

        # Execute all commands in a task
        output = None
        for command in task_block:

            rawData = task_block[command]

            # Variable assignment
            variableMatch = re.search(
                vars.VariableMatcher.ONE_VARIABLE_ONLY_REGEX, command)
            if variableMatch:
                data = vars.resolve_variables(rawData, self.variables)
                self.variables[variableMatch.group(1)] = data

            # Execute handler
            elif command in self.command_handlers:
                output = self.run_command(self.command_handlers[command],
                                          rawData)

            # Unknown action
            else:
                raise YayException("Unknown action: {}".format(command))

        return output
예제 #3
0
    def test_resolve_variable_in_dict(self):
        input = {'entry': '${one}'}

        result = vars.resolve_variables(input, self.variables)

        assert type(result) is dict
        assert 'entry' in result
        assert result['entry'] == '1'
예제 #4
0
 def expand_for_each(self, for_each_command):
     for_each_command['steps'] = []
     set_variable_command = for_each_command['data'][0]['data']
     variable = list(set_variable_command.keys())[0]
     values = set_variable_command[variable]
     values = vars.resolve_variables(values, self.variables)
     for value in values:
         for_each_body = copy.deepcopy(for_each_command['data'])
         for_each_body[0]['data'][variable] = value
         for_each_command['steps'].extend(for_each_body)
예제 #5
0
파일: core.py 프로젝트: ndebuhr/yay
def execute_single_command(handler, rawData, variables):

    # Resolve variables
    # Don't resolve variables yet for Do or For each, etc. -- they will be resolved just in time
    if handler in delayed_variable_resolvers:
        data = rawData
    else:
        data = vars.resolve_variables(rawData, variables)

    # Execute action
    return handler(data, variables)
예제 #6
0
    def handle_until(self, until, output):
        if is_dict(until):
            until_copy = copy.deepcopy(until)
            until_copy = vars.resolve_variables(until_copy, self.variables)

            condition = conditions.parse_condition(until_copy)
            running = not condition.is_true()
        else:
            running = (output != until)

        return running
예제 #7
0
    def run_single_command(self, handler, rawData):

        # Resolve variables
        # Don't resolve variables yet for Do or For each, etc. -- they will be resolved just in time
        if handler.delayed_variable_resolver:
            data = rawData
        else:
            data = vars.resolve_variables(rawData, self.variables)

        # Execute action
        return handler.handler_method(data, self)
예제 #8
0
    def run_single_command(self, handler, rawData):

        # Execute control structures immediately
        if handler.delayed_variable_resolver:
            data = rawData
            return handler.handler_method(data, self)

        # Execute command handlers locally that are not loaded by default
        elif handler.command not in runtime.default_command_handlers:
            data = vars.resolve_variables(rawData, self.variables)
            return handler.handler_method(data, self)

        # Run remotely through Celery
        else:
            data = vars.resolve_variables(rawData, self.variables)
            celery_task = run_command_remotely.apply_async(
                (handler.command, data, self.variables), serializer='pickle')
            (result,
             self.variables) = celery_task.get(disable_sync_subtasks=False)
            return result
예제 #9
0
def for_each(data, context):
    variable_assignment = get_first_variable_assignment(data)

    output = []
    items = data[variable_assignment]
    items = vars.resolve_variables(items, context.variables)
    for value in as_list(items):
        data[variable_assignment] = value
        result = context.run_task({'Do': copy.deepcopy(data)})
        output.append(result)

    return output
예제 #10
0
    def test_resolve_in_list(self):
        input = [0, 'one', '${two}', '${dict}']

        result = vars.resolve_variables(input, self.variables)

        assert isinstance(result, list)
        assert len(result) == 4
        assert result[0] == 0
        assert result[1] == 'one'
        assert result[2] == '2'
        assert type(result[3]) is dict
        assert result[3]['entry'] == 'value'
예제 #11
0
파일: core.py 프로젝트: angel-git/yay
def invoke_single_handler(handler, rawData, variables):

    # Resolve variables
    data = vars.resolve_variables(rawData, variables)

    # Execute action
    result = handler(data, variables)

    # Store result
    if not result == None:
        variables[RESULT_VARIABLE] = result
    return result
예제 #12
0
파일: core.py 프로젝트: ndebuhr/yay
def if_statement(data, variables, break_on_success=False):
    actions = get_parameter(data, 'Do')

    del data['Do']
    data = vars.resolve_variables(data, variables)

    condition = parse_condition(data)

    if condition.is_true():
        execute_command(process_task, actions, variables)

        if break_on_success:
            raise FlowBreak()
예제 #13
0
def if_statement(data, context, break_on_success=False):

    cons = conditions.pop_conditions(data)
    cons = vars.resolve_variables(cons, context.variables)
    condition = conditions.parse_condition(cons)

    if condition.is_true():
        context.run_task({'Do': data})

        # context.run_task({'Do': actions})

        if break_on_success:
            raise FlowBreak()
예제 #14
0
파일: core.py 프로젝트: ndebuhr/yay
def repeat(data, variables):
    actions = get_parameter(data, 'Do')
    until = get_parameter(data, 'Until')

    finished = False
    while not finished:
        result = execute_command(do, actions, variables)

        if is_dict(until):
            until_copy = copy.deepcopy(until)
            until_copy = vars.resolve_variables(until_copy, variables)

            condition = parse_condition(until_copy)
            finished = condition.is_true()
        else:
            finished = (result == until)
예제 #15
0
def repeat(data, context):
    actions = get_parameter(data, 'Do')
    until = get_parameter(data, 'Until')

    finished = False
    while not finished:
        result = context.run_task({'Do': actions})

        if is_dict(until):
            until_copy = copy.deepcopy(until)
            until_copy = vars.resolve_variables(until_copy, context.variables)

            condition = conditions.parse_condition(until_copy)
            finished = condition.is_true()
        else:
            finished = (result == until)
예제 #16
0
    def run_step(self, step):

        command = step['command']

        # Handle flow control
        if 'Do' == command:
            self.run_next_step(step)
            return
        if 'For each' == command:
            self.expand_for_each(step)
            self.run_next_step(step)
            return
        if 'If' == command or 'If any' == command:
            data = vars.resolve_variables(step['data'], self.variables)
            condition = conditions.parse_condition(data)
            if condition.is_true():
                self.run_next_step(step)

                if 'If any' == command:
                    raise FlowBreak()
            return
        if 'Repeat' == command:
            step['steps'] = copy.deepcopy(step['data'])
            self.run_next_step(step)
            return

        # Unknown command
        if command not in self.command_handlers:
            raise YayException("Unknown action: {}".format(command))

        # Execute command
        output = self.run_single_command(self.command_handlers[command], step['data'])

        if output:
            step['output'] = output

        return output
예제 #17
0
파일: core.py 프로젝트: ndebuhr/yay
def foreach(data, variables):
    actions = get_parameter(data, 'Do')
    if len(data) != 2:
        raise YayException(
            "'For each' needs exactly two parameters: 'Do' and the name of the variable."
        )

    loop_variable = get_foreach_variable(data)

    items = data[loop_variable]
    items = vars.resolve_variables(items, variables)

    for item in items:
        stash = None
        if loop_variable in variables:
            stash = variables[loop_variable]
        variables[loop_variable] = item

        execute_command(do, actions, variables)

        if (stash):
            variables[loop_variable] = stash
        else:
            del variables[loop_variable]
예제 #18
0
파일: core.py 프로젝트: ndebuhr/yay
def process_task(task_block, variables={}):

    # Execute all commands in a task
    output = None
    for command in task_block:

        rawData = task_block[command]

        # Variable assignement
        variableMatch = re.search(vars.VariableMatcher.ONE_VARIABLE_ONLY_REGEX,
                                  command)
        if variableMatch:
            data = vars.resolve_variables(rawData, variables)
            variables[variableMatch.group(1)] = data

        # Execute handler
        elif command in handlers:
            output = execute_command(handlers[command], rawData, variables)

        # Unknown action
        else:
            raise YayException("Unknown action: {}".format(command))

    return output
예제 #19
0
    def test_resolve_json_path_index(self):
        input = '${list[0]}'

        result = vars.resolve_variables(input, self.variables)

        assert result == 0
예제 #20
0
    def test_unknown_json_path_returns_empty_list(self):
        input = '${dict.unknown}'

        result = vars.resolve_variables(input, self.variables)

        assert result == []
예제 #21
0
    def test_resolve_json_path_nested(self):
        input = 'My ${dict.nested.item}'

        result = vars.resolve_variables(input, self.variables)

        assert result == 'My stuff'
예제 #22
0
    def test_resolve_json_path(self):
        input = 'My ${dict.entry}'

        result = vars.resolve_variables(input, self.variables)

        assert result == 'My value'
예제 #23
0
파일: test_vars.py 프로젝트: ndebuhr/yay
    def test_resolve_unknow_json_path(self):
        input = '${dict.unknown}'

        result = vars.resolve_variables(input, self.variables)

        assert result == input
예제 #24
0
def to_live(data, context):
    if not is_raw(data):
        return data

    return vars.resolve_variables(live(data), context.variables)
예제 #25
0
    def test_resolve_two_variablea(self):
        input = '${one}${two}'

        result = vars.resolve_variables(input, self.variables)

        assert result == '12'
예제 #26
0
    def test_resolve_single_variable(self):
        input = '${one}'

        result = vars.resolve_variables(input, self.variables)

        assert result == '1'
예제 #27
0
    def test_resolve_another_variable(self):
        input = '${a}'

        result = vars.resolve_variables(input, self.variables)

        assert result == 'Would like to be 1'
예제 #28
0
def apply_variables(data, context):
    return raw(vars.resolve_variables(live(data), context.variables))