Modification suite à bascule de schema.txt vers code

This commit is contained in:
Fabrication du Numérique 2025-05-01 09:39:31 +02:00
parent 2fd0e28d12
commit 5421a2c3cc

View File

@ -34,9 +34,10 @@ GITEA_URL = os.getenv("GITEA_URL", "https://fabnum-git.peccini.fr/api/v1")
GITEA_TOKEN = os.getenv("GITEA_TOKEN", "") GITEA_TOKEN = os.getenv("GITEA_TOKEN", "")
ORGANISATION = os.getenv("ORGANISATION", "fabnum") ORGANISATION = os.getenv("ORGANISATION", "fabnum")
DEPOT_FICHES = os.getenv("DEPOT_FICHES", "fiches") DEPOT_FICHES = os.getenv("DEPOT_FICHES", "fiches")
DEPOT_CODE = os.getenv("DEPOT_CODE", "code")
ENV = os.getenv("ENV") ENV = os.getenv("ENV")
ENV_CODE = os.getenv("ENV_CODE")
DOT_FILE = "schema.txt" DOT_FILE = os.getenv("DOT_FILE")
niveau_labels = { niveau_labels = {
0: "Produit final", 0: "Produit final",
@ -126,7 +127,7 @@ st.markdown("""
def recuperer_date_dernier_commit_schema(): def recuperer_date_dernier_commit_schema():
headers = {"Authorization": f"token " + GITEA_TOKEN} headers = {"Authorization": f"token " + GITEA_TOKEN}
url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_FICHES}/commits?path=schema.txt&sha={ENV}" url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_CODE}/commits?path={DOT_FILE}&sha={ENV_CODE}"
try: try:
response = requests.get(url, headers=headers, timeout=10) response = requests.get(url, headers=headers, timeout=10)
@ -139,12 +140,12 @@ def recuperer_date_dernier_commit_schema():
else: else:
return None return None
except Exception as e: except Exception as e:
st.error(f"Erreur lors de la récupération du dernier commit de schema.txt : {e}") st.error(f"Erreur lors de la récupération du dernier commit de {DOT_FILE} : {e}")
return None return None
def charger_schema_depuis_gitea(fichier_local="schema_temp.txt"): def charger_schema_depuis_gitea(fichier_local="schema_temp.txt"):
headers = {"Authorization": f"token " + GITEA_TOKEN} headers = {"Authorization": f"token " + GITEA_TOKEN}
url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_FICHES}/contents/schema.txt?ref={ENV}" url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_CODE}/contents/{DOT_FILE}?ref={ENV_CODE}"
try: try:
response = requests.get(url, headers=headers, timeout=10) response = requests.get(url, headers=headers, timeout=10)
@ -162,7 +163,7 @@ def charger_schema_depuis_gitea(fichier_local="schema_temp.txt"):
return "OK" return "OK"
except Exception as e: except Exception as e:
st.error(f"Erreur lors du chargement de schema.txt depuis Gitea : {e}") st.error(f"Erreur lors du chargement de {DOT_FILE} depuis Gitea : {e}")
return None return None
@st.cache_data(ttl=600) @st.cache_data(ttl=600)
@ -801,27 +802,50 @@ def afficher_fiches():
reponse_fiche.raise_for_status() reponse_fiche.raise_for_status()
contenu_md = reponse_fiche.content.decode("utf-8") contenu_md = reponse_fiche.content.decode("utf-8")
# Traitement du markdown # Nouveau traitement hiérarchique du markdown
lignes = contenu_md.split('\n') lignes = contenu_md.split('\n')
contenu_niveau_1 = [] sections_n1 = []
sections_niveau_2 = {} section_n1_actuelle = {"titre": None, "intro": [], "sections_n2": {}}
section_actuelle = None dans_section_n1 = False
section_n2_actuelle = None
for ligne in lignes: for ligne in lignes:
if re.match(r'^##[^#]', ligne): if re.match(r'^#[^#]', ligne):
section_actuelle = ligne.strip('# ').strip() # Nouveau titre de niveau 1
sections_niveau_2[section_actuelle] = [] if section_n1_actuelle["titre"] or section_n1_actuelle["intro"] or section_n1_actuelle["sections_n2"]:
elif section_actuelle: sections_n1.append(section_n1_actuelle)
sections_niveau_2[section_actuelle].append(ligne) section_n1_actuelle = {
else: "titre": ligne.strip('# ').strip(),
contenu_niveau_1.append(ligne) "intro": [],
"sections_n2": {}
}
section_n2_actuelle = None
dans_section_n1 = True
if contenu_niveau_1: elif re.match(r'^##[^#]', ligne):
st.markdown("\n".join(contenu_niveau_1), unsafe_allow_html=True) # Nouveau titre de niveau 2
section_n2_actuelle = ligne.strip('# ').strip()
section_n1_actuelle["sections_n2"][section_n2_actuelle] = []
elif section_n2_actuelle:
section_n1_actuelle["sections_n2"][section_n2_actuelle].append(ligne)
elif dans_section_n1:
section_n1_actuelle["intro"].append(ligne)
for titre, contenu in sections_niveau_2.items(): # Ajouter la dernière section si présente
with st.expander(titre): if section_n1_actuelle["titre"] or section_n1_actuelle["intro"] or section_n1_actuelle["sections_n2"]:
st.markdown("\n".join(contenu), unsafe_allow_html=True) sections_n1.append(section_n1_actuelle)
# Affichage
for bloc in sections_n1:
if bloc["titre"]:
st.markdown(f"# {bloc['titre']}")
if bloc["intro"]:
st.markdown("\n".join(bloc["intro"]), unsafe_allow_html=True)
for sous_titre, contenu in bloc["sections_n2"].items():
with st.expander(sous_titre):
st.markdown("\n".join(contenu), unsafe_allow_html=True)
gerer_tickets_fiche(fiche_choisie) gerer_tickets_fiche(fiche_choisie)