From 27d7c459800b490c2d6f3797640b679efd2ab011 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 10 Jan 2021 07:21:28 +0100 Subject: [PATCH] Feat: add temporary save --- recopytex/scripts/exam.py | 20 ++++++++++++++++++++ recopytex/scripts/prompts.py | 14 +++++++++----- recopytex/scripts/recopytex.py | 19 +++++++++++++------ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/recopytex/scripts/exam.py b/recopytex/scripts/exam.py index c11fd61..1fcd0a3 100644 --- a/recopytex/scripts/exam.py +++ b/recopytex/scripts/exam.py @@ -3,6 +3,8 @@ from datetime import datetime from pathlib import Path +from prompt_toolkit import HTML +import yaml from .getconfig import config @@ -110,3 +112,21 @@ class Exam: ) return rows + @property + def themes(self): + themes = set() + for questions in self._exercises.values(): + themes.update([q["theme"] for q in questions]) + return themes + + def display_exercise(self, name): + pass + + def display(self, name): + pass + + def write(self): + print(f"Sauvegarde temporaire dans {self.path('.yml')}") + self.tribe_path.mkdir(exist_ok=True) + with open(self.path(".yml"), "w") as f: + f.write(yaml.dump(self.to_dict())) diff --git a/recopytex/scripts/prompts.py b/recopytex/scripts/prompts.py index 19de4fd..685c1ba 100644 --- a/recopytex/scripts/prompts.py +++ b/recopytex/scripts/prompts.py @@ -133,7 +133,7 @@ def prompt_exam(**kwrd): @prompt_until_validate() -def prompt_exercise(number=1, **kwrd): +def prompt_exercise(number=1, completer={}, **kwrd): exercise = {} try: kwrd["name"] @@ -157,7 +157,9 @@ def prompt_exercise(number=1, **kwrd): else: for ques in kwrd["questions"]: try: - exercise["questions"].append(prompt_question(**ques)) + exercise["questions"].append( + prompt_question(completer=completer, **ques) + ) except CancelError: print("Cette question a été supprimée") last_question_id = exercise["questions"][-1]["id"] @@ -167,7 +169,9 @@ def prompt_exercise(number=1, **kwrd): ) while appending: try: - exercise["questions"].append(prompt_question(last_question_id)) + exercise["questions"].append( + prompt_question(last_question_id, completer=completer) + ) except CancelError: print("Cette question a été supprimée") else: @@ -180,7 +184,7 @@ def prompt_exercise(number=1, **kwrd): @prompt_until_validate(cancelable=True) -def prompt_question(last_question_id="1a", **kwrd): +def prompt_question(last_question_id="1a", completer={}, **kwrd): try: kwrd["id"] except KeyError: @@ -206,7 +210,7 @@ def prompt_question(last_question_id="1a", **kwrd): question["theme"] = prompt( "Domaine: ", default=kwrd.get("theme", ""), - # completer + completer=WordCompleter(completer.get("theme", [])), ) question["comment"] = prompt( diff --git a/recopytex/scripts/recopytex.py b/recopytex/scripts/recopytex.py index a70a290..3cd5416 100644 --- a/recopytex/scripts/recopytex.py +++ b/recopytex/scripts/recopytex.py @@ -41,8 +41,6 @@ def new_exam(): """ Create new exam csv file """ exam = Exam(**prompt_exam()) - print(f"Sauvegarde temporaire dans {exam.path('.yml')}") - if exam.path(".yml").exists(): print(f"Fichier sauvegarde trouvé à {exam.path('.yml')} -- importation") with open(exam.path(".yml"), "r") as f: @@ -51,18 +49,27 @@ def new_exam(): ].items(): exam.add_exercise(name, questions) + print(exam.themes) # print(yaml.dump(exam.to_dict())) + exam.write() + for name, questions in exam.exercices.items(): - exam.modify_exercise(**prompt_exercise(name=name, questions=questions)) + exam.modify_exercise( + **prompt_exercise( + name=name, completer={"theme": exam.themes}, questions=questions + ) + ) + exam.write() new_exercise = prompt_validate("Ajouter un exercice? ") while new_exercise: - exam.add_exercise(**prompt_exercise(len(exam.exercices) + 1)) + exam.add_exercise( + **prompt_exercise(len(exam.exercices) + 1, completer={"theme": exam.themes}) + ) + exam.write() new_exercise = prompt_validate("Ajouter un exercice? ") - print(yaml.dump(exam.to_dict())) - rows = exam.to_row() base_df = pd.DataFrame.from_dict(rows)[NO_ST_COLUMNS.keys()]