From 61b07dfc4c732a46ad8701ffebb370004e0c270b Mon Sep 17 00:00:00 2001 From: virusdefender Date: Fri, 5 Apr 2019 14:18:05 +0800 Subject: [PATCH] fix fps parser --- docs/data.json | 8 ++++++++ fps/parser.py | 9 +++++---- problem/views/admin.py | 8 ++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/data.json b/docs/data.json index 6d86d76b..f7548dd1 100644 --- a/docs/data.json +++ b/docs/data.json @@ -1,5 +1,13 @@ { "update": [ + { + "version": "2019-04-05", + "level": "Recommend", + "title": "2019-04-05", + "details": [ + "Fix bugs in Free Problem Set parser" + ] + }, { "version": "2019-04-03", "level": "Important", diff --git a/fps/parser.py b/fps/parser.py index 4f989ef6..63d3559e 100644 --- a/fps/parser.py +++ b/fps/parser.py @@ -5,6 +5,7 @@ import random import string import hashlib import json +import os import xml.etree.ElementTree as ET @@ -145,14 +146,14 @@ class FPSHelper(object): if spj: one_info = { "input_size": len(input_content), - "input_name": f"{index}.in" + "input_name": f"{index + 1}.in" } else: one_info = { "input_size": len(input_content), - "input_name": f"{index}.in", + "input_name": f"{index + 1}.in", "output_size": len(output_content), - "output_name": f"{index}.out", + "output_name": f"{index + 1}.out", "stripped_output_md5": hashlib.md5(output_content.rstrip().encode("utf-8")).hexdigest() } test_cases[index] = one_info @@ -162,11 +163,11 @@ class FPSHelper(object): } with open(os.path.join(base_dir, "info"), "w", encoding="utf-8") as f: f.write(json.dumps(info, indent=4)) + return info if __name__ == "__main__": import pprint - import os parser = FPSParser("fps.xml") helper = FPSHelper() diff --git a/problem/views/admin.py b/problem/views/admin.py index 5a5268aa..8a282114 100644 --- a/problem/views/admin.py +++ b/problem/views/admin.py @@ -652,7 +652,7 @@ class FPSProblemImport(CSRFExemptAPIView): input_description=problem_data["input"], output_description=problem_data["output"], hint=problem_data["hint"], - test_case_score=[], + test_case_score=problem_data["test_case_score"], time_limit=time_limit, memory_limit=problem_data["memory_limit"]["value"], samples=problem_data["samples"], @@ -686,12 +686,16 @@ class FPSProblemImport(CSRFExemptAPIView): test_case_id = rand_str() test_case_dir = os.path.join(settings.TEST_CASE_DIR, test_case_id) os.mkdir(test_case_dir) - helper.save_test_case(_problem, test_case_dir) + score = [] + for item in helper.save_test_case(_problem, test_case_dir)["test_cases"].values(): + score.append({"score": 0, "input_name": item["input_name"], + "output_name": item.get("output_name")}) problem_data = helper.save_image(_problem, settings.UPLOAD_DIR, settings.UPLOAD_PREFIX) s = FPSProblemSerializer(data=problem_data) if not s.is_valid(): return self.error(f"Parse FPS file error: {s.errors}") problem_data = s.data problem_data["test_case_id"] = test_case_id + problem_data["test_case_score"] = score self._create_problem(problem_data, request.user) return self.success({"import_count": len(problems)})