UDLdap.py: make a cache for __getitem__() decisions.
authorPeter Palfrader <peter@palfrader.org>
Mon, 12 Mar 2012 14:47:43 +0000 (15:47 +0100)
committerPeter Palfrader <peter@palfrader.org>
Mon, 12 Mar 2012 14:47:43 +0000 (15:47 +0100)
UDLdap.py
debian/changelog

index b546112..3c0f0ef 100644 (file)
--- 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
 
index 6b9399a..33974b4 100644 (file)
@@ -43,6 +43,7 @@ userdir-ldap (0.3.80) UNRELEASED; urgency=low
       TarInfo object without referring to any on-disk files.
     - get rid of global state variable CurrentHost.  This will enable upcoming
       changes.
+    - UDLdap.py: make a cache for __getitem__() decisions.
 
   [ Stephen Gran ]
   * Fix deprecation warnings for sha module by using hashlib module instead
@@ -55,7 +56,7 @@ userdir-ldap (0.3.80) UNRELEASED; urgency=low
   * ud-replicate: set correct permissions for web-passwords
   * add freecdb to depends
 
- -- Peter Palfrader <weasel@debian.org>  Mon, 12 Mar 2012 15:15:36 +0100
+ -- Peter Palfrader <weasel@debian.org>  Mon, 12 Mar 2012 15:47:13 +0100
 
 userdir-ldap (0.3.79) unstable; urgency=low