2 Provides caching of old results from mass commands, so that
3 we don't have to do them again. Successes are cached as
4 the string "OK"; errors are stored as the string exception name.
5 Entries are indexed by location.
9 from wizard.cache import *
16 return SerializedDict(file)
20 class SerializedDict(object):
22 Implements a dictionary that serializes itself to disk. We
23 use sqlite in order to implement relatively efficient updates.
26 >>> f = "/tmp/wizard-cache-test.db"
27 >>> if os.path.exists(f): os.unlink(f)
28 >>> d = SerializedDict(f)
31 ... raise Exception("didn't raise")
40 >>> e = SerializedDict(f)
48 #: Connection to sqlite database
50 def __init__(self, file):
51 self.conn = sqlite3.connect(file)
53 self.conn.execute('create table dict(key string primary key, value string)')
54 except sqlite3.OperationalError as e:
55 if e.args[0] != "table dict already exists":
57 self.conn.isolation_level = None
58 def __getitem__(self, key):
59 row = self.conn.execute('select value from dict where key = ?', (key,)).fetchone()
64 def __setitem__(self, key, value):
65 self.conn.execute('insert or replace into dict (key, value) values (?, ?)', (key, value))
66 def __delitem__(self, key):
67 self.conn.execute('delete from dict where key = ?', (key,))
68 def __contains__(self, key):
69 return bool(self.conn.execute('select count(*) from dict where key = ?', (key,)).fetchone()[0])