diff --git a/tools/fill_emails_from_contacts.py b/tools/fill_emails_from_contacts.py index 18bca29..21d4a68 100644 --- a/tools/fill_emails_from_contacts.py +++ b/tools/fill_emails_from_contacts.py @@ -23,6 +23,28 @@ def normalize(s): return " ".join(ascii_str.split()) +def synth_email(name): + """Genere prenom.nom@sanef.com a partir d'un nom complet. + 'Frédéric GRAFFAGNINO' -> 'frederic.graffagnino@sanef.com' + 'Pierre-Louis THOUVENOT' -> 'pierre-louis.thouvenot@sanef.com' + """ + if not name: + return None + norm = normalize(name) + parts = [p for p in norm.split() if p] + if len(parts) < 2: + return None + first = parts[0] + last = parts[-1] + # Nettoie caracteres parasites (garde lettres + tiret + apostrophe) + import re + first = re.sub(r"[^a-z\-']", "", first) + last = re.sub(r"[^a-z\-']", "", last) + if not first or not last: + return None + return f"{first}.{last}@sanef.com" + + def main(): parser = argparse.ArgumentParser() parser.add_argument("--dry-run", action="store_true") @@ -52,7 +74,7 @@ def main(): print(f"[INFO] {len(rows)} (dom,env) a completer") updated = 0 - unmatched = set() + from_contacts = synth = 0 for r in rows: updates = {} if r.responsable_nom and not (r.responsable_email or "").strip(): @@ -60,15 +82,23 @@ def main(): email = name_to_email.get(key) if email: updates["responsable_email"] = email - elif key: - unmatched.add(r.responsable_nom) + from_contacts += 1 + else: + synth_e = synth_email(r.responsable_nom) + if synth_e: + updates["responsable_email"] = synth_e + synth += 1 if r.referent_nom and not (r.referent_email or "").strip(): key = normalize(r.referent_nom) email = name_to_email.get(key) if email: updates["referent_email"] = email - elif key: - unmatched.add(r.referent_nom) + from_contacts += 1 + else: + synth_e = synth_email(r.referent_nom) + if synth_e: + updates["referent_email"] = synth_e + synth += 1 if not updates: continue @@ -81,10 +111,8 @@ def main(): updated += 1 conn.close() - print(f"\n[DONE] Maj: {updated}") - if unmatched: - print(f"[WARN] Noms sans match dans contacts ({len(unmatched)}) : " - f"{sorted(unmatched)[:10]}...") + print(f"\n[DONE] Maj: {updated} | depuis contacts: {from_contacts} | " + f"synthetises (prenom.nom@sanef.com): {synth}") if __name__ == "__main__":