From fffef7bbc408b753b6d61699b800224ab77f18c4 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 11 Oct 2009 16:30:07 -0400 Subject: [PATCH] Implement sqlite dict. We might not actually use this. Signed-off-by: Edward Z. Yang --- doc/index.rst | 1 + doc/module/wizard.cache.rst | 11 ++++++ wizard/cache.py | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 doc/module/wizard.cache.rst create mode 100644 wizard/cache.py diff --git a/doc/index.rst b/doc/index.rst index a5ba181..48f7038 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -62,6 +62,7 @@ Modules :maxdepth: 1 module/wizard + module/wizard.cache module/wizard.deploy module/wizard.install module/wizard.shell diff --git a/doc/module/wizard.cache.rst b/doc/module/wizard.cache.rst new file mode 100644 index 0000000..a7cef06 --- /dev/null +++ b/doc/module/wizard.cache.rst @@ -0,0 +1,11 @@ +:mod:`wizard.cache` +=================== + +.. automodule:: wizard.cache + +Classes +------- +.. autoclass:: make + :members: +.. autoclass:: SerializedDict + :members: diff --git a/wizard/cache.py b/wizard/cache.py new file mode 100644 index 0000000..82de819 --- /dev/null +++ b/wizard/cache.py @@ -0,0 +1,69 @@ +""" +Provides caching of old results from mass commands, so that +we don't have to do them again. Successes are cached as +the string "OK"; errors are stored as the string exception name. +Entries are indexed by location. + +.. testsetup:: * + + from wizard.cache import * +""" + +import sqlite3 + +def make(file): + if file: + return SerializedDict(file) + else: + return {} + +class SerializedDict(object): + """ + Implements a dictionary that serializes itself to disk. We + use sqlite in order to implement relatively efficient updates. + + >>> import os + >>> f = "/tmp/wizard-cache-test.db" + >>> if os.path.exists(f): os.unlink(f) + >>> d = SerializedDict(f) + >>> try: + ... d["foo"] + ... raise Exception("didn't raise") + ... except KeyError: + ... pass + >>> d["foo"] = "baz" + >>> d["foo"] + 'baz' + >>> d["foo"] = "bar" + >>> d["foo"] + 'bar' + >>> e = SerializedDict(f) + >>> e["foo"] + 'bar' + >>> del e["foo"] + >>> "foo" in e + False + >>> os.unlink(f) + """ + #: Connection to sqlite database + conn = None + def __init__(self, file): + self.conn = sqlite3.connect(file) + try: + self.conn.execute('create table dict(key string primary key, value string)') + except sqlite3.OperationalError as e: + if e.args[0] != "table dict already exists": + raise + self.conn.isolation_level = None + def __getitem__(self, key): + row = self.conn.execute('select value from dict where key = ?', (key,)).fetchone() + if row: + return str(row[0]) + else: + raise KeyError + def __setitem__(self, key, value): + self.conn.execute('insert or replace into dict (key, value) values (?, ?)', (key, value)) + def __delitem__(self, key): + self.conn.execute('delete from dict where key = ?', (key,)) + def __contains__(self, key): + return bool(self.conn.execute('select count(*) from dict where key = ?', (key,)).fetchone()[0]) -- 2.44.0