Feat: add temporary save

This commit is contained in:
Bertrand Benjamin 2021-01-10 07:21:28 +01:00
parent 159e7a9f2e
commit 27d7c45980
3 changed files with 42 additions and 11 deletions

View File

@ -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()))

View File

@ -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(

View File

@ -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()]