server/sources/ldapuser.py
branchtls-sprint
changeset 1016 26387b836099
parent 975 0928daea04e9
child 1263 01152fffd593
equal deleted inserted replaced
1014:4792a1bb72a9 1016:26387b836099
    18 WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    18 WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    19 WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    19 WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    20 FOR A PARTICULAR PURPOSE.
    20 FOR A PARTICULAR PURPOSE.
    21 """
    21 """
    22 
    22 
    23 from mx.DateTime import now, DateTimeDelta
    23 from datetime import datetime, timedelta
    24 
    24 
    25 from logilab.common.textutils import get_csv
    25 from logilab.common.textutils import get_csv
    26 from rql.nodes import Relation, VariableRef, Constant, Function
    26 from rql.nodes import Relation, VariableRef, Constant, Function
    27 
    27 
    28 import ldap
    28 import ldap
    51     }
    51     }
    52 
    52 
    53 class TimedCache(dict):
    53 class TimedCache(dict):
    54     def __init__(self, ttlm, ttls=0):
    54     def __init__(self, ttlm, ttls=0):
    55         # time to live in minutes
    55         # time to live in minutes
    56         self.ttl = DateTimeDelta(0, 0, ttlm, ttls)
    56         self.ttl = timedelta(0, ttlm*60 + ttls, 0)
    57         
    57         
    58     def __setitem__(self, key, value):
    58     def __setitem__(self, key, value):
    59         dict.__setitem__(self, key, (now(), value))
    59         dict.__setitem__(self, key, (datetime.now(), value))
    60         
    60         
    61     def __getitem__(self, key):
    61     def __getitem__(self, key):
    62         return dict.__getitem__(self, key)[1]
    62         return dict.__getitem__(self, key)[1]
    63     
    63     
    64     def clear_expired(self):
    64     def clear_expired(self):
    65         now_ = now()
    65         now_ = datetime.now()
    66         ttl = self.ttl
    66         ttl = self.ttl
    67         for key, (timestamp, value) in self.items():
    67         for key, (timestamp, value) in self.items():
    68             if now_ - timestamp > ttl:
    68             if now_ - timestamp > ttl:
    69                 del self[key]
    69                 del self[key]
    70                 
    70