@@ -0,0 +1,3209 @@
+%22%22%22Implements a db backed storage area for intermediate results%22%22%22%0Aimport sqlite3%0A%0A%0Aclass Store(object):%0A    %22%22%22%0A    Represents an sqlite3 backed storage area that's vaguely key value%0A    modeled for intermediate storage about metadata / data for metrics%0A    about multiple wikis that have some underlying country related basis%0A    %22%22%22%0A%0A    _initial_sql_ = %5B%0A        'CREATE TABLE IF NOT EXISTS meta (key, value);',%0A        'CREATE UNIQUE INDEX IF NOT EXISTS meta_key ON meta(key);',%0A        'CREATE TABLE IF NOT EXISTS wiki_meta (wiki, key, value);',%0A        'CREATE UNIQUE INDEX IF NOT EXISTS wiki_meta_key ON wiki_meta(wiki, key);',%0A        'CREATE TABLE IF NOT EXISTS country_info (wiki, country, key, value);',%0A        'CREATE UNIQUE INDEX IF NOT EXISTS country_info_key ON country_info(wiki, country, key);'%0A    %5D%0A%0A    def __init__(self, path):%0A        %22%22%22Initialize a store at the given path.%0A%0A        Creates the tables required if they do not exist%22%22%22%0A        self.db = sqlite3.connect(path)%0A        for sql in Store._initial_sql_:%0A            self.db.execute(sql)%0A%0A    def set_meta(self, key, value):%0A        %22%22%22Set generic metadata key value, global to the store%22%22%22%0A        self.db.execute(%22INSERT OR REPLACE INTO meta VALUES (?, ?)%22, (key, value))%0A        self.db.commit()%0A%0A    def get_meta(self, key):%0A        %22%22%22Get generic metadata key value, global to the store%22%22%22%0A        try:%0A            cur = self.db.cursor()%0A            cur.execute(%22SELECT value from meta WHERE key = ?%22, (key, ))%0A            cur.fetchone()%0A            return cur%5B0%5D%0A        finally:%0A            cur.close()%0A%0A    def set_wiki_meta(self, wiki, key, value):%0A        %22%22%22Set wiki specific meta key value%22%22%22%0A        self.db.execute(%22INSERT OR REPLACE INTO wiki_meta VALUES (?, ?, ?)%22, (wiki, key, value))%0A        self.db.commit()%0A%0A    def get_wiki_meta(self, key):%0A        %22%22%22Get wiki specific meta key value%22%22%22%0A        try:%0A            cur = self.db.cursor()%0A            cur.execute(%22SELECT value from wiki_meta WHERE wiki = ? AND key = ?%22, (wiki, key, ))%0A            cur.fetchone()%0A            return cur%5B0%5D%0A        finally:%0A            cur.close()%0A%0A    def set_country_info(self, wiki, country, key, value):%0A        %22%22%22Set a country and wiki specific key and value%22%22%22%0A        self.db.execute(%22INSERT OR REPLACE INTO country_info VALUES (?, ?, ?, ?)%22, (wiki, country, key, value))%0A        self.db.commit()%0A%0A    def set_country_info_bulk(self, wiki, key, country_dict):%0A        %22%22%22Bulk insert a dictionary of country specific key and value.%0A%0A        The dictionary should be of form %7B'country': 'value'%7D%0A        %22%22%22%0A        insert_data = %5B(wiki, k, key, v) for (k, v) in country_dict.iteritems()%5D%0A        self.db.executemany(%22INSERT OR REPLACE INTO country_info VALUES (?, ?, ?, ?)%22, insert_data)%0A        self.db.commit()%0A%0A    def get_country_info(self, wiki, country, key):%0A        %22%22%22Get a country and wiki specific value for a given key%22%22%22%0A        try:%0A            cur = self.db.cursor()%0A            cur.execute(%22SELECT value from country_info WHERE wiki = ? AND country = ?AND key = ?%22,%0A                (wiki, country, key, ))%0A            cur.fetchone()%0A            return cur%5B0%5D%0A        finally:%0A            cur.close()%0A

Add sqlite3 backed intermediate store helper class