Feat: can read exam config from yaml

This commit is contained in:
Bertrand Benjamin 2021-01-06 09:09:35 +01:00
parent 56a669b2be
commit 6eb918e0f5

View File

@ -52,18 +52,37 @@ def exam_dict2row(exam):
) )
return rows 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() @cli.command()
def new_exam(): def new_exam():
""" Create new exam csv file """ """ Create new exam csv file """
exam = prompt_exam() exam = prompt_exam()
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"] = [] exam["exercices"] = []
for i, ex in enumerate(exam["exercices"]):
exam["exercices"][i] = prompt_exercise(**ex)
new_exercise = prompt_validate("Ajouter un exercice? ") new_exercise = prompt_validate("Ajouter un exercice? ")
while new_exercise: while new_exercise:
exam["exercices"].append(prompt_exercise(len(exam["exercices"])+1)) exam["exercices"].append(prompt_exercise(len(exam["exercices"])+1))
new_exercise = prompt_validate("Ajouter un exercice? ") new_exercise = prompt_validate("Ajouter un exercice? ")
print(yaml.dump(exam))
rows = exam_dict2row(exam) rows = exam_dict2row(exam)
@ -77,7 +96,7 @@ def new_exam():
path = Path(config["source"]) / exam["tribe"]["name"] path = Path(config["source"]) / exam["tribe"]["name"]
path.mkdir(exist_ok=True) 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) base_df.to_csv(dest, index=False)
print(f"Le fichier note a été enregistré à {dest}") print(f"Le fichier note a été enregistré à {dest}")