cache is always a dictionary in memory
[tiramisu.git] / tiramisu / storage / sqlite3 / value.py
index 00be15c..3f76e2c 100644 (file)
 #
 # ____________________________________________________________
 
-from tiramisu.storage.sqlite3.storage import Cache
+from .sqlite3db import Sqlite3DB
 from tiramisu.setting import owners
 
 
-class Values(Cache):
-    __slots__ = tuple()
+class Values(Sqlite3DB):
+    __slots__ = ('__weakref__',)
 
     def __init__(self, storage):
         """init plugin means create values storage
         """
+        # should init cache too
+        super(Values, self).__init__(storage)
         values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
         values_table += 'key, value text, owner text)'
-        # should init cache too
-        super(Values, self).__init__('value', storage)
-        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])
+            except AttributeError:
+                owners.addowner(owner[0])
 
     # sqlite
     def _sqlite_select(self, path):
@@ -70,12 +78,12 @@ class Values(Cache):
         path = self._sqlite_encode_path(path)
         self.storage.execute("DELETE FROM value WHERE path = ?", (path,))
 
-    def get_modified_values(self, context):
+    def get_modified_values(self):
         """return all values in a dictionary
         example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
         """
         ret = {}
-        for path, value, owner in self.storage.select("SELECT value",
+        for path, value, owner in self.storage.select("SELECT * FROM value",
                                                       only_one=False):
             path = self._sqlite_decode_path(path)
             owner = getattr(owners, owner)
@@ -102,4 +110,34 @@ class Values(Cache):
         if owner is None:
             return default
         else:
-            return getattr(owners, owner[0])
+            owner = owner[0]
+            # autocreate owners
+            try:
+                return getattr(owners, owner)
+            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])