cimetiere.py 2.37 KB
Newer Older
1 2 3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
4
import lc_ldap
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import filter2 as filter

cimetiere_root = '/home/cimetiere_lc'

def find_in_filter(lf, key):
    if lf:
        if lf[0] == '=':
             if lf[1] == key:
                 return lf[2]
        else:
            for i in lf[1:]:
                ret=find_in_filter(i, key)
                if ret:
                    return ret
    return None

def valide_filter(lf, ldif):
    if lf:
        if lf[0] == '=':
24
            return True if [i for i in ldif.get(lf[1], [lf[2]]) if lf[2].lower() in i.lower()] else False
25
        elif lf[0] == '!=':
26
            return False if [i for i in ldif.get(lf[1], []) if lf[2].lower() in i.lower()] else True
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        elif lf[0] == '|':
            return reduce(lambda x,y: x or y, [valide_filter(i, ldif) for i in lf[1:]] + [False, False])
        elif lf[0] == '&':
            return reduce(lambda x,y: x and y, [valide_filter(i, ldif) for i in lf[1:]] + [True, True])
        else:
            raise ValueError("Unknown operator %s" % lf[0])

def condition((fdate, ftime, fdn, path), date=None, aid=None, mid=None, fl=None):
    if date:
        if date[0] != '-' and fdate < date[0]:
            return False
        if date[1] != '-' and fdate > date[1]:
            return False
    if aid:
       if not ("aid=%s," % aid) in fdn:
           return False
    if mid:
       if not ("mid=%s," % mid) in fdn:
           return False
    if fl:
47
       (dn, ldif)= lc_ldap.lc_ldap.ressuscite_build_ldif(path)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
       return valide_filter(fl, ldif)
    return True

    
def find(type, fl=None, date=None):
    """type est un objectclass primaire
       fl un filtre humain du genre key1=val1&key2=val2|key3=val3
       date un tuple (debut, fin) avec - pour \infinity"""
    if not type in os.listdir(cimetiere_root):
        raise ValueError("type %s invalid, valid types are %s"  % (type,os.listdir(cimetiere_root)))
    cimetiere  = map(lambda s:  '%s/%s/%s' % (cimetiere_root, type, s), os.listdir('%s/%s' % (cimetiere_root, type)))
    cimetiere.sort()
    fl = filter.human_to_list(fl)
    aid = find_in_filter(fl, "aid")
    mid = find_in_filter(fl, "mid")
    valid=[]
    for file in cimetiere:
        name=os.path.basename(file)
        (fdate, time, dn) = name.split('_')
        if condition((fdate, time, dn, file), fl=fl, aid=aid, mid=mid, date=date):
68
            valid.append(file)
69
    return valid