recopytex/recopytex/scripts/recopytex.py

135 lines
3.6 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
import click
from pathlib import Path
import sys
import papermill as pm
import pandas as pd
from datetime import datetime
import yaml
from .getconfig import config, CONFIGPATH
from .prompts import prompt_exam, prompt_exercise, prompt_validate
from ..config import NO_ST_COLUMNS
from .exam import Exam
from ..dashboard.index import app as dash
@click.group()
def cli():
pass
@cli.command()
def print_config():
click.echo(f"Config file is {CONFIGPATH}")
click.echo("It contains")
click.echo(config)
@cli.command()
def setup():
"""Setup the environnement using recoconfig.yml"""
for tribe in config["tribes"]:
Path(tribe["name"]).mkdir(exist_ok=True)
if not Path(tribe["students"]).exists():
print(f"The file {tribe['students']} does not exists")
@cli.command()
def new_exam():
"""Create new exam csv file"""
exam = Exam(**prompt_exam())
if exam.path(".yml").exists():
print(f"Fichier sauvegarde trouvé à {exam.path('.yml')} -- importation")
with open(exam.path(".yml"), "r") as f:
for name, questions in yaml.load(f, Loader=yaml.SafeLoader)[
"exercices"
].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, 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, completer={"theme": exam.themes})
)
exam.write()
new_exercise = prompt_validate("Ajouter un exercice? ")
rows = exam.to_row()
base_df = pd.DataFrame.from_dict(rows)[NO_ST_COLUMNS.keys()]
base_df.rename(columns=NO_ST_COLUMNS, inplace=True)
students = pd.read_csv(exam.tribe_student_path)["Nom"]
for student in students:
base_df[student] = ""
exam.tribe_path.mkdir(exist_ok=True)
base_df.to_csv(exam.path(".csv"), index=False)
print(f"Le fichier note a été enregistré à {exam.path('.csv')}")
@cli.command()
@click.option("--debug", default=0, help="Debug mode for dash")
def dashboard(debug):
dash.run_server(debug=bool(debug))
@cli.command()
@click.argument("csv_file")
def report(csv_file):
csv = Path(csv_file)
if not csv.exists():
click.echo(f"{csv_file} does not exists")
sys.exit(1)
if csv.suffix != ".csv":
click.echo(f"{csv_file} has to be a csv file")
sys.exit(1)
csv_file = Path(csv_file)
tribe_dir = csv_file.parent
csv_filename = csv_file.name.split(".")[0]
assessment = str(csv_filename).split("_")[-1].capitalize()
date = str(csv_filename).split("_")[0]
try:
date = datetime.strptime(date, "%y%m%d")
except ValueError:
date = datetime.today().strptime(date, "%y%m%d")
tribe = str(tribe_dir).split("/")[-1]
template = Path(config["templates"]) / "tpl_evaluation.ipynb"
dest = Path(config["output"]) / tribe / csv_filename
dest.mkdir(parents=True, exist_ok=True)
click.echo(f"Building {assessment} ({date:%d/%m/%y}) report")
pm.execute_notebook(
str(template),
str(dest / f"{assessment}.ipynb"),
parameters=dict(
tribe=tribe,
assessment=assessment,
date=f"{date:%d/%m/%y}",
csv_file=str(csv_file.absolute()),
),
)