impl_get_information and impl_set_information are, now, persistent in storage

This commit is contained in:
2013-09-03 10:38:28 +02:00
parent dcd6efd063
commit aeeaf6ec14
7 changed files with 143 additions and 54 deletions

View File

@ -22,12 +22,13 @@ from tiramisu.storage.dictionary.storage import Cache
class Values(Cache):
__slots__ = ('_values', '__weakref__')
__slots__ = ('_values', '_informations', '__weakref__')
def __init__(self, storage):
"""init plugin means create values storage
"""
self._values = {}
self._informations = {}
# should init cache too
super(Values, self).__init__(storage)
@ -72,3 +73,22 @@ class Values(Cache):
return: owner object
"""
return self._values.get(path, (default, None))[0]
def set_information(self, key, value):
"""updates the information's attribute
(which is a dictionary)
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
self._informations[key] = value
def get_information(self, key):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
if key in self._informations:
return self._informations[key]
else:
raise ValueError("not found")

View File

@ -32,7 +32,10 @@ class Values(Cache):
super(Values, self).__init__('value', storage)
values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
values_table += 'key, value text, owner text)'
self.storage.execute(values_table)
self.storage.execute(values_table, commit=False)
informations_table = 'CREATE TABLE IF NOT EXISTS information(key text primary '
informations_table += 'key, value text)'
self.storage.execute(informations_table)
for owner in self.storage.select("SELECT DISTINCT owner FROM value", tuple(), False):
try:
getattr(owners, owner[0])
@ -114,3 +117,27 @@ class Values(Cache):
except AttributeError:
owners.addowner(owner)
return getattr(owners, owner)
def set_information(self, key, value):
"""updates the information's attribute
(which is a dictionary)
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
self.storage.execute("DELETE FROM information WHERE key = ?", (key,),
False)
self.storage.execute("INSERT INTO information(key, value) VALUES "
"(?, ?)", (key, self._sqlite_encode(value)))
def get_information(self, key):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
value = self.storage.select("SELECT value FROM information WHERE key = ?",
(key,))
if value is None:
raise ValueError("not found")
else:
return self._sqlite_decode(value[0])