From 6eb918e0f55779b89a2353f2b09545d9e582dd90 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 6 Jan 2021 09:09:35 +0100 Subject: [PATCH] Feat: can read exam config from yaml --- recopytex/scripts/recopytex.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/recopytex/scripts/recopytex.py b/recopytex/scripts/recopytex.py index caec83c..acb1198 100644 --- a/recopytex/scripts/recopytex.py +++ b/recopytex/scripts/recopytex.py @@ -52,18 +52,37 @@ def exam_dict2row(exam): ) return rows +def get_exam_name(exam): + """ Get exam name from exam data """ + return f"{exam['date'].strftime('%y%m%d')}_{exam['name']}" + +def get_tribe_path(exam): + """ Get tribe path from exam data """ + return Path(config["source"]) / exam["tribe"]["name"] + +def get_exam_path(exam, extention=""): + return get_tribe_path(exam)/ (get_exam_name(exam) + extention) + @cli.command() def new_exam(): """ Create new exam csv file """ exam = prompt_exam() - exam["exercices"] = [] + + if get_exam_path(exam, ".yml").exists(): + with open(get_exam_path(exam, ".yml"), "r") as f: + exam["exercices"] = yaml.load(f, Loader=yaml.SafeLoader)["exercices"] + else: + exam["exercices"] = [] + + for i, ex in enumerate(exam["exercices"]): + exam["exercices"][i] = prompt_exercise(**ex) + new_exercise = prompt_validate("Ajouter un exercice? ") while new_exercise: exam["exercices"].append(prompt_exercise(len(exam["exercices"])+1)) new_exercise = prompt_validate("Ajouter un exercice? ") - print(yaml.dump(exam)) rows = exam_dict2row(exam) @@ -77,7 +96,7 @@ def new_exam(): path = Path(config["source"]) / exam["tribe"]["name"] path.mkdir(exist_ok=True) - dest = path / f"{exam['date'].strftime('%y%m%d')}_{exam['name']}.csv" + dest = path / get_exam_name(exam) + ".csv" base_df.to_csv(dest, index=False) print(f"Le fichier note a été enregistré à {dest}")