1 # -*- coding: utf-8 -*-
2 "default plugin for value: set it in a simple dictionary"
3 # Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # ____________________________________________________________
21 from tiramisu.storage.sqlite3.storage import Cache
22 from tiramisu.setting import owners
26 __slots__ = ('__weakref__',)
28 def __init__(self, storage):
29 """init plugin means create values storage
31 # should init cache too
32 super(Values, self).__init__('value', storage)
33 values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
34 values_table += 'key, value text, owner text)'
35 self.storage.execute(values_table, commit=False)
36 informations_table = 'CREATE TABLE IF NOT EXISTS information(key text primary '
37 informations_table += 'key, value text)'
38 self.storage.execute(informations_table)
39 for owner in self.storage.select("SELECT DISTINCT owner FROM value", tuple(), False):
41 getattr(owners, owner[0])
42 except AttributeError:
43 owners.addowner(owner[0])
46 def _sqlite_select(self, path):
47 return self.storage.select("SELECT value FROM value WHERE path = ?",
51 def setvalue(self, path, value, owner):
52 """set value for an option
53 a specified value must be associated to an owner
56 path = self._sqlite_encode_path(path)
57 self.storage.execute("INSERT INTO value(path, value, owner) VALUES "
58 "(?, ?, ?)", (path, self._sqlite_encode(value),
61 def getvalue(self, path):
62 """get value for an option
63 return: only value, not the owner
65 path = self._sqlite_encode_path(path)
66 return self._sqlite_decode(self._sqlite_select(path)[0])
68 def hasvalue(self, path):
72 path = self._sqlite_encode_path(path)
73 return self._sqlite_select(path) is not None
75 def resetvalue(self, path):
76 """remove value means delete value in storage
78 path = self._sqlite_encode_path(path)
79 self.storage.execute("DELETE FROM value WHERE path = ?", (path,))
81 def get_modified_values(self):
82 """return all values in a dictionary
83 example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
86 for path, value, owner in self.storage.select("SELECT * FROM value",
88 path = self._sqlite_decode_path(path)
89 owner = getattr(owners, owner)
91 value = self._sqlite_decode(value)
92 ret[path] = (owner, value)
96 def setowner(self, path, owner):
97 """change owner for an option
99 path = self._sqlite_encode_path(path)
100 self.storage.execute("UPDATE value SET owner = ? WHERE path = ?",
103 def getowner(self, path, default):
104 """get owner for an option
107 path = self._sqlite_encode_path(path)
108 owner = self.storage.select("SELECT owner FROM value WHERE path = ?",
116 return getattr(owners, owner)
117 except AttributeError:
118 owners.addowner(owner)
119 return getattr(owners, owner)
121 def set_information(self, key, value):
122 """updates the information's attribute
123 (which is a dictionary)
125 :param key: information's key (ex: "help", "doc"
126 :param value: information's value (ex: "the help string")
128 self.storage.execute("DELETE FROM information WHERE key = ?", (key,),
130 self.storage.execute("INSERT INTO information(key, value) VALUES "
131 "(?, ?)", (key, self._sqlite_encode(value)))
133 def get_information(self, key):
134 """retrieves one information's item
136 :param key: the item string (ex: "help")
138 value = self.storage.select("SELECT value FROM information WHERE key = ?",
141 raise ValueError("not found")
143 return self._sqlite_decode(value[0])