コード例 #1
0
 def test_format_handles_bool(self):
     # If command is false/true in yaml file, it gets coverted to False/True
     # which is bool object. format should handle this.
     assert CommandFormatter.format(True, self.template_dir,
                                    self.files) == 'true'
     assert CommandFormatter.format(False, self.template_dir,
                                    self.files) == 'false'
コード例 #2
0
ファイル: yaml_assistant.py プロジェクト: voxik/devassistant
    def _run_one_section(self, section, kwargs):
        execute_else = False

        for i, command_dict in enumerate(section):
            for comm_type, comm in command_dict.items():
                if comm_type.startswith('run'):
                    s = self._get_section_to_run(section=comm, kwargs_override=False, **kwargs)
                    # use copy of kwargs, so that local kwargs don't get modified
                    self._run_one_section(s, copy.deepcopy(kwargs))
                elif comm_type == 'snippet':
                    snippet, section_name = self._get_snippet_and_section_name(comm, **kwargs)
                    section = snippet.get_run_section(section_name) if snippet else None
                    if section:
                        # push and pop snippet's files into kwargs
                        if '__files__' in kwargs:
                            kwargs['__files__'].append(snippet.get_files_section())
                        else:
                            kwargs['__files__'] = [snippet.get_files_section()]
                        # use copy of kwargs, so that local kwargs don't get modified
                        self._run_one_section(section, copy.deepcopy(kwargs))
                        kwargs['__files__'].pop()
                    else:
                        logger.warning('Couldn\'t find run section "{0}", in snippet {1} skipping.'.format(section_name,
                                                                                                           comm.split('(')[0]))
                elif comm_type.startswith('$'):
                    # intentionally pass kwargs as dict, not as keywords
                    self._assign_variable(comm_type, comm, kwargs)
                elif comm_type.startswith('if'):
                    if self._evaluate(comm_type[2:].strip(), **kwargs):
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                    elif len(section) > i + 1:
                        next_section_dict = section[i + 1]
                        next_section_comm_type, next_section_comm = list(next_section_dict.items())[0]
                        if next_section_comm_type == 'else':
                            execute_else = True
                elif comm_type == 'else':
                    # else on its own means error, otherwise execute it
                    if not list(section[i - 1].items())[0][0].startswith('if'):
                        logger.warning('Yaml error: encountered "else" with no associated "if", skipping.')
                    elif execute_else:
                        execute_else = False
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                else:
                    files = kwargs['__files__'][-1] if kwargs.get('__files__', None) else self._files
                    run_command(comm_type, CommandFormatter.format(comm, self.template_dir, files, **kwargs), **kwargs)
コード例 #3
0
ファイル: yaml_assistant.py プロジェクト: voxik/devassistant
    def _evaluate(self, expression, **kwargs):
        result = True
        invert_result = False
        expr = expression.strip()
        if expr.startswith('not '):
            invert_result = True
            expr = expr[4:]

        if expr.startswith('$'):
            var_name = self._get_var_name(expr)
            result = kwargs.get(var_name, False)
        elif expr.startswith('defined '):
            result = self._get_var_name(expr[8:]) in kwargs
        else:
            try:
                result = run_command('cl', CommandFormatter.format(expr, self.template_dir, self._files, **kwargs), **kwargs)
            except exceptions.RunException:
                result = False

        return result if not invert_result else not result
コード例 #4
0
ファイル: yaml_assistant.py プロジェクト: voxik/devassistant
    def _evaluate(self, expression, **kwargs):
        result = True
        invert_result = False
        expr = expression.strip()
        if expr.startswith('not '):
            invert_result = True
            expr = expr[4:]

        if expr.startswith('$'):
            var_name = self._get_var_name(expr)
            result = kwargs.get(var_name, False)
        elif expr.startswith('defined '):
            result = self._get_var_name(expr[8:]) in kwargs
        else:
            try:
                result = run_command(
                    'cl',
                    CommandFormatter.format(expr, self.template_dir,
                                            self._files, **kwargs), **kwargs)
            except exceptions.RunException:
                result = False

        return result if not invert_result else not result
コード例 #5
0
 def test_format(self, comm, arg_dict, result):
     assert CommandFormatter.format(comm, self.template_dir, self.files,
                                    **arg_dict) == result
コード例 #6
0
ファイル: yaml_assistant.py プロジェクト: voxik/devassistant
    def _run_one_section(self, section, kwargs):
        execute_else = False

        for i, command_dict in enumerate(section):
            for comm_type, comm in command_dict.items():
                if comm_type.startswith('run'):
                    s = self._get_section_to_run(section=comm,
                                                 kwargs_override=False,
                                                 **kwargs)
                    # use copy of kwargs, so that local kwargs don't get modified
                    self._run_one_section(s, copy.deepcopy(kwargs))
                elif comm_type == 'snippet':
                    snippet, section_name = self._get_snippet_and_section_name(
                        comm, **kwargs)
                    section = snippet.get_run_section(
                        section_name) if snippet else None
                    if section:
                        # push and pop snippet's files into kwargs
                        if '__files__' in kwargs:
                            kwargs['__files__'].append(
                                snippet.get_files_section())
                        else:
                            kwargs['__files__'] = [snippet.get_files_section()]
                        # use copy of kwargs, so that local kwargs don't get modified
                        self._run_one_section(section, copy.deepcopy(kwargs))
                        kwargs['__files__'].pop()
                    else:
                        logger.warning(
                            'Couldn\'t find run section "{0}", in snippet {1} skipping.'
                            .format(section_name,
                                    comm.split('(')[0]))
                elif comm_type.startswith('$'):
                    # intentionally pass kwargs as dict, not as keywords
                    self._assign_variable(comm_type, comm, kwargs)
                elif comm_type.startswith('if'):
                    if self._evaluate(comm_type[2:].strip(), **kwargs):
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                    elif len(section) > i + 1:
                        next_section_dict = section[i + 1]
                        next_section_comm_type, next_section_comm = list(
                            next_section_dict.items())[0]
                        if next_section_comm_type == 'else':
                            execute_else = True
                elif comm_type == 'else':
                    # else on its own means error, otherwise execute it
                    if not list(section[i - 1].items())[0][0].startswith('if'):
                        logger.warning(
                            'Yaml error: encountered "else" with no associated "if", skipping.'
                        )
                    elif execute_else:
                        execute_else = False
                        # run with original kwargs, so that they might be changed for code after this if
                        self._run_one_section(comm, kwargs)
                else:
                    files = kwargs['__files__'][-1] if kwargs.get(
                        '__files__', None) else self._files
                    run_command(
                        comm_type,
                        CommandFormatter.format(comm, self.template_dir, files,
                                                **kwargs), **kwargs)
コード例 #7
0
 def test_format_handles_bool(self): 
     # If command is false/true in yaml file, it gets coverted to False/True 
     # which is bool object. format should handle this. 
     assert CommandFormatter.format(True, self.template_dir, self.files) == 'true' 
     assert CommandFormatter.format(False, self.template_dir, self.files) == 'false'
コード例 #8
0
 def test_format(self, comm, arg_dict, result):
     assert CommandFormatter.format(comm, self.template_dir, self.files, **arg_dict) == result