# -*- coding: utf-8 -*- """ Trac Plugin for Monotone Copyright 2006-2008 Thomas Moschny (thomas.moschny@gmx.de) {{{ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }}} """ backends = {} def add_backend(name, factory): backends[name] = factory def get_cache(realm, cachespec): cachespec = cachespec.split(':') backend = cachespec[0] args = cachespec[1:] return backends[backend](realm, *args) # ---------------------------------------------------------------------- class LocalMem(object): """A handle to a simple dictionary.""" caches = {} def __init__(self, realm, prefix=''): name = prefix + realm self.cache = self.caches.setdefault(name, {}) def __getitem__(self, key): return self.cache[key] def __setitem__(self, key, value): self.cache[key] = value def close(self): pass add_backend('localmem', LocalMem) # ---------------------------------------------------------------------- try: import shelve import dbm except ImportError: dbm = None class DBMShelve(object): """Using Python's shelve on a DBM database.""" def __init__(self, realm, prefix): self.dbname = prefix + realm self.cache = shelve.Shelf(dbm.open(self.dbname, 'c')) def __getitem__(self, key): return self.cache[key] def __setitem__(self, key, value): self.cache[key] = value def close(self): self.cache.close() if dbm: add_backend('dbmshelve', DBMShelve) # ---------------------------------------------------------------------- try: from bsddb3 import dbshelve, db except ImportError: try: from bsddb import dbshelve, db except ImportError: dbshelve, db = None, None class BSDDBShelve(object): """Using the bsddb3 interface to db.""" # fixme: using reference counting, and passing the DB_THREAD flag # to bsddb, we could probably do with only one env and db handle # for all threads. def __init__(self, realm, dir, prefix=''): name = prefix + realm self.dbenv = db.DBEnv() self.dbenv.open( dir, db.DB_INIT_CDB|db.DB_INIT_MPOOL| db.DB_CREATE) self.cache = dbshelve.open( name, flags=db.DB_CREATE, dbenv=self.dbenv) def __getitem__(self, key): return self.cache[key] def __setitem__(self, key, value): self.cache[key] = value def close(self): self.cache.close() self.dbenv.close() if db: add_backend('bsddb', BSDDBShelve)