Feat: rewrite new_exam prompt without Pyinquier

This commit is contained in:
Bertrand Benjamin 2020-12-31 18:00:42 +01:00
parent 77c358b0c1
commit 04a2506d86

View File

@ -2,12 +2,27 @@
# encoding: utf-8 # encoding: utf-8
from PyInquirer import prompt from prompt_toolkit import prompt
from prompt_toolkit.validation import Validator
from prompt_toolkit.completion import WordCompleter
from unidecode import unidecode
from datetime import datetime from datetime import datetime
from functools import wraps from functools import wraps
from .getconfig import config from .getconfig import config
VALIDATE = [
"o",
"ok",
"OK",
"oui",
"OUI",
"yes",
"YES",
]
REFUSE = ["n", "non", "NON", "no", "NO"]
CANCEL = ["a", "annuler"]
class CancelError(Exception): class CancelError(Exception):
pass pass
@ -17,19 +32,23 @@ def prompt_until_validate_or_cancel(func):
@wraps(func) @wraps(func)
def wrapper(*args, **kwrd): def wrapper(*args, **kwrd):
ans = func(*args, **kwrd) ans = func(*args, **kwrd)
question = [
{ confirm = prompt(
"type": "list", "C'est ok? (a ou annuler pour sortir) ",
"name": "confirm", completer=WordCompleter(VALIDATE + REFUSE + CANCEL),
"message": "C'est bon?", ).lower()
"choices": ["Oui", "Non", "Annuler cet élément"],
} if confirm in CANCEL:
]
confirm = prompt(question)["confirm"]
while confirm == "Non":
ans = func(*args, **ans, **kwrd)
if confirm == "Annuler cet élément":
raise CancelError raise CancelError
while confirm not in VALIDATE:
ans = func(*args, **ans, **kwrd)
confirm = prompt(
"C'est ok? (a ou annuler pour sortir) ",
completer=WordCompleter(VALIDATE + REFUSE + CANCEL),
).lower()
if confirm in ["a", "annuler"]:
raise CancelError
return ans return ans
return wrapper return wrapper
@ -39,9 +58,18 @@ def prompt_until_validate(func):
@wraps(func) @wraps(func)
def wrapper(*args, **kwrd): def wrapper(*args, **kwrd):
ans = func(*args, **kwrd) ans = func(*args, **kwrd)
question = [{"type": "confirm", "name": "confirm", "message": "C'est bon?"}]
while not prompt(question)["confirm"]: confirm = prompt(
"C'est ok? ",
completer=WordCompleter(VALIDATE + REFUSE),
).lower()
while confirm not in VALIDATE:
ans = func(*args, **ans, **kwrd) ans = func(*args, **ans, **kwrd)
confirm = prompt(
"C'est ok? ",
completer=WordCompleter(VALIDATE + REFUSE),
).lower()
return ans return ans
return wrapper return wrapper
@ -50,46 +78,36 @@ def prompt_until_validate(func):
@prompt_until_validate @prompt_until_validate
def prompt_exam(**kwrd): def prompt_exam(**kwrd):
""" Prompt questions to edit an exam """ """ Prompt questions to edit an exam """
questions = [
{
"message": "Nom de l'évaluation",
"type": "input",
"name": "name",
"default": kwrd.get("name", "DS"),
},
{
"message": "Nom de la classe",
"type": "list",
"name": "_tribe",
"choices": [t["name"] for t in config["tribes"]],
"default": kwrd.get("tribe", ""),
},
{
"message": "Date (%y%m%d)",
"type": "input",
"name": "_date",
"validate": lambda x: (len(x) == 6) and x.isdigit(),
"default": kwrd.get("_date", datetime.today().strftime("%y%m%d")),
},
{
"message": "Trimestre",
"type": "input",
"name": "term",
"validate": lambda x: x.isdigit(),
"default": kwrd.get("term", "1"),
},
{
"message": "Nombre d'exercice",
"type": "input",
"name": "ExQty",
"validate": lambda x: x.isdigit(),
"default": kwrd.get("ExQty", "1"),
},
]
print("Nouvelle évaluation") print("Nouvelle évaluation")
exam = prompt(questions) exam = {}
exam["tribe"] = [t for t in config["tribes"] if exam["_tribe"] == t["name"]][0] exam["name"] = prompt("Nom de l'évaluation: ", default=kwrd.get("name", "DS"))
exam["date"] = datetime.strptime(exam["_date"], "%y%m%d")
tribes_name = [t["name"] for t in config["tribes"]]
exam["tribe"] = prompt(
"Nom de la classe: ",
default=kwrd.get("tribe", ""),
completer=WordCompleter(tribes_name),
validator=Validator.from_callable(lambda x: x in tribes_name),
)
exam["date"] = prompt(
"Date de l'évaluation (%y%m%d): ",
default=kwrd.get("date", datetime.today()).strftime("%y%m%d"),
validator=Validator.from_callable(lambda x: (len(x) == 6) and x.isdigit()),
)
exam["date"] = datetime.strptime(exam["date"], "%y%m%d")
exam["term"] = prompt(
"Trimestre: ",
validator=Validator.from_callable(lambda x: x.isdigit()),
default=kwrd.get("term", "1"),
)
exam["ExQty"] = prompt(
"Nombre d'exercices: ",
validator=Validator.from_callable(lambda x: x.isdigit()),
default=kwrd.get("ExQty", "1"),
)
return exam return exam
@ -103,15 +121,11 @@ def prompt_exercise(number=1, **kwrd):
else: else:
print(f"Modification de la questions {kwrd['name']}") print(f"Modification de la questions {kwrd['name']}")
questions = [ exercise = {}
{ exercise["name"] = prompt(
"message": "Nom de l'exercice", "Nom de l'exercice", default=kwrd.get("name", f"Exercice {number}")
"type": "input", )
"name": "name",
"default": kwrd.get("name", f"Exercice {number}"),
}
]
exercise = prompt(questions)
exercise["questions"] = [] exercise["questions"] = []
try: try:
@ -126,13 +140,13 @@ def prompt_exercise(number=1, **kwrd):
print("Cette question a été supprimée") print("Cette question a été supprimée")
last_question_id = exercise["questions"][-1]["id"] last_question_id = exercise["questions"][-1]["id"]
append = [ # append = prompt("Ajouter une élément de notation",
{ # {
"message": "Ajouter un élément de notation", # "message": "Ajouter un élément de notation",
"type": "confirm", # "type": "confirm",
"name": "append", # "name": "append",
} #
] # ]
while prompt(append)["append"]: while prompt(append)["append"]:
try: try:
exercise["questions"].append(prompt_question(last_question_id)) exercise["questions"].append(prompt_question(last_question_id))