Améliore messages d'erreur si maquette Apo mal encodée

This commit is contained in:
Emmanuel Viennet 2023-01-31 14:00:11 -03:00
parent a42a3708fb
commit 221ff84dce
4 changed files with 76 additions and 33 deletions

View File

@ -43,6 +43,7 @@ Pour chaque étudiant commun:
comparer les résultats comparer les résultats
""" """
from flask import g, url_for
from app import log from app import log
from app.scodoc import sco_apogee_csv from app.scodoc import sco_apogee_csv
@ -72,11 +73,11 @@ def apo_compare_csv_form():
""" """
<div class="apo_compare_csv_form_but"> <div class="apo_compare_csv_form_but">
Fichier Apogée A: Fichier Apogée A:
<input type="file" size="30" name="A_file"/> <input type="file" size="30" name="file_a"/>
</div> </div>
<div class="apo_compare_csv_form_but"> <div class="apo_compare_csv_form_but">
Fichier Apogée B: Fichier Apogée B:
<input type="file" size="30" name="B_file"/> <input type="file" size="30" name="file_b"/>
</div> </div>
<input type="checkbox" name="autodetect" checked/>autodétecter encodage</input> <input type="checkbox" name="autodetect" checked/>autodétecter encodage</input>
<div class="apo_compare_csv_form_submit"> <div class="apo_compare_csv_form_submit">
@ -88,17 +89,36 @@ def apo_compare_csv_form():
return "\n".join(H) return "\n".join(H)
def apo_compare_csv(A_file, B_file, autodetect=True): def apo_compare_csv(file_a, file_b, autodetect=True):
"""Page comparing 2 Apogee CSV files""" """Page comparing 2 Apogee CSV files"""
A = _load_apo_data(A_file, autodetect=autodetect) try:
B = _load_apo_data(B_file, autodetect=autodetect) apo_data_a = _load_apo_data(file_a, autodetect=autodetect)
apo_data_b = _load_apo_data(file_b, autodetect=autodetect)
except (UnicodeDecodeError, UnicodeEncodeError) as exc:
dest_url = url_for("notes.semset_page", scodoc_dept=g.scodoc_dept)
if autodetect:
raise ScoValueError(
"""
Erreur: l'encodage de l'un des fichiers est mal détecté.
Essayez sans auto-détection, ou vérifiez le codage et le contenu
des fichiers.
""",
dest_url=dest_url,
) from exc
else:
raise ScoValueError(
f"""
Erreur: l'encodage de l'un des fichiers est incorrect.
Vérifiez qu'il est bien en {sco_apogee_csv.APO_INPUT_ENCODING}
""",
dest_url=dest_url,
) from exc
H = [ H = [
html_sco_header.sco_header(page_title="Comparaison de fichiers Apogée"), html_sco_header.sco_header(page_title="Comparaison de fichiers Apogée"),
"<h2>Comparaison de fichiers Apogée</h2>", "<h2>Comparaison de fichiers Apogée</h2>",
_help_txt, _help_txt,
'<div class="apo_compare_csv">', '<div class="apo_compare_csv">',
_apo_compare_csv(A, B), _apo_compare_csv(apo_data_a, apo_data_b),
"</div>", "</div>",
"""<p><a href="apo_compare_csv_form" class="stdlink">Autre comparaison</a></p>""", """<p><a href="apo_compare_csv_form" class="stdlink">Autre comparaison</a></p>""",
html_sco_header.sco_footer(), html_sco_header.sco_footer(),
@ -112,9 +132,9 @@ def _load_apo_data(csvfile, autodetect=True):
if autodetect: if autodetect:
data_b, message = sco_apogee_csv.fix_data_encoding(data_b) data_b, message = sco_apogee_csv.fix_data_encoding(data_b)
if message: if message:
log("apo_compare_csv: %s" % message) log(f"apo_compare_csv: {message}")
if not data_b: if not data_b:
raise ScoValueError("apo_compare_csv: no data") raise ScoValueError("fichier vide ? (apo_compare_csv: no data)")
data = data_b.decode(sco_apogee_csv.APO_INPUT_ENCODING) data = data_b.decode(sco_apogee_csv.APO_INPUT_ENCODING)
apo_data = sco_apogee_csv.ApoData(data, orig_filename=csvfile.filename) apo_data = sco_apogee_csv.ApoData(data, orig_filename=csvfile.filename)
return apo_data return apo_data

View File

@ -155,28 +155,25 @@ def fix_data_encoding(
text: bytes, text: bytes,
default_source_encoding=APO_INPUT_ENCODING, default_source_encoding=APO_INPUT_ENCODING,
dest_encoding=APO_INPUT_ENCODING, dest_encoding=APO_INPUT_ENCODING,
) -> bytes: ) -> tuple[bytes, str]:
"""Try to ensure that text is using dest_encoding """Try to ensure that text is using dest_encoding
returns converted text, and a message describing the conversion. returns converted text, and a message describing the conversion.
Raises UnicodeEncodeError en cas de problème, en général liée à
une auto-détection errornée.
""" """
message = "" message = ""
detected_encoding = guess_data_encoding(text) detected_encoding = guess_data_encoding(text)
if not detected_encoding: if not detected_encoding:
if default_source_encoding != dest_encoding: if default_source_encoding != dest_encoding:
message = "converting from %s to %s" % ( message = f"converting from {default_source_encoding} to {dest_encoding}"
default_source_encoding, text = text.decode(default_source_encoding).encode(dest_encoding)
dest_encoding,
)
text = text.decode(default_source_encoding).encode(
dest_encoding
) # XXX #py3 #sco8 à tester
else: else:
if detected_encoding != dest_encoding: if detected_encoding != dest_encoding:
message = "converting from detected %s to %s" % ( message = (
detected_encoding, f"converting from detected {default_source_encoding} to {dest_encoding}"
dest_encoding,
) )
text = text.decode(detected_encoding).encode(dest_encoding) # XXX text = text.decode(detected_encoding).encode(dest_encoding)
return text, message return text, message

View File

@ -591,19 +591,45 @@ def view_apo_csv_store(semset_id="", csvfile=None, data: bytes = "", autodetect=
if not semset_id: if not semset_id:
raise ValueError("invalid null semset_id") raise ValueError("invalid null semset_id")
semset = sco_semset.SemSet(semset_id=semset_id) semset = sco_semset.SemSet(semset_id=semset_id)
try:
if csvfile: if csvfile:
data = csvfile.read() # bytes data = csvfile.read() # bytes
if autodetect: if autodetect:
# check encoding (although documentation states that users SHOULD upload LATIN1) # check encoding (although documentation states that users SHOULD upload LATIN1)
data, message = sco_apogee_csv.fix_data_encoding(data) data, message = sco_apogee_csv.fix_data_encoding(data)
if message: if message:
log("view_apo_csv_store: %s" % message) log(f"view_apo_csv_store: {message}")
else: else:
log("view_apo_csv_store: autodetection of encoding disabled by user") log("view_apo_csv_store: autodetection of encoding disabled by user")
if not data: if not data:
raise ScoValueError("view_apo_csv_store: no data") raise ScoValueError("view_apo_csv_store: no data")
# data est du bytes, encodé en APO_INPUT_ENCODING # data est du bytes, encodé en APO_INPUT_ENCODING
data_str = data.decode(APO_INPUT_ENCODING) data_str = data.decode(APO_INPUT_ENCODING)
except (UnicodeDecodeError, UnicodeEncodeError) as exc:
dest_url = url_for(
"notes.apo_semset_maq_status",
scodoc_dept=g.scodoc_dept,
semset_id=semset_id,
)
if autodetect:
raise ScoValueError(
f"""
Erreur: l'encodage du fichier est mal détecté.
Essayez sans auto-détection, ou vérifiez le codage et le contenu
du fichier (qui doit être en {sco_apogee_csv.APO_INPUT_ENCODING}).
""",
dest_url=dest_url,
) from exc
else:
raise ScoValueError(
f"""
Erreur: l'encodage du fichier est incorrect.
Vérifiez qu'il est bien en {sco_apogee_csv.APO_INPUT_ENCODING}
""",
dest_url=dest_url,
) from exc
# check si etape maquette appartient bien au semset # check si etape maquette appartient bien au semset
apo_data = sco_apogee_csv.ApoData( apo_data = sco_apogee_csv.ApoData(
data_str, periode=semset["sem_id"] data_str, periode=semset["sem_id"]

View File

@ -1,7 +1,7 @@
# -*- mode: python -*- # -*- mode: python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
SCOVERSION = "9.4.34" SCOVERSION = "9.4.35"
SCONAME = "ScoDoc" SCONAME = "ScoDoc"