# HG changeset patch # User Goffi # Date 1554655497 -7200 # Node ID c652d079a9a155ef0e138174f1fbd7549d362b4f # Parent 28fe69b87ba4084ad9296c8941516d9ab8a5a6fd core (memory/persistent): added clear method + initialise with a dict: A "clear" method has been added to delete all data for a specific namespace (and profile). Internal cache (self._cache) is not initialised with an empty dict instead of None to allow using methods like __setitem__ or clear without having to do a potentially expansive `load()` before. The only counterpart is that we won't have an exception anymore if developer forget to load() data before using them, which can make debugging a bit more difficult. diff -r 28fe69b87ba4 -r c652d079a9a1 sat/memory/persistent.py --- a/sat/memory/persistent.py Sun Apr 07 18:38:18 2019 +0200 +++ b/sat/memory/persistent.py Sun Apr 07 18:44:57 2019 +0200 @@ -44,7 +44,7 @@ if not self.storage: log.error(_("PersistentDict can't be used before memory initialisation")) raise MemoryNotInitializedError - self._cache = None + self._cache = {} self.namespace = namespace self.profile = profile @@ -111,13 +111,19 @@ return self._cache.__getitem__(key) def __setitem__(self, key, value): - self.storage.setPrivateValue(self.namespace, key, value, self.binary, self.profile) + self.storage.setPrivateValue(self.namespace, key, value, self.binary, + self.profile) return self._cache.__setitem__(key, value) def __delitem__(self, key): self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile) return self._cache.__delitem__(key) + def clear(self): + """Delete all values from this namespace""" + self._cache.clear() + return self.storage.delPrivateNamespace(self.namespace, self.binary, self.profile) + def get(self, key, default=None): return self._cache.get(key, default) @@ -133,7 +139,8 @@ @return: deferred fired when data is actually saved """ - return self.storage.setPrivateValue(self.namespace, name, self._cache[name], self.binary, self.profile) + return self.storage.setPrivateValue(self.namespace, name, self._cache[name], + self.binary, self.profile) class PersistentBinaryDict(PersistentDict):