diff --git a/gestion/tools/whosthere.py b/gestion/tools/whosthere.py index 6159daf0..0e2eac0b 100755 --- a/gestion/tools/whosthere.py +++ b/gestion/tools/whosthere.py @@ -10,10 +10,11 @@ import collections import os import xml.dom.minidom -from gestion.ldap_crans import crans_ldap +from gestion.ldap_crans import crans_ldap, MachineWifi from gestion.hptools import hpswitch, ConversationError from gestion.affich_tools import coul, cprint from gestion.whos import aff +import gestion.affichage as affichage # Constantes pour munin. # L'ordre est important : il détermine comment sont empilées les valeurs @@ -29,6 +30,36 @@ STATE_DESCR = collections.OrderedDict([ CLUB_CRANS = 35 CLUB_BDE = 1 +def pretty_name(item): + v = "" + + if hasattr(item, 'nom'): + v = item.nom() + if hasattr(item, 'prenom'): + v = item.prenom() + " " + v + v = v.replace('.wifi.crans.org', ' (WiFi)') + v = v.replace('.crans.org', '') + return v + +def show_liste_by_prop(liste): + by_owner = dict() + + for machine in liste: + # ldap_crans or lc_ldap + owner = (getattr(machine, 'proprietaire', None) or \ + getattr(machine, 'proprio', None))() + if owner.dn not in by_owner: + by_owner[owner.dn] = [pretty_name(owner), []] + by_owner[owner.dn][1].append(pretty_name(machine)) + + for items in by_owner.itervalues(): + items[1] = ", ".join(items[1]) + print affichage.tableau(by_owner.values(), largeur=[None, '*'], + alignement=['g', 'g']).rstrip() + +def show_liste(liste): + print ", ".join(pretty_name(m) for m in liste) + def _mucode(u): """Sad but true: munin ne fait pas d'utf-8 …""" return u.encode('iso-8859-15', errors='ignore') @@ -120,7 +151,7 @@ class WhosThere(object): current = self.query() if current['ma']: cprint('---=== Machines des membres actifs ===---', 'bleu') - aff(current['ma']) + show_liste_by_prop(current['ma']) cprint("---=== Il y a du monde ===---", 'vert') else: cprint("---=== Il semble n'y avoir personne ... ===---", 'rouge') @@ -128,13 +159,13 @@ class WhosThere(object): cprint("Machine inconnue: %s" % mac, 'rouge') if current['crans']: cprint("---=== Machines Cr@ns ===---", 'bleu') - aff(current['crans']) + show_liste(current['crans']) if current['bde']: cprint("---=== Machines du BDE ===---", 'bleu') - aff(current['bde']) + show_liste(current['bde']) if current['adh']: cprint("---=== Machines d'adhérents ===---", 'bleu') - aff(current['adh']) + show_liste_by_prop(current['adh']) def munin_config(self): """Donne la configuration du graphe munin"""