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('cl', True, self.template_dir, self.files) == 'true' assert CommandFormatter.format('cl', False, self.template_dir, self.files) == 'false'
def _run_one_section(self, section, kwargs): skip_else = False for i, command_dict in enumerate(section): if self.stop_flag: break for comm_type, comm in command_dict.items(): if comm_type.startswith('call'): # calling workflow: # 1) get proper run section (either from self or from snippet) # 2) if running snippet, add its files to kwargs['__files__'] # 3) actually run # 4) if running snippet, pop its files from kwargs['__files__'] sect = self._get_section_from_call(comm, 'run') if sect is None: logger.warning( 'Couldn\'t find section to run: {0}.'.format(comm)) continue if self._is_snippet_call(comm, **kwargs): # we're calling a snippet => add files and template_dir to kwargs snippet = yaml_snippet_loader.YamlSnippetLoader.get_snippet_by_name( comm.split('.')[0]) if '__files__' not in kwargs: kwargs['__files__'] = [] kwargs['__template_dir__'] = [] kwargs['__files__'].append(snippet.get_files_section()) kwargs['__template_dir__'].append( snippet.get_template_dir()) self._run_one_section(sect, copy.deepcopy(kwargs)) if self._is_snippet_call(comm, **kwargs): kwargs['__files__'].pop() kwargs['__template_dir__'].pop() elif comm_type.startswith('$'): # intentionally pass kwargs as dict, not as keywords try: self._assign_variable(comm_type, comm, kwargs) except exceptions.YamlSyntaxError as e: logger.error(e) raise e elif comm_type.startswith('if'): possible_else = None if len(section) > i + 1: # do we have "else" clause? possible_else = list(section[i + 1].items())[0] _, skip_else, to_run = self._get_section_from_condition( (comm_type, comm), possible_else, **kwargs) if to_run: # run with original kwargs, so that they might be changed for code after this if self._run_one_section(to_run, kwargs) elif comm_type == 'else': if not skip_else: logger.warning( 'Yaml error: encountered "else" with no associated "if", skipping.' ) skip_else = False elif comm_type.startswith('for'): # syntax: "for $i in $x: <section> or "for $i in cl_command: <section>" try: control_var, expression = self._parse_for(comm_type) except exceptions.YamlSyntaxError as e: logger.error(e) raise e try: eval_expression = evaluate_expression() except exceptions.YamlSyntaxError as e: logger.log(e) raise e for i in eval_expression: kwargs[control_var] = i self._run_one_section(comm, kwargs) elif comm_type.startswith('scl'): if '__scls__' not in kwargs: kwargs['__scls__'] = [] # list of lists of scl names kwargs['__scls__'].append(comm_type.split()[1:]) self._run_one_section(comm, kwargs) kwargs['__scls__'].pop() else: files = kwargs['__files__'][-1] if kwargs.get( '__files__', None) else self._files template_dir = kwargs['__template_dir__'][-1] if kwargs.get( '__template_dir__', None) else self.template_dir run_command( comm_type, CommandFormatter.format(comm_type, comm, template_dir, files, **kwargs), **kwargs)
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("cl", True, self.template_dir, self.files) == "true" assert CommandFormatter.format("cl", False, self.template_dir, self.files) == "false"
def test_format(self, comm, arg_dict, result): assert CommandFormatter.format("cl", comm, self.template_dir, self.files, **arg_dict) == result
def test_format(self, comm, arg_dict, result): assert CommandFormatter.format('cl', comm, self.template_dir, self.files, **arg_dict) == result
def _run_one_section(self, section, kwargs): skip_else = False for i, command_dict in enumerate(section): if self.stop_flag: break for comm_type, comm in command_dict.items(): if comm_type.startswith('call'): # calling workflow: # 1) get proper run section (either from self or from snippet) # 2) if running snippet, add its files to kwargs['__files__'] # 3) actually run # 4) if running snippet, pop its files from kwargs['__files__'] sect = self._get_section_from_call(comm, 'run') if sect is None: logger.warning('Couldn\'t find section to run: {0}.'.format(comm)) continue if self._is_snippet_call(comm, **kwargs): # we're calling a snippet => add files and template_dir to kwargs snippet = yaml_snippet_loader.YamlSnippetLoader.get_snippet_by_name(comm.split('.')[0]) if '__files__' not in kwargs: kwargs['__files__'] = [] kwargs['__template_dir__'] = [] kwargs['__files__'].append(snippet.get_files_section()) kwargs['__template_dir__'].append(snippet.get_template_dir()) self._run_one_section(sect, copy.deepcopy(kwargs)) if self._is_snippet_call(comm, **kwargs): kwargs['__files__'].pop() kwargs['__template_dir__'].pop() elif comm_type.startswith('$'): # intentionally pass kwargs as dict, not as keywords try: self._assign_variable(comm_type, comm, kwargs) except exceptions.YamlSyntaxError as e: logger.error(e) raise e elif comm_type.startswith('if'): possible_else = None if len(section) > i + 1: # do we have "else" clause? possible_else = list(section[i + 1].items())[0] _, skip_else, to_run = self._get_section_from_condition((comm_type, comm), possible_else, **kwargs) if to_run: # run with original kwargs, so that they might be changed for code after this if self._run_one_section(to_run, kwargs) elif comm_type == 'else': if not skip_else: logger.warning('Yaml error: encountered "else" with no associated "if", skipping.') skip_else = False elif comm_type.startswith('for'): # syntax: "for $i in $x: <section> or "for $i in cl_command: <section>" try: control_var, expression = self._parse_for(comm_type) except exceptions.YamlSyntaxError as e: logger.error(e) raise e try: eval_expression = evaluate_expression() except exceptions.YamlSyntaxError as e: logger.log(e) raise e for i in eval_expression: kwargs[control_var] = i self._run_one_section(comm, kwargs) elif comm_type.startswith('scl'): if '__scls__' not in kwargs: kwargs['__scls__'] = [] # list of lists of scl names kwargs['__scls__'].append(comm_type.split()[1:]) self._run_one_section(comm, kwargs) kwargs['__scls__'].pop() else: files = kwargs['__files__'][-1] if kwargs.get('__files__', None) else self._files template_dir = kwargs['__template_dir__'][-1] if kwargs.get('__template_dir__', None) else self.template_dir run_command(comm_type, CommandFormatter.format(comm_type, comm, template_dir, files, **kwargs), **kwargs)