X-Git-Url: https://git.adam-barratt.org.uk/?p=mirror%2Fuserdir-ldap.git;a=blobdiff_plain;f=UDLdap.py;fp=UDLdap.py;h=3c0f0efe121591bd0d1470b952800fb1933694ae;hp=b546112af6a3abb636e5f0a4b0f62a6fd00cb4c4;hb=e115e0a01d4b4f14eafd83346cb0de83d818385a;hpb=8b8ed35607427e0242b70b3623bc7a6814ac78b6 diff --git a/UDLdap.py b/UDLdap.py index b546112..3c0f0ef 100644 --- a/UDLdap.py +++ b/UDLdap.py @@ -27,24 +27,28 @@ class Account: def __init__(self, dn, attributes): self.dn = dn self.attributes = attributes + self.cache = {} def __getitem__(self, key): + if key in self.cache: + return self.cache[key] + if key in self.attributes: if key in self.array_values: - return self.attributes[key] - - if not len(self.attributes[key]) == 1: + self.cache[key] = self.attributes[key] + elif not len(self.attributes[key]) == 1: raise ValueError, 'non-array value has not exactly one value' - - if key in self.int_values: - return int(self.attributes[key][0]) + elif key in self.int_values: + self.cache[key] = int(self.attributes[key][0]) else: - return self.attributes[key][0] + self.cache[key] = self.attributes[key][0] elif key in self.defaults: - return self.defaults[key] + self.cache[key] = self.defaults[key] else: raise IndexError, "No such key: %s (dn: %s)"%(key, self.dn) + return self.cache[key] + def __contains__(self, key): return key in self.attributes