Prise en charge d'une macro et de filtres spéciaux.
* whos_lc -f permet de chercher sur les prises, connexion=ok et adhesion=ok * whos_lc -P permet de chercher le proprio d'une machine ou d'une facture. * Quelques améliorations dans l'affichage
This commit is contained in:
parent
395db97395
commit
3944441033
1 changed files with 63 additions and 12 deletions
|
@ -7,9 +7,13 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
|
import re
|
||||||
|
import time
|
||||||
from ldap import SIZELIMIT_EXCEEDED
|
from ldap import SIZELIMIT_EXCEEDED
|
||||||
|
|
||||||
from gestion.config import encoding
|
from gestion.config import encoding
|
||||||
|
from gestion import affichage
|
||||||
|
from gestion import annuaires_pg
|
||||||
import lc_ldap.shortcuts
|
import lc_ldap.shortcuts
|
||||||
import lc_ldap.objets
|
import lc_ldap.objets
|
||||||
import lc_ldap.filter2 as lfilter
|
import lc_ldap.filter2 as lfilter
|
||||||
|
@ -17,6 +21,10 @@ import lc_ldap.crans_utils
|
||||||
|
|
||||||
ENCODING = encoding.in_encoding
|
ENCODING = encoding.in_encoding
|
||||||
|
|
||||||
|
MACRO_FILTRE_PRISE = re.compile(r'\(prise=(?P<prise>[a-zA-Z][0-9]{3})\)')
|
||||||
|
MACRO_FILTRE_ADHESION = re.compile(r'\(adhesion=ok\)')
|
||||||
|
MACRO_FILTRE_CONNEXION = re.compile(r'\(connexion=ok\)')
|
||||||
|
|
||||||
def explore_db(args):
|
def explore_db(args):
|
||||||
"""Utilise le contenu de args pour décider comment explorer la base de données."""
|
"""Utilise le contenu de args pour décider comment explorer la base de données."""
|
||||||
data = search_ldap(args)
|
data = search_ldap(args)
|
||||||
|
@ -24,7 +32,9 @@ def explore_db(args):
|
||||||
data = limits(data, args)
|
data = limits(data, args)
|
||||||
dataLen = sum([len(elem) for elem in data.itervalues()])
|
dataLen = sum([len(elem) for elem in data.itervalues()])
|
||||||
if dataLen:
|
if dataLen:
|
||||||
for elem in data.itervalues():
|
for (key, elem) in data.iteritems():
|
||||||
|
_header = affichage.style(u"Résultats de type %s trouvés dans la base." % (key,), ['cyan'])
|
||||||
|
print _header.encode(ENCODING)
|
||||||
if len(elem) == 1:
|
if len(elem) == 1:
|
||||||
elem[0].display(
|
elem[0].display(
|
||||||
historique=args.historique,
|
historique=args.historique,
|
||||||
|
@ -46,17 +56,17 @@ def search_ldap(args):
|
||||||
else:
|
else:
|
||||||
search_filter = lfilter.human_to_ldap(args.filtre.decode(ENCODING))
|
search_filter = lfilter.human_to_ldap(args.filtre.decode(ENCODING))
|
||||||
|
|
||||||
|
if args.macro_filtre:
|
||||||
|
search_filter = filter_macro(search_filter)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resultats = LDAP.search(search_filter, sizelimit=args.limit)
|
resultats = LDAP.search(search_filter, sizelimit=args.limite)
|
||||||
except SIZELIMIT_EXCEEDED:
|
except SIZELIMIT_EXCEEDED:
|
||||||
raise EnvironmentError("La limite de résultats LDAP (%s) a été dépassée. Vous pouvez l'augmenter avec l'option -l" % (args.limit,))
|
raise EnvironmentError("La limite de résultats LDAP (%s) a été dépassée. Vous pouvez l'augmenter avec l'option -l" % (args.limite,))
|
||||||
|
|
||||||
for elem in resultats:
|
for elem in resultats:
|
||||||
if not data.has_key(elem.__class__.__name__):
|
if elem not in data.get(elem.__class__.__name__, []):
|
||||||
data[elem.__class__.__name__] = [elem]
|
data.setdefault(elem.__class__.__name__, []).append(elem)
|
||||||
else:
|
|
||||||
if elem not in data[elem.__class__.__name__]:
|
|
||||||
data[elem.__class__.__name__].append(elem)
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -65,7 +75,49 @@ def macro_expand(data, args):
|
||||||
Permet de récupérer les propriétaires d'objets cherchés,
|
Permet de récupérer les propriétaires d'objets cherchés,
|
||||||
ou les factures ou les machines. De chercher par prise,
|
ou les factures ou les machines. De chercher par prise,
|
||||||
etc etc"""
|
etc etc"""
|
||||||
return data
|
|
||||||
|
new_data = dict(data)
|
||||||
|
|
||||||
|
if args.proprietaire:
|
||||||
|
for elem_list in data.itervalues():
|
||||||
|
for elem in elem_list:
|
||||||
|
if hasattr(elem, 'proprio'):
|
||||||
|
_proprio = elem.proprio()
|
||||||
|
|
||||||
|
# On a pas besoin du Crans dans le dico
|
||||||
|
if isinstance(_proprio, lc_ldap.objets.AssociationCrans):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if _proprio not in data.get(_proprio.__class__.__name__, []):
|
||||||
|
new_data.setdefault(_proprio.__class__.__name__, []).append(_proprio)
|
||||||
|
args.adherent = True
|
||||||
|
|
||||||
|
return new_data
|
||||||
|
|
||||||
|
def filter_macro(filtre):
|
||||||
|
"""Étend les macros du filtre passé en argument"""
|
||||||
|
|
||||||
|
thetime = lc_ldap.crans_utils.to_generalized_time_format(time.time())
|
||||||
|
|
||||||
|
prise_match = MACRO_FILTRE_PRISE.match(filtre)
|
||||||
|
adhesion_match = MACRO_FILTRE_ADHESION.match(filtre)
|
||||||
|
connexion_match = MACRO_FILTRE_CONNEXION.match(filtre)
|
||||||
|
|
||||||
|
if prise_match:
|
||||||
|
prise = prise_match.groupdict()['prise']
|
||||||
|
bat = prise[0]
|
||||||
|
chbre = bat + annuaires_pg.reverse(bat, prise[1:])[0]
|
||||||
|
filtre = filtre.replace(u"(prise=%s)" % (prise,), u"(chbre=%s)" % (chbre,))
|
||||||
|
|
||||||
|
if adhesion_match:
|
||||||
|
args.limite = 0
|
||||||
|
filtre = filtre.replace(u"(adhesion=ok)", u"(&(finAdhesion>=%s)(|(aid=*)(cid=*)))" % (thetime,))
|
||||||
|
|
||||||
|
if connexion_match:
|
||||||
|
args.limite = 0
|
||||||
|
filtre = filtre.replace(u"(connexion=ok)", u"(&(finConnexion>=%s)(|(aid=*)(cid=*)))" % (thetime,))
|
||||||
|
|
||||||
|
return filtre
|
||||||
|
|
||||||
def limits(data, args):
|
def limits(data, args):
|
||||||
"""Applique les limitations dans la recherche.
|
"""Applique les limitations dans la recherche.
|
||||||
|
@ -120,9 +172,10 @@ if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Recherche dans la base des adhérents", add_help=False)
|
parser = argparse.ArgumentParser(description="Recherche dans la base des adhérents", add_help=False)
|
||||||
parser.add_argument('-A', '--adresse', help="Affiche l'adresse de l'adhérent.", action="store_true")
|
parser.add_argument('-A', '--adresse', help="Affiche l'adresse de l'adhérent.", action="store_true")
|
||||||
parser.add_argument('-d', '--blacklist', type=int, help="Choix du nombre d'entrées blacklist à afficher pour les entrées détaillées.", action="store", default=10)
|
parser.add_argument('-d', '--blacklist', type=int, help="Choix du nombre d'entrées blacklist à afficher pour les entrées détaillées.", action="store", default=10)
|
||||||
|
parser.add_argument('-f', '--macro_filtre', help="Flag activant la gestion des macros pour le filtre LDAP.", action="store_true")
|
||||||
parser.add_argument('-h', '--help', help="Affiche ce message et quitte.", action="store_true")
|
parser.add_argument('-h', '--help', help="Affiche ce message et quitte.", action="store_true")
|
||||||
parser.add_argument('-i', '--ipsec', help="Affichage de la clef wifi de la machine.", action="store_true")
|
parser.add_argument('-i', '--ipsec', help="Affichage de la clef wifi de la machine.", action="store_true")
|
||||||
parser.add_argument('-l', '--limit', type=int, help="Modifier la taille limite de recherche dans la base LDAP", action="store", default=1000)
|
parser.add_argument('-l', '--limite', type=int, help="Modifier la taille limite de recherche dans la base LDAP", action="store", default=1000)
|
||||||
parser.add_argument('-L', '--historique', type=int, help="Choix du nombre d'entrées d'historique à afficher pour les entrées détaillées.", action="store", default=10)
|
parser.add_argument('-L', '--historique', type=int, help="Choix du nombre d'entrées d'historique à afficher pour les entrées détaillées.", action="store", default=10)
|
||||||
parser.add_argument('-s', '--sshfp', help="Affiche les fingerprint SSH si elles existent.", action="store_true")
|
parser.add_argument('-s', '--sshfp', help="Affiche les fingerprint SSH si elles existent.", action="store_true")
|
||||||
parser.add_argument('-t', '--ldap', help="Utiliser les filtres tels que définis dans ldap", action="store_true")
|
parser.add_argument('-t', '--ldap', help="Utiliser les filtres tels que définis dans ldap", action="store_true")
|
||||||
|
@ -137,9 +190,7 @@ if __name__ == "__main__":
|
||||||
type_group.add_argument('-b', '--borne', help="Limite l'affichage aux bornes.", action="store_true")
|
type_group.add_argument('-b', '--borne', help="Limite l'affichage aux bornes.", action="store_true")
|
||||||
type_group.add_argument('-c', '--club', help="Limite l'affichage aux clubs.", action="store_true")
|
type_group.add_argument('-c', '--club', help="Limite l'affichage aux clubs.", action="store_true")
|
||||||
type_group.add_argument('--crans', help="Limite l'affichage aux machines crans.", action="store_true")
|
type_group.add_argument('--crans', help="Limite l'affichage aux machines crans.", action="store_true")
|
||||||
type_group.add_argument('-F', '--factures', help="Récupère les factures de l'objet cherché.", action="store_true")
|
|
||||||
type_group.add_argument('-m', '--machine', help="Limite l'affichage aux machines.", action="store_true")
|
type_group.add_argument('-m', '--machine', help="Limite l'affichage aux machines.", action="store_true")
|
||||||
type_group.add_argument('-M', '--machines', help="Récupère les machines de l'objet cherché.", action="store_true")
|
|
||||||
type_group.add_argument('-P', '--proprietaire', help="Récupère le propriétaire de l'objet cherché.", action="store_true")
|
type_group.add_argument('-P', '--proprietaire', help="Récupère le propriétaire de l'objet cherché.", action="store_true")
|
||||||
type_group.add_argument('--serveur', help="Limite l'affichage aux serveurs.", action="store_true")
|
type_group.add_argument('--serveur', help="Limite l'affichage aux serveurs.", action="store_true")
|
||||||
type_group.add_argument('--special', help="Limite l'affichage aux machines spéciales.", action="store_true")
|
type_group.add_argument('--special', help="Limite l'affichage aux machines spéciales.", action="store_true")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue