160 lines
4.1 KiB
Python
160 lines
4.1 KiB
Python
#!/usr/bin/env python
|
|
# encoding: utf-8
|
|
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from prompt_toolkit import HTML
|
|
from ..config import NO_ST_COLUMNS
|
|
import pandas as pd
|
|
import yaml
|
|
from .getconfig import config
|
|
|
|
|
|
def try_parsing_date(text, formats=["%Y-%m-%d", "%Y.%m.%d", "%Y/%m/%d"]):
|
|
for fmt in formats:
|
|
try:
|
|
return datetime.strptime(text[:10], fmt)
|
|
except ValueError:
|
|
pass
|
|
raise ValueError("no valid date format found")
|
|
|
|
|
|
class Exam:
|
|
def __init__(self, name, tribename, date, term, **kwrds):
|
|
self._name = name
|
|
self._tribename = tribename
|
|
|
|
self._date = try_parsing_date(date)
|
|
|
|
self._term = term
|
|
|
|
try:
|
|
kwrds["exercices"]
|
|
except KeyError:
|
|
self._exercises = {}
|
|
else:
|
|
self._exercises = kwrds["exercices"]
|
|
|
|
@property
|
|
def name(self):
|
|
return self._name
|
|
|
|
@property
|
|
def tribename(self):
|
|
return self._tribename
|
|
|
|
@property
|
|
def date(self):
|
|
return self._date
|
|
|
|
@property
|
|
def term(self):
|
|
return self._term
|
|
|
|
def add_exercise(self, name, questions):
|
|
""" Add key with questions in ._exercises """
|
|
try:
|
|
self._exercises[name]
|
|
except KeyError:
|
|
self._exercises[name] = questions
|
|
else:
|
|
raise KeyError("The exercise already exsists. Use modify_exercise")
|
|
|
|
def modify_exercise(self, name, questions, append=False):
|
|
"""Modify questions of an exercise
|
|
|
|
If append==True, add questions to the exercise questions
|
|
|
|
"""
|
|
try:
|
|
self._exercises[name]
|
|
except KeyError:
|
|
raise KeyError("The exercise already exsists. Use modify_exercise")
|
|
else:
|
|
if append:
|
|
self._exercises[name] += questions
|
|
else:
|
|
self._exercises[name] = questions
|
|
|
|
@property
|
|
def exercices(self):
|
|
return self._exercises
|
|
|
|
@property
|
|
def tribe_path(self):
|
|
return Path(config["source"]) / self.tribename
|
|
|
|
@property
|
|
def tribe_student_path(self):
|
|
return (
|
|
Path(config["source"])
|
|
/ [t["students"] for t in config["tribes"] if t["name"] == self.tribename][
|
|
0
|
|
]
|
|
)
|
|
|
|
@property
|
|
def long_name(self):
|
|
""" Get exam name with date inside """
|
|
return f"{self.date.strftime('%y%m%d')}_{self.name}"
|
|
|
|
def path(self, extention=""):
|
|
return self.tribe_path / (self.long_name + extention)
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"name": self.name,
|
|
"tribename": self.tribename,
|
|
"date": self.date,
|
|
"term": self.term,
|
|
"exercices": self.exercices,
|
|
}
|
|
|
|
def to_row(self):
|
|
rows = []
|
|
for ex, questions in self.exercices.items():
|
|
for q in questions:
|
|
rows.append(
|
|
{
|
|
"term": self.term,
|
|
"assessment": self.name,
|
|
"date": self.date.strftime("%d/%m/%Y"),
|
|
"exercise": ex,
|
|
"question": q["id"],
|
|
**q,
|
|
}
|
|
)
|
|
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_yaml(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()))
|
|
|
|
def write_csv(self):
|
|
rows = self.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(self.tribe_student_path)["Nom"]
|
|
for student in students:
|
|
base_df[student] = ""
|
|
|
|
self.tribe_path.mkdir(exist_ok=True)
|
|
base_df.to_csv(self.path(".csv"), index=False)
|