2020-2021/tasks.py

150 lines
4.0 KiB
Python
Raw Normal View History

2020-08-14 13:06:30 +00:00
from invoke import task
from pathlib import Path
from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.validation import Validator
from unidecode import unidecode
2020-08-16 13:32:44 +00:00
import jinja2
2020-08-14 13:06:30 +00:00
from pytex import texenv
import datetime
import locale
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
TRIBES = [
2020-08-16 13:32:44 +00:00
{"name": "Terminale Technologique", "directory": "TST"},
{"name": "Terminale Terminale spé sti2d", "directory": "TST_sti2d"},
{"name": "Science numérique et technologique", "directory": "SNT"},
2020-08-27 09:09:08 +00:00
{"name": "Enseignement scientifique", "directory": "EnsSci"},
2020-08-16 13:32:44 +00:00
]
2020-08-14 13:06:30 +00:00
AUTHOR = "Benjamin Bertrand"
2020-08-16 13:32:44 +00:00
2020-08-14 13:06:30 +00:00
def is_tribe(tribe):
return tribe in [t["directory"] for t in TRIBES]
2020-08-16 13:32:44 +00:00
2020-08-14 13:06:30 +00:00
def get_sequences(tribe):
p = Path(tribe)
return [x for x in p.iterdir() if (x.is_dir() and x.name[0:2].isdigit())]
2020-08-16 13:32:44 +00:00
2020-08-14 13:06:30 +00:00
def get_tags(tribe=""):
p = Path(tribe)
tags = set()
indexes = p.glob("**/index.rst")
for index_p in indexes:
with open(index_p, "r") as index:
line = index.readline()
while line:
if line.startswith(":tags"):
2020-08-16 13:32:44 +00:00
tags.update(line[7:].replace("\n", "").split(", "))
2020-08-14 13:06:30 +00:00
break
line = index.readline()
return {t.lower() for t in tags}
2020-08-16 13:32:44 +00:00
2020-08-14 13:06:30 +00:00
def next_weekday(date, day=0):
2020-08-16 13:32:44 +00:00
return date + datetime.timedelta(days=(day - date.weekday() + 7) % 7)
2020-08-14 13:06:30 +00:00
def sequence_prompt(
author=AUTHOR,
tribe="",
title="",
tags="",
summary="",
*args,
**kwrds,
):
2020-08-14 13:06:30 +00:00
sequence = {}
print("Nouvelle séquence")
sequence["author"] = prompt("Auteur: ", default=author)
2020-08-14 13:06:30 +00:00
sequence["date"] = datetime.date.today()
tribes = WordCompleter([t["directory"] for t in TRIBES])
2020-08-16 13:32:44 +00:00
sequence["tribe"] = prompt(
"Classe: ",
completer=tribes,
validator=Validator.from_callable(is_tribe),
default=tribe,
2020-08-16 13:32:44 +00:00
)
2020-08-14 13:06:30 +00:00
sequences = get_sequences(sequence["tribe"])
2020-08-16 13:32:44 +00:00
print("Séquences trouvées:\n\t" + "\n\t".join(sorted([s.name for s in sequences])))
2020-09-22 18:25:42 +00:00
sequence["num"] = max([int(seq.name[0:2]) for seq in sequences]) + 1
sequence["title"] = prompt(
f"Nom de la séquence (n°{sequence['num']}): ", default=title
)
2020-08-14 13:06:30 +00:00
known_tags = get_tags()
2020-08-16 13:32:44 +00:00
sequence["tags"] = prompt(
"Tags (séparés par une virgule): ",
completer=WordCompleter(known_tags),
default=tags,
2020-08-16 13:32:44 +00:00
)
2020-08-14 13:06:30 +00:00
sequence["summary"] = prompt("Résumé: ", default=summary)
2020-08-14 13:06:30 +00:00
return sequence
2020-08-16 13:32:44 +00:00
2020-08-14 13:06:30 +00:00
def import_skel(dest, datas, skel="sequence", skel_path="./tools/skeleton"):
d = Path(dest)
d.mkdir()
p = Path(skel_path + "/" + skel)
files = [t.name for t in p.iterdir()]
template_loader = jinja2.FileSystemLoader(searchpath=p)
template_env = jinja2.Environment(loader=template_loader)
2020-08-16 13:32:44 +00:00
2020-08-14 13:06:30 +00:00
texenv.loader.loaders.append(template_loader)
print("")
for filename in files:
if filename.endswith(".tex"):
template = texenv.get_template(filename)
else:
template = template_env.get_template(filename)
output_text = template.render(**datas)
2020-08-16 13:32:44 +00:00
with open(d / filename, "w") as f:
2020-08-14 13:06:30 +00:00
f.write(output_text)
print(f"Importation de {filename} - OK")
2020-08-16 13:32:44 +00:00
2020-08-14 13:06:30 +00:00
@task
def sequence(c):
sequence = {}
validate = False
while not validate:
sequence = sequence_prompt(**sequence)
dest = unidecode(
("{tribe}/{num:0>2}_{title}/".format(**sequence)).replace(" ", "_")
)
dest = Path(dest)
seq_summary = """\
Séquence: {num:0>2} {title}
Niveau: {tribe}
Auteur: {author}
Date: {date}
Tags: {tags}
Sommaire: {summary}
Dossier: {dest}
""".format(
dest=dest, **sequence
)
print(seq_summary)
validate = prompt("C'est ok? ").lower() in ["o", "k", "oui", "ok"]
sequence["title_under"] = "#" * len(sequence["title"])
import_skel(dest, sequence, "sequence")
2020-08-14 13:06:30 +00:00