couple of tweaks. fixed up whitespacing for template, added a default __contains__...
authorBrian Harring <ferringb@gentoo.org>
Sun, 18 Dec 2005 11:02:51 +0000 (11:02 -0000)
committerBrian Harring <ferringb@gentoo.org>
Sun, 18 Dec 2005 11:02:51 +0000 (11:02 -0000)
finally added serialize_eclasses class attribute; controls whether __(get|set)item__ do _eclasses_ mangling on their own, or
leave it to the child class.

updated anydbm to handle serializing itself

svn path=/main/trunk/; revision=2395

pym/cache/anydbm.py
pym/cache/template.py

index 28d1a889e7d68407394e147bec5af2c618cc279f..2959e41e5d58fa9d89e25568e6deaa847ff28f19 100644 (file)
@@ -18,6 +18,7 @@ class database(fs_template.FsBased):
 
        autocommits = True
        cleanse_keys = True
+       serialize_eclasses = False
 
        def __init__(self, *args, **config):
                super(database,self).__init__(*args, **config)
@@ -50,13 +51,9 @@ class database(fs_template.FsBased):
        def iteritems(self):
                return self.__db.iteritems()
 
-       def __getitem__(self, cpv):
+       def _getitem(self, cpv):
                # we override getitem because it's just a cpickling of the data handed in.
-               d = pickle.loads(self.__db[cpv])
-                if "_eclasses_" in d:
-                        d["_eclasses_"] = reconstruct_eclasses(cpv, d["_eclasses_"])
-               return d
-
+               return pickle.loads(self.__db[cpv])
 
        def _setitem(self, cpv, values):
                self.__db[cpv] = pickle.dumps(values,pickle.HIGHEST_PROTOCOL)
@@ -64,15 +61,12 @@ class database(fs_template.FsBased):
        def _delitem(self, cpv):
                del self.__db[cpv]
 
-
        def iterkeys(self):
                return iter(self.__db)
 
-
        def has_key(self, cpv):
                return cpv in self.__db
 
-
        def __del__(self):
                if "__db" in self.__dict__ and self.__db != None:
                        self.__db.sync()
index c230b938944d409b576fbead51f9ad15f2f24a8a..f9c613b50d0cbb937f1741dbf14d63e771f699f6 100644 (file)
@@ -14,6 +14,7 @@ class database(object):
        complete_eclass_entries_ = True
        autocommits = False
        cleanse_keys = False
+       serialize_eclasses = True
 
        def __init__(self, location, label, auxdbkeys, readonly=False):
                """ initialize the derived class; specifically, store label/keys"""
@@ -23,7 +24,6 @@ class database(object):
                self.readonly = readonly
                self.sync_rate = 0
                self.updates = 0
-
        
        def __getitem__(self, cpv):
                """set a cpv to values
@@ -33,7 +33,7 @@ class database(object):
                        self.commit()
                        self.updates = 0
                d=self._getitem(cpv)
-               if "_eclasses_" in d:
+               if self.serialize_eclasses and "_eclasses_" in d:
                        d["_eclasses_"] = reconstruct_eclasses(cpv, d["_eclasses_"])
                return d
 
@@ -42,7 +42,6 @@ class database(object):
                override this in derived classess"""
                raise NotImplementedError
 
-
        def __setitem__(self, cpv, values):
                """set a cpv to values
                This shouldn't be overriden in derived classes since it handles the readonly checks"""
@@ -53,9 +52,9 @@ class database(object):
                        for k in d.keys():
                                if d[k] == '':
                                        del d[k]
-                       if "_eclasses_" in values:
+                       if self.serialize_eclasses and "_eclasses_" in values:
                                d["_eclasses_"] = serialize_eclasses(d["_eclasses_"])
-               elif "_eclasses_" in values:
+               elif self.serialize_eclasses and "_eclasses_" in values:
                        d = ProtectedDict(values)
                        d["_eclasses_"] = serialize_eclasses(d["_eclasses_"])
                else:
@@ -67,13 +66,11 @@ class database(object):
                                self.commit()
                                self.updates = 0
 
-
        def _setitem(self, name, values):
                """__setitem__ calls this after readonly checks.  override it in derived classes
                note _eclassees_ key *must* be handled"""
                raise NotImplementedError
 
-
        def __delitem__(self, cpv):
                """delete a key from the cache.
                This shouldn't be overriden in derived classes since it handles the readonly checks"""
@@ -86,16 +83,13 @@ class database(object):
                        self.commit()
                        self.updates = 0
 
-
        def _delitem(self,cpv):
                """__delitem__ calls this after readonly checks.  override it in derived classes"""
                raise NotImplementedError
 
-
        def has_key(self, cpv):
                raise NotImplementedError
 
-
        def keys(self):
                return tuple(self.iterkeys())
 
@@ -118,6 +112,9 @@ class database(object):
                if not self.autocommits:
                        raise NotImplementedError
 
+       def __contains__(self, cpv):
+               return self.has_key(cpv)
+
        def get_matches(self, match_dict):
                """generic function for walking the entire cache db, matching restrictions to
                filter what cpv's are returned.  Derived classes should override this if they
@@ -148,7 +145,6 @@ class database(object):
                                        cont = False
                                        break
                        if cont:
-#                              yield cpv,vals
                                yield cpv