# ihh.py import re import yaml from jinja2 import Template from ..utils.pastille import pastille IHH_RE = re.compile(r"```yaml\s+opération:(.*?)```", re.S | re.I) def _synth_ihh(operations: list[dict]) -> str: data_by_item = {} for op in operations: # nom = op.get('nom', '') item_id = op.get('minerai', op.get('produit', op.get('composant', ''))) if not item_id: continue if item_id not in data_by_item: data_by_item[item_id] = { 'type': 'minerai' if 'extraction' in op or 'reserves' in op or 'traitement' in op else 'produit' if 'assemblage' in op else 'composant', 'extraction_ihh_pays': '-', 'extraction_ihh_acteurs': '-', 'reserves_ihh_pays': '-', 'traitement_ihh_pays': '-', 'traitement_ihh_acteurs': '-', 'assemblage_ihh_pays': '-', 'assemblage_ihh_acteurs': '-', 'fabrication_ihh_pays': '-', 'fabrication_ihh_acteurs': '-' } if 'extraction' in op: data_by_item[item_id]['extraction_ihh_pays'] = op['extraction'].get('ihh_pays', '-') data_by_item[item_id]['extraction_ihh_acteurs'] = op['extraction'].get('ihh_acteurs', '-') data_by_item[item_id]['reserves_ihh_pays'] = op['reserves'].get('ihh_pays', '-') data_by_item[item_id]['traitement_ihh_pays'] = op['traitement'].get('ihh_pays', '-') data_by_item[item_id]['traitement_ihh_acteurs'] = op['traitement'].get('ihh_acteurs', '-') elif 'assemblage' in op: data_by_item[item_id]['assemblage_ihh_pays'] = op['assemblage'].get('ihh_pays', '-') data_by_item[item_id]['assemblage_ihh_acteurs'] = op['assemblage'].get('ihh_acteurs', '-') elif 'fabrication' in op: data_by_item[item_id]['fabrication_ihh_pays'] = op['fabrication'].get('ihh_pays', '-') data_by_item[item_id]['fabrication_ihh_acteurs'] = op['fabrication'].get('ihh_acteurs', '-') result = [] produits = {k: v for k, v in data_by_item.items() if v['type'] == 'produit'} if produits: result.append("\n\n## Assemblage des produits\n") produit_lignes = [ "| Produit | Assemblage IHH Pays | Assemblage IHH Acteurs |", "| :-- | :--: | :--: |" ] for produit, data in sorted(produits.items()): pastille_1 = pastille("IHH", data['assemblage_ihh_pays']) pastille_2 = pastille("IHH", data['assemblage_ihh_acteurs']) produit_lignes.append( f"| {produit} | {pastille_1} {data['assemblage_ihh_pays']} | {pastille_2} {data['assemblage_ihh_acteurs']} |" ) result.append("\n".join(produit_lignes)) composants = {k: v for k, v in data_by_item.items() if v['type'] == 'composant'} if composants: result.append("\n\n## Fabrication des composants\n") composant_lignes = [ "| Composant | Fabrication IHH Pays | Fabrication IHH Acteurs |", "| :-- | :--: | :--: |" ] for composant, data in sorted(composants.items()): pastille_1 = pastille("IHH", data['fabrication_ihh_pays']) pastille_2 = pastille("IHH", data['fabrication_ihh_acteurs']) composant_lignes.append( f"| {composant} | {pastille_1} {data['fabrication_ihh_pays']} | {pastille_2} {data['fabrication_ihh_acteurs']} |" ) result.append("\n".join(composant_lignes)) minerais = {k: v for k, v in data_by_item.items() if v['type'] == 'minerai'} if minerais: result.append("\n\n## Opérations sur les minerais\n") minerai_lignes = [ "| Minerai | Extraction IHH Pays | Extraction IHH Acteurs | Réserves IHH Pays | Traitement IHH Pays | Traitement IHH Acteurs |", "| :-- | :--: | :--: | :--: | :--: | :--: |" ] for minerai, data in sorted(minerais.items()): pastille_1 = pastille("IHH", data['extraction_ihh_pays']) pastille_2 = pastille("IHH", data['extraction_ihh_acteurs']) pastille_3 = pastille("IHH", data['reserves_ihh_pays']) pastille_4 = pastille("IHH", data['traitement_ihh_pays']) pastille_5 = pastille("IHH", data['traitement_ihh_acteurs']) minerai_lignes.append( f"| {minerai} | {pastille_1} {data['extraction_ihh_pays']} | {pastille_2} {data['extraction_ihh_acteurs']} | " f"{pastille_3} {data['reserves_ihh_pays']} | {pastille_4} {data['traitement_ihh_pays']} | {pastille_5} {data['traitement_ihh_acteurs']} |" ) result.append("\n".join(minerai_lignes)) return "\n".join(result) def build_ihh_sections(md: str) -> str: segments = [] operations = [] intro = None matches = list(IHH_RE.finditer(md)) if matches: first = matches[0] intro = md[:first.start()].strip() else: return md for m in matches: bloc_text = m.group(1) bloc = yaml.safe_load("opération:" + bloc_text) operations.append(bloc["opération"]) start = m.end() next_match = IHH_RE.search(md, start) end = next_match.start() if next_match else len(md) section_template = md[start:end].strip() rendered = Template(section_template).render(**bloc["opération"]) segments.append(rendered) if intro: segments.insert(0, intro) if "# Tableaux de synthèse" in md: synth_table = _synth_ihh(operations) md_final = "\n\n".join(segments) md_final = re.sub( r"(?:##?|#) Tableaux de synthèse\s*\n.*?", f"# Tableaux de synthèse\n\n{synth_table}\n", md_final, flags=re.S ) else: md_final = "\n\n".join(segments) return md_final