# Copyright 1999-2006 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header: $ import template from metadata_overlay import database as db_module class database(db_module): def __init__(self, location, label, auxdbkeys, **config): super(database, self).__init__(location, label, auxdbkeys, **config) self._db_cache = {} def __getitem__(self, cpv): """Each item will remain cached in memory until reset() is called.""" try: return self._db_cache[cpv].copy() except KeyError: pass # This raises a KeyError if necessary. data = self._copy_dict(super(database, self).__getitem__(cpv)) self._db_cache[cpv] = data return data.copy() def _setitem(self, name, values): my_values = self._copy_dict(values) self._db_cache[name] = my_values super(database, self)._setitem(name, my_values) def _delitem(self, cpv): super(database, self)._delitem(cpv) try: del self._db_cache[cpv] except KeyError: pass def has_key(self, cpv): return cpv in self._db_cache or super(database, self).has_key(cpv) def reset(self): self._db_cache = {} def _copy_dict(self, d): """For mappings that don't support the copy() method""" data = {} data.update(d) return data