def _cache_update_needed(self, course, taskid): """ :param course: a Course object :param taskid: a (valid) task id :raise InvalidNameException, TaskNotFoundException :return: True if an update of the cache is needed, False else """ if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) task_fs = self.get_task_fs(course.get_id(), taskid) if (course.get_id(), taskid) not in self._cache: return True try: last_update, __, __ = self._get_last_updates( course, taskid, task_fs, False) except: raise TaskNotFoundException() last_modif = self._cache[(course.get_id(), taskid)][1] for filename, mftime in last_update.items(): if filename not in last_modif or last_modif[filename] < mftime: return True return False
def update_task_descriptor_content(self, courseid, taskid, content, force_extension=None): """ Update the task descriptor with the dict in content :param course: the course id of the course :param taskid: the task id of the task :param content: the content to put in the task file :param force_extension: If None, save it the same format. Else, save with the given extension :raise InvalidNameException, TaskNotFoundException, TaskUnreadableException """ if not id_checker(courseid): raise InvalidNameException("Course with invalid name: " + courseid) if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) if force_extension is None: path_to_descriptor, _, descriptor_manager = self._get_task_descriptor_info( courseid, taskid) elif force_extension in self.get_available_task_file_extensions(): path_to_descriptor = os.path.join(self._tasks_directory, courseid, taskid, "task." + force_extension) descriptor_manager = self._task_file_managers[force_extension] else: raise TaskReaderNotFoundException() try: with codecs.open(path_to_descriptor, 'w', 'utf-8') as fd: fd.write(descriptor_manager.dump(content)) except: raise TaskNotFoundException()
def update_temporal_task_file(self, course, taskid, data): """ :param course: a Course object :param taskid: the task id of the task :param data: a Dict with the temporary data of the task to be stored :raise InvalidNameException, TaskReaderNotFoundException, TaskNotFoundException Create or Update a temporary task file that is used to store the task data that is required for some plugins """ if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) task_fs = self.get_task_fs(course.get_id(), taskid) task_file_manager = None try: for file_extension, file_manager in self._task_file_managers.items( ): if file_extension is "yaml": task_file_manager = file_manager except: raise TaskReaderNotFoundException() if task_file_manager: temporal_task_file_content = task_file_manager.dump(data) try: task_fs.put("task_temp.yaml", temporal_task_file_content) except: raise TaskNotFoundException()
def _cache_update_needed(self, course, taskid): """ :param course: a Course object :param taskid: a (valid) task id :raise InvalidNameException, TaskNotFoundException :return: True if an update of the cache is needed, False else """ if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) task_fs = self.get_task_fs(course.get_id(), taskid) if (course.get_id(), taskid) not in self._cache: return True try: descriptor_name = self._get_task_descriptor_info(course.get_id(), taskid)[0] last_update = {descriptor_name: task_fs.get_last_modification_time(descriptor_name)} translations_fs = task_fs.from_subfolder("$i18n") if translations_fs.exists(): for f in translations_fs.list(folders=False, files=True, recursive=False): lang = f[0:len(f) - 3] if translations_fs.exists(lang + ".mo"): last_update["$i18n/" + lang + ".mo"] = translations_fs.get_last_modification_time(lang + ".mo") except: raise TaskNotFoundException() last_modif = self._cache[(course.get_id(), taskid)][1] for filename, mftime in last_update.items(): if filename not in last_modif or last_modif[filename] < mftime: return True return False
def _cache_update_needed(self, course, taskid): """ :param course: a Course object :param taskid: a (valid) task id :raise InvalidNameException, TaskNotFoundException :return: True if an update of the cache is needed, False else """ if (course.get_id(), taskid) not in self._cache: return True try: last_update = os.stat(self._get_task_descriptor_info(course.get_id(), taskid)[0]).st_mtime except: raise TaskNotFoundException() if self._cache[(course.get_id(), taskid)][1] < last_update: return True
def _get_task_descriptor_info(self, courseid, taskid): """ :param courseid: the course id of the course :param taskid: the task id of the task :raise InvalidNameException, TaskNotFoundException :return: a tuple, containing: (descriptor filename, task file manager for the descriptor) """ if not id_checker(courseid): raise InvalidNameException("Course with invalid name: " + courseid) if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) task_fs = self.get_task_fs(courseid, taskid) for ext, task_file_manager in self._task_file_managers.items(): if task_fs.exists("task." + ext): return "task." + ext, task_file_manager raise TaskNotFoundException()
def delete_task(self, courseid, taskid): """ :param courseid: the course id of the course :param taskid: the task id of the task :raise InvalidNameException or CourseNotFoundException Erase the content of the task folder """ if not id_checker(courseid): raise InvalidNameException("Course with invalid name: " + courseid) if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) task_fs = self.get_task_fs(courseid, taskid) if not task_fs.exists(): raise TaskNotFoundException() task_fs.delete() get_course_logger(courseid).info("Task %s erased from the factory.", taskid)
def delete_task(self, courseid, taskid): """ :param courseid: the course id of the course :param taskid: the task id of the task :raise InvalidNameException or CourseNotFoundException Erase the content of the task folder """ if not id_checker(courseid): raise InvalidNameException("Course with invalid name: " + courseid) if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) task_directory = os.path.join(self._tasks_directory, courseid, taskid) if not os.path.exists(task_directory): raise TaskNotFoundException() shutil.rmtree(task_directory) get_course_logger(courseid).info("Task %s erased from the factory.", taskid)
def _get_task_descriptor_info(self, courseid, taskid): """ :param courseid: the course id of the course :param taskid: the task id of the task :raise InvalidNameException, TaskNotFoundException :return: a tuple, containing: (the path to the descriptor of the task, extension of the descriptor, task file manager for the descriptor) """ if not id_checker(courseid): raise InvalidNameException("Course with invalid name: " + courseid) if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) base_file = os.path.join(self._tasks_directory, courseid, taskid, "task") for ext, task_file_manager in self._task_file_managers.items(): if os.path.isfile(base_file + "." + ext): return base_file + "." + ext, ext, task_file_manager raise TaskNotFoundException()
def _cache_update_needed(self, course, taskid): """ :param course: a Course object :param taskid: a (valid) task id :raise InvalidNameException, TaskNotFoundException :return: True if an update of the cache is needed, False else """ if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) task_fs = self.get_task_fs(course.get_id(), taskid) if (course.get_id(), taskid) not in self._cache: return True try: last_update = task_fs.get_last_modification_time( self._get_task_descriptor_info(course.get_id(), taskid)[0]) except: raise TaskNotFoundException() if self._cache[(course.get_id(), taskid)][1] < last_update: return True
def update_task_descriptor_content(self, courseid, taskid, content, force_extension=None): """ Update the task descriptor with the dict in content :param courseid: the course id of the course :param taskid: the task id of the task :param content: the content to put in the task file :param force_extension: If None, save it the same format. Else, save with the given extension :raise InvalidNameException, TaskNotFoundException, TaskUnreadableException """ if not id_checker(courseid): raise InvalidNameException("Course with invalid name: " + courseid) if not id_checker(taskid): raise InvalidNameException("Task with invalid name: " + taskid) if force_extension is None: path_to_descriptor, descriptor_manager = self._get_task_descriptor_info( courseid, taskid) elif force_extension in self.get_available_task_file_extensions(): path_to_descriptor = "task." + force_extension descriptor_manager = self._task_file_managers[force_extension] else: raise TaskReaderNotFoundException() try: self.get_task_fs(courseid, taskid).put(path_to_descriptor, descriptor_manager.dump(content)) except: raise TaskNotFoundException() self._hook_manager.call_hook('task_updated', courseid=courseid, taskid=taskid, new_content=content)