def get_sub_tasks(self): # ensure we just process this method once if self.sub_tasks: return self.sub_tasks if self.__preparation: pattern = r"{0}".format(urllib.unquote(self.__preparation.get('pattern'))) # ok, lets see what we can do if self.__preparation.get('filterFile'): filter_file = self.__preparation.get('filterFile') write_file = self.__preparation.get('writeFile') placeholder = self.__preparation.get('placeholder') copy = self.__preparation.get('copy') filter_files_found = [name for name in glob.glob(filter_file)] write_files_found = [name for name in glob.glob(write_file)] if len(filter_files_found) < 1: raise InvalidFileDirectory('Task - Files were not found matching {0}.'.format(filter_file)) logging.debug('Task - Found [%s] file(s) to filter using [%s].', str(len(filter_files_found)), str(filter_file)) if len(write_files_found) < 1: raise InvalidFileDirectory('Task - Files were not found matching {0}.'.format(write_file)) logging.debug('Task - Found [%s] file(s) to write using [%s].', str(len(write_files_found)), str(write_file)) # ok, it seems we have a pattern, so we should look inside each file if pattern: # look matching regex in each line of a file all_matching_lines = self.__find_in_files(filter_files_found, pattern) logging.debug('Task - Found [%s] matching result(s) using [%s].', str(len(all_matching_lines)), str(pattern)) # look matching regex in each line of each file for w_file in write_files_found: for line in filter(None, all_matching_lines): new_file = w_file if copy: # create a copy of the file with the same name plus new_file = self.__create_copy(w_file) # get commands to execute self.sub_tasks.append(self.__replace_in_commands(new_file, line)) # look in side the new file for the placeholder and replace it self.__replace_in_file(new_file, placeholder, line) else: # seems you just want to filter files, lets replace them in the commands for f_file in filter_files_found: self.sub_tasks.append(self.__replace_in_commands(f_file, f_file)) else: raise Exception('Task - Oops this shouldn\'t happen!') else: sub_task = SubTask(self) sub_task.add_commands(self.__commands) self.sub_tasks.append(sub_task) logging.debug('Task - This Task has been splitted in [%s] Sub Task(s).', str(len(self.sub_tasks))) return self.sub_tasks
def _decompose_task(self): """Decomposes the tasks into sub tasks, based on the requirements in the task percept""" self.sub_tasks = [SubTask(task_requirement=requirement, parent_task_name=self.name) for requirement in self.task_percept.requirements]
def __replace_in_commands(self, file, line=''): sub_task = SubTask(self) for command in self.__commands: # replace placeholder with actual created file sub_task.add_command(command.replace('$file', file).replace('$line', line)) return sub_task