def reload_pla(directory, rel_path, original): """Reload the given file as a PLTP. Also reload its PL, but without modyfing their ID. original is a pltp returned by load_pltp or reload_pltp Return: - (PLTP, []) if the PLTP was loaded successfully - (PLTP, warning_list) if the PLTP was loaded with warnings """ try: dic, warnings = parse_file(directory, rel_path) original_type = get_activity_type_class(original.activity_type)() if "type" in dic and original.activity_type != dic["type"]: return None, f"Activity type must remain '{original.activity_type}'" original.activity_data = { **dic, **original_type.install(original), "__reload_path": os.path.join(directory.name, rel_path) } pl_list = list() for item in dic['__pl']: pl_directory = Directory.objects.get(name=item['directory_name']) pl, pl_warnings = load_pl(pl_directory, item['path']) if pl is None: return None, pl_warnings warnings += pl_warnings pl_list.append(pl) originals = list(original.indexed_pl()) original.pl.clear() for pl in pl_list: correspond = list( filter( lambda i: i.directory == pl.directory and i.rel_path == pl. rel_path, originals)) if correspond: correspond = correspond[0] correspond.json = pl.json correspond.save() logger.info("PL '" + str(correspond.id) + " (" + correspond.name + ")' has been updated.") PLPosition.objects.create(parent=original, pl=correspond) else: pl.save() logger.info("PL '" + str(pl.id) + " (" + pl.name + ")' has been created.") PLPosition.objects.create(parent=original, pl=pl) original.save() logger.info("Activity '" + original.name + "' has been reloaded.") return original, [htmlprint.code(warning) for warning in warnings] except Exception as e: # pragma: no cover if not settings.DEBUG: return None, htmlprint.code(str(e)) return (None, (htmlprint.code(str(e)) + '<br>DEBUG set to True - Showing Traceback :<br>' + htmlprint.html_exc()))
def load_PLTP(directory, rel_path, force=False): """ Load the given file as a PLTP. Save it and its PL in the database. Return: - (PLTP, []) if the PLTP was loaded successfully - (PLTP, warning_list) if the PLTP was loaded with warnings - (None, None) if PLTP is already loaded """ name = splitext(basename(rel_path))[0] sha1 = hashlib.sha1() sha1.update((directory.name + ':' + rel_path).encode('utf-8')) sha1 = sha1.hexdigest() try: existing = PLTP.objects.get(sha1=sha1) if not force: return None, None existing.delete() # Delete the current PLTP entry if force is False except: # If the PLTP does not exist, keep going pass dic, warnings = parse_file(directory, rel_path) pl_list = list() for item in dic['__pl']: try: pl_directory = Directory.objects.get(name=item['directory_name']) except ObjectDoesNotExist: raise DirectoryNotFound(dic['__rel_path'], item['line'], item['path'], item['lineno']) pl_directory, pl_path = get_location(directory, item['path']) pl, pl_warnings = load_PL(pl_directory, pl_path) warnings += pl_warnings pl_list.append(pl) for pl in pl_list: pl.save() logger.info("PL '" + str(pl.id) + " (" + pl.name + ")' has been added to the database") pltp = PLTP(name=name, sha1=sha1, json=dic, directory=directory, rel_path=rel_path) pltp.save() logger.info("PLTP '" + sha1 + " (" + name + ")' has been added to the database") for pl in pl_list: pltp.pl.add(pl) return pltp, [exception_to_html(warning) for warning in warnings]
def test_process_extends(self, mock_logger): parser.parse_file(self.dir, "extend.pl") with self.assertRaises(UnknownExtension): parser.parse_file(self.dir, "extend_unknown.pl") dic = { '__rel_path': "unknown.pl", '__extends': [{ 'path': 'unknown.pl', 'line': "0", 'lineno': 0, 'directory_name': self.dir.name }] } with self.assertRaises(FileNotFound): parser.process_extends(dic) dir_name = os.path.join(FAKE_FB_ROOT, "dir2") if os.path.isdir(dir_name): shutil.rmtree(dir_name) dir2 = Directory.objects.create(name='dir2', owner=self.user) shutil.rmtree(dir2.root) shutil.copytree(FAKE_PL, dir2.root) dir2.delete() with self.assertRaises(DirectoryNotFound): parser.parse_file(dir2, "extend_no_dir.pl")
def load_pl(directory, rel_path): """ Load the given path as a PL. Return: - (PL, []) if the PL was loaded successfully - (PL, warning_list) if the PL was loaded with warnings This function return a PL object but does not save it in the database """ dic, warnings = parse_file(directory, rel_path) name = splitext(basename(rel_path))[0] pl = PL(name=name, json=dic, directory=directory, rel_path=rel_path) return pl, [htmlprint.code(warning) for warning in warnings]
def reload_pltp(directory, rel_path, original): """Reload the given file as a PLTP. Also reload its PL, but without modyfing their ID. original is a pltp returned by load_pltp or reload_pltp Return: - (PLTP, []) if the PLTP was loaded successfully - (PLTP, warning_list) if the PLTP was loaded with warnings """ try: dic, warnings = parse_file(directory, rel_path) pl_list = list() for item in dic['__pl']: pl_directory = Directory.objects.get(name=item['directory_name']) pl, pl_warnings = load_pl(pl_directory, item['path']) warnings += pl_warnings pl_list.append(pl) originals = list(original.pl.all()) original.pl.clear() for pl in pl_list: correspond = list( filter( lambda i: i.directory == pl.directory and i.rel_path == pl. rel_path, originals)) if correspond: correspond = correspond[0] correspond.json = pl.json correspond.save() logger.info("PL '" + str(correspond.id) + " (" + correspond.name + ")' has been updated.") Index.objects.create(pltp=original, pl=correspond) else: pl.save() logger.info("PL '" + str(pl.id) + " (" + pl.name + ")' has been created.") Index.objects.create(pltp=original, pl=pl) original.json = dic original.save() logger.info("PLTP '" + original.sha1 + " (" + original.name + ")' has been updated.") return original, [htmlprint.code(warning) for warning in warnings] except Exception as e: # pragma: no cover if not settings.DEBUG: return None, htmlprint.code(str(e)) return (None, (htmlprint.code(str(e)) + '<br>DEBUG set to True - Showing Traceback :<br>' + htmlprint.html_exc()))
def load_pltp(directory, rel_path): """ Load the given file as a PLTP. Save it and its PL in the database. Return: - (PLTP, []) if the PLTP was loaded successfully - (PLTP, warning_list) if the PLTP was loaded with warnings """ name = splitext(basename(rel_path))[0] sha1 = hashlib.sha1() sha1.update( (directory.name + ':' + rel_path + str(time.time())).encode('utf-8')) sha1 = sha1.hexdigest() try: PLTP.objects.get( sha1=sha1).delete() # Reloading the pltp if it already exists except PLTP.DoesNotExist: # If the PLTP does not exist, keep going pass dic, warnings = parse_file(directory, rel_path) pl_list = list() for item in dic['__pl']: pl_directory = Directory.objects.get(name=item['directory_name']) pl, pl_warnings = load_pl(pl_directory, item['path']) warnings += pl_warnings pl_list.append(pl) for pl in pl_list: pl.save() logger.info("PL '" + str(pl.id) + " (" + pl.name + ")' has been added to the database") pltp = PLTP(name=name, sha1=sha1, json=dic, directory=directory, rel_path=rel_path) pltp.save() logger.info("PLTP '" + sha1 + " (" + name + ")' has been added to the database") for pl in pl_list: Index.objects.create(pltp=pltp, pl=pl) return pltp, [htmlprint.code(warning) for warning in warnings]
def load_pla(directory, rel_path, type=None): """ Load the given path as a PL Activity (pla) Return: - (Activity, []) if the activity was successfully loaded - (Activity, warning_list) if the activity was loaded with warnings This function returns an Activity saved in the database """ name = splitext(basename(rel_path))[0] """sha1 = hashlib.sha1() sha1.update((directory.name + ':' + rel_path + str(time.time())).encode('utf-8')) sha1 = sha1.hexdigest() try: Activity.objects.get(sha1=sha1).delete() # Reloading the activity if it already exists except Activity.DoesNotExist: # If the activity does not exist, keep going pass""" dic, warnings = parse_file(directory, rel_path) pl_list = list() for item in dic['__pl']: pl_directory = Directory.objects.get(name=item['directory_name']) pl, pl_warnings = load_pl(pl_directory, item['path']) warnings += pl_warnings pl_list.append(pl) for pl in pl_list: pl.save() logger.info("PL '" + str(pl.id) + " (" + pl.name + ")' has been added to the database") activity_type = type if type else dic["type"] dic["__reload_path"] = os.path.join(directory.name, rel_path) pla = Activity.objects.create(name=name, activity_type=activity_type, activity_data=dic) logger.info("PLA '" + name + "' has been added to the database") for pl in pl_list: PLPosition.objects.create(parent=pla, pl=pl) return pla, [htmlprint.code(warning) for warning in warnings]
def test_parse_file(self, mock_logger): parser.parse_file(self.dir, "working.pl") parser.parse_file(self.dir, "extend.pl") with self.assertRaises(TypeError): parser.parse_file(self.dir, "test_not_string_key.pl") with self.assertRaises(MissingKey): parser.parse_file(self.dir, "test_missing_key.pltp") with self.assertRaises(MissingKey): parser.parse_file(self.dir, "test_no_grader.pl") with self.assertRaises(MissingKey): parser.parse_file(self.dir, "test_no_builder.pl") # testing .pl concatenation with self.assertRaises(MissingKey): parser.parse_file(self.dir, "test_missing_key") with self.assertRaises(UnknownExtension): parser.parse_file(self.dir, "pl.unknown")