2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-10 17:35:40 +02:00
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import logging
|
2021-04-02 09:09:41 +02:00
|
|
|
import docx2python
|
|
|
|
|
2021-04-10 17:35:40 +02:00
|
|
|
from config import Config
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-10 17:35:40 +02:00
|
|
|
__LOGGER = logging.getLogger(__name__)
|
2021-04-02 09:09:41 +02:00
|
|
|
REPERTOIRE = "import"
|
2021-04-10 17:35:40 +02:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Parse doc ressources et crée SAE",
|
|
|
|
usage='%(prog)s [options]'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"DOCUMENT",
|
|
|
|
nargs="?",
|
2021-04-15 13:50:47 +02:00
|
|
|
default=REPERTOIRE + "/" + "sae_v0" + ".docx"
|
2021-04-10 17:35:40 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-o",
|
|
|
|
"--outdir",
|
|
|
|
default="export",
|
|
|
|
help="repertoire resultat, defaut: export"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-r",
|
|
|
|
"--root",
|
|
|
|
default="..",
|
|
|
|
help="repertoire de base (racine) pour chercher les fichiers de données"
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
Config.ROOT = args.root
|
|
|
|
|
|
|
|
__LOGGER.warning(f"{sys.argv[0]} processing {args.DOCUMENT}")
|
|
|
|
__LOGGER.warning(f"{sys.argv[0]} outputs to {args.outdir}")
|
|
|
|
|
|
|
|
# Ces imports doivent être faits après la config
|
|
|
|
from tools import *
|
|
|
|
from ressourcedocx import *
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
# Ouverture du document
|
2021-04-10 17:35:40 +02:00
|
|
|
docu = docx2python.docx2python(args.DOCUMENT)
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
docu = docu.body
|
|
|
|
docu[0] # Titre général
|
|
|
|
docu[1] # Tableau de synthèse des ressources
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
ENTETES_CHAPEAU = ["Titre", "Code", "Semestre", "Heures de formation",
|
|
|
|
"dont heures de TP",
|
|
|
|
"Heures \"projet",
|
|
|
|
"Description",
|
|
|
|
"Liste des ressources", "Type de livrable", "Mots clefs"]
|
|
|
|
ENTETES_EXEMPLES = ["Titre", "Description", "Formes", "Quelle problématique",
|
|
|
|
"Modalités"]
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Format du parsing issu de docx2python
|
|
|
|
[ # document
|
|
|
|
[ # table A
|
|
|
|
[ # table A row
|
|
|
|
[ # table A cell 1 <-- structure des tableaux
|
|
|
|
"""
|
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
|
2021-04-02 09:09:41 +02:00
|
|
|
print("*Etape 1* : Parsing")
|
2021-04-02 14:59:08 +02:00
|
|
|
nbre_saes = 0
|
|
|
|
last_sae = None
|
|
|
|
liste_saes = [] # la liste des saes telle qu'extraite du docx
|
|
|
|
liste_exemples = {} # la liste des exemples de chaque SAé
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
for i in range(1, len(docu)): # A priori un tableau
|
|
|
|
est_sae, est_exemple = False, False
|
2021-04-02 09:09:41 +02:00
|
|
|
try:
|
2021-04-02 14:59:08 +02:00
|
|
|
if "Titre de la " in docu[i][0][0][0] or "Nom de la " in docu[i][0][0][0]: # [03][00][0][0]
|
|
|
|
if "Code" in docu[i][1][0][0]:
|
|
|
|
est_sae = True
|
|
|
|
nbre_saes += 1
|
|
|
|
else: # c'est un exemple
|
|
|
|
est_exemple = True
|
2021-04-02 09:09:41 +02:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
if est_sae == True:
|
2021-04-02 09:09:41 +02:00
|
|
|
res = docu[i] # la ressource
|
2021-04-05 12:05:04 +02:00
|
|
|
nom_sae = caracteres_recalcitrants(res[0][1][0])
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
# Création de la ressource
|
2021-04-02 14:59:08 +02:00
|
|
|
r = SAEDocx(nom_sae, res)
|
|
|
|
liste_saes.append(r)
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# Parsing des données brute de la sae
|
|
|
|
data = [None for i in range(len(ENTETES_CHAPEAU))] # les données attendues Nom, Code, ..., Mots clés
|
2021-04-02 09:09:41 +02:00
|
|
|
apprentissages = [None for i in range(3)] # les apprentissages des 3 compétences
|
2021-04-07 11:51:19 +02:00
|
|
|
coeffs = [None for i in range(3)]
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
non_interprete = []
|
|
|
|
for j in range(len(res)): # parcours des entêtes du tableau décrivant la ressource
|
|
|
|
ligne = res[j]
|
|
|
|
if len(ligne) == 2: # ligne de données classique champ => valeur
|
2021-04-02 15:24:56 +02:00
|
|
|
champ = caracteres_recalcitrants(ligne[0][0]) # le nom du champ
|
2021-04-02 14:59:08 +02:00
|
|
|
if champ.startswith("Nom de la"):
|
|
|
|
champ = "Titre de la" # corrige les noms/titres
|
|
|
|
i = get_indice_sans_accent_ni_espace(champ, ENTETES_CHAPEAU) # l'indice de l'entete dans ENTETES
|
2021-04-02 09:09:41 +02:00
|
|
|
if i != None:
|
2021-04-05 12:05:04 +02:00
|
|
|
data[i] = caracteres_recalcitrants("\n".join(res[j][1]))
|
2021-04-02 09:09:41 +02:00
|
|
|
else:
|
|
|
|
non_interprete.append((champ, ligne[1][0]))
|
|
|
|
else: # ligne de données soit chapeau (ex Compétences ciblées) soit détail par compétence
|
|
|
|
champ = ligne[0][0]
|
2021-04-02 14:59:08 +02:00
|
|
|
|
|
|
|
if "Apprentissage(s)" in champ: # les compétences ciblées sont déduites de la présence d'apprentissage critiques
|
2021-04-02 09:09:41 +02:00
|
|
|
# j+1 = les ACs par compétences
|
|
|
|
acs = res[j+2]
|
|
|
|
for k in range(len(acs)):
|
2021-04-02 15:24:56 +02:00
|
|
|
apprentissages[k] = caracteres_recalcitrants("\n".join(acs[k])) # fusionne les ACS (généralement sur plusieurs lignes)
|
2021-04-07 11:51:19 +02:00
|
|
|
elif "Compétence(s) ciblée(s)" in champ:
|
|
|
|
les_coeffs = res[j+2]
|
|
|
|
coeffs = [elmt[0] for elmt in les_coeffs]
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
if non_interprete: # souvent Heures de formation (incluant les TP)
|
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
__LOGGER.warning(f"Dans la saé \"{nom_sae}\", champs en trop non interprétés : " + ",".join(
|
2021-04-02 09:09:41 +02:00
|
|
|
[chp[0] for chp in non_interprete]))
|
|
|
|
|
|
|
|
# Analyse des champs manquants
|
|
|
|
champ_manquants = []
|
2021-04-02 14:59:08 +02:00
|
|
|
for (j, champ) in enumerate(ENTETES_CHAPEAU):
|
2021-04-02 09:09:41 +02:00
|
|
|
if not data[j]:
|
|
|
|
champ_manquants += [champ]
|
|
|
|
if champ_manquants:
|
2021-04-02 14:59:08 +02:00
|
|
|
__LOGGER.warning(f"Dans \"{nom_sae}\", champs manquants : " + ",".join(champ_manquants))
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
# Sauvegarde des champs de la ressource
|
|
|
|
info = tuple(data[1:])
|
|
|
|
r.charge_informations(*info)
|
|
|
|
r.charge_ac(apprentissages)
|
2021-04-07 11:51:19 +02:00
|
|
|
r.charge_coeffs(coeffs)
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# nettoie le titre et le code
|
2021-04-04 11:15:59 +02:00
|
|
|
r.nettoie_code()
|
2021-04-05 12:05:04 +02:00
|
|
|
r.nettoie_titre_sae()
|
|
|
|
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
last_sae = r.code
|
|
|
|
liste_exemples[r.code] = []
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
elif est_exemple == True:
|
|
|
|
res = docu[i] # la ressource
|
2021-04-05 12:05:04 +02:00
|
|
|
nom_exemple = caracteres_recalcitrants(res[0][1][0])
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# Création de la ressource
|
2021-04-03 09:53:25 +02:00
|
|
|
r = ExempleSAEDocx(nom_exemple, res, last_sae)
|
2021-04-02 14:59:08 +02:00
|
|
|
liste_exemples[last_sae].append(r)
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# Parsing des données brute de la sae
|
|
|
|
data = [None for i in range(len(ENTETES_EXEMPLES))] # les données attendues Nom, Code, ..., Mots clés
|
|
|
|
apprentissages = [None for i in range(3)] # les apprentissages des 3 compétences
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
non_interprete = []
|
|
|
|
for j in range(len(res)): # parcours des entêtes du tableau décrivant la ressource
|
|
|
|
ligne = res[j]
|
|
|
|
if len(ligne) == 2: # ligne de données classique champ => valeur
|
2021-04-02 15:24:56 +02:00
|
|
|
champ = caracteres_recalcitrants(ligne[0][0]) # le nom du champ
|
2021-04-02 14:59:08 +02:00
|
|
|
i = get_indice_sans_accent_ni_espace(champ, ENTETES_EXEMPLES) # l'indice de l'entete dans ENTETES
|
|
|
|
if i != None:
|
2021-04-05 12:05:04 +02:00
|
|
|
data[i] = caracteres_recalcitrants("\n".join(res[j][1]))
|
2021-04-02 14:59:08 +02:00
|
|
|
else:
|
|
|
|
non_interprete.append((champ, ligne[1][0]))
|
|
|
|
else: # ligne de données soit chapeau (ex Compétences ciblées) soit détail par compétence
|
|
|
|
print("??? plus de 2 colonnes ?")
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
if non_interprete: # souvent Heures de formation (incluant les TP)
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
__LOGGER.warning(f"Dans l'exemple \"{nom_exemple}\", champs en trop non interprétés : " + ",".join(
|
|
|
|
[chp[0] for chp in non_interprete]))
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# Analyse des champs manquants
|
|
|
|
champ_manquants = []
|
|
|
|
for (j, champ) in enumerate(ENTETES_EXEMPLES):
|
|
|
|
if not data[j]:
|
|
|
|
champ_manquants += [champ]
|
|
|
|
if champ_manquants:
|
|
|
|
__LOGGER.warning(f"Dans \"{nom_exemple}\", champs manquants : " + ",".join(champ_manquants))
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# Sauvegarde des champs de la ressource
|
|
|
|
info = tuple(data[1:])
|
|
|
|
r.charge_informations(*info)
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# fin du parsing
|
|
|
|
print(f"{nbre_saes} saes")
|
|
|
|
for s in liste_exemples:
|
|
|
|
print(f"{s} :" + str(len(liste_exemples[s])) + " exemples")
|
2021-04-02 09:09:41 +02:00
|
|
|
|
|
|
|
# ************************************************************************
|
2021-04-03 09:53:25 +02:00
|
|
|
print("*Etape 2* : Post-traitement")
|
2021-04-02 09:09:41 +02:00
|
|
|
|
2021-04-02 14:59:08 +02:00
|
|
|
# Post traitement des saes => gestion des heures/des acs/ + tri par semestre
|
2021-04-03 09:53:25 +02:00
|
|
|
print(" > SAE")
|
2021-04-02 14:59:08 +02:00
|
|
|
saes = {"S1" : [], "S2": []}
|
|
|
|
|
|
|
|
for s in liste_saes:
|
2021-04-02 16:08:14 +02:00
|
|
|
print(f"{s.nom}")
|
2021-04-07 11:51:19 +02:00
|
|
|
s.nettoie_champs()
|
2021-04-02 16:08:14 +02:00
|
|
|
|
|
|
|
# Tri dans le bon semestre
|
|
|
|
saes[s.semestre] += [s]
|
|
|
|
|
2021-04-03 09:53:25 +02:00
|
|
|
# Post traitement des exemples "S1" => "codesae chapeau" => "ExempleSAE"
|
|
|
|
exemples = {"S1" : {}, "S2" : {} }
|
|
|
|
print(" > Exemples")
|
|
|
|
for s in liste_exemples: # la sae
|
2021-04-05 12:05:04 +02:00
|
|
|
|
2021-04-03 09:53:25 +02:00
|
|
|
sem = get_officiel_sem_sae_by_code(s)
|
|
|
|
exemples[sem][s] = []
|
|
|
|
for e in liste_exemples[s]:
|
|
|
|
print(f"{s} : {e.nom}")
|
2021-04-05 12:05:04 +02:00
|
|
|
if e.nom.startswith("Caractériser un support radio"):
|
|
|
|
print("ici")
|
2021-04-04 11:15:59 +02:00
|
|
|
e.nettoie_champs()
|
2021-04-03 09:53:25 +02:00
|
|
|
|
|
|
|
# Tri dans le bon semestre
|
|
|
|
exemples[sem][s].append(e)
|
|
|
|
|
|
|
|
# Export yaml
|
|
|
|
print("*Etape 3* : Export yaml")
|
2021-04-02 16:08:14 +02:00
|
|
|
for sem in saes:
|
|
|
|
for s in saes[sem]:
|
|
|
|
output = s.to_yaml()
|
|
|
|
if s.code:
|
2021-04-10 17:35:40 +02:00
|
|
|
code_clean = s.code.replace("É", "E")
|
|
|
|
fichier = f"{args.outdir}/{code_clean}.yml"
|
2021-04-02 16:08:14 +02:00
|
|
|
with open(fichier, "w", encoding="utf8") as fid:
|
|
|
|
fid.write(output)
|
|
|
|
|
2021-04-03 09:53:25 +02:00
|
|
|
for sem in exemples:
|
|
|
|
for s in exemples[sem]:
|
|
|
|
for (i, e) in enumerate(exemples[sem][s]):
|
|
|
|
output = e.to_yaml()
|
2021-04-10 17:35:40 +02:00
|
|
|
code_clean = s.replace("É", "E")
|
|
|
|
fichier = f"{args.outdir}/{code_clean}_exemple{i+1}.yml"
|
2021-04-03 09:53:25 +02:00
|
|
|
with open(fichier, "w", encoding="utf8") as fid:
|
|
|
|
fid.write(output)
|
|
|
|
|
|
|
|
|