ConstError in tiramisu/error.py
[tiramisu.git] / tiramisu / storage / sqlite3 / value.py
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)
4 #
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.
9 #
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.
14 #
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
18 #
19 # ____________________________________________________________
20
21 from tiramisu.storage.sqlite3.storage import Cache
22 from tiramisu.setting import owners
23
24
25 class Values(Cache):
26     __slots__ = tuple()
27
28     def __init__(self, storage):
29         """init plugin means create values storage
30         """
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)
36         for owner in self.storage.select("SELECT DISTINCT owner FROM value", tuple(), False):
37             try:
38                 getattr(owners, owner[0])
39             except AttributeError:
40                 owners.add_owner(owner[0])
41
42     # sqlite
43     def _sqlite_select(self, path):
44         return self.storage.select("SELECT value FROM value WHERE path = ?",
45                                    (path,))
46
47     # value
48     def setvalue(self, path, value, owner):
49         """set value for an option
50         a specified value must be associated to an owner
51         """
52         self.resetvalue(path)
53         path = self._sqlite_encode_path(path)
54         self.storage.execute("INSERT INTO value(path, value, owner) VALUES "
55                              "(?, ?, ?)", (path, self._sqlite_encode(value),
56                                            str(owner)))
57
58     def getvalue(self, path):
59         """get value for an option
60         return: only value, not the owner
61         """
62         path = self._sqlite_encode_path(path)
63         return self._sqlite_decode(self._sqlite_select(path)[0])
64
65     def hasvalue(self, path):
66         """if opt has a value
67         return: boolean
68         """
69         path = self._sqlite_encode_path(path)
70         return self._sqlite_select(path) is not None
71
72     def resetvalue(self, path):
73         """remove value means delete value in storage
74         """
75         path = self._sqlite_encode_path(path)
76         self.storage.execute("DELETE FROM value WHERE path = ?", (path,))
77
78     def get_modified_values(self):
79         """return all values in a dictionary
80         example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
81         """
82         ret = {}
83         for path, value, owner in self.storage.select("SELECT * FROM value",
84                                                       only_one=False):
85             path = self._sqlite_decode_path(path)
86             owner = getattr(owners, owner)
87
88             value = self._sqlite_decode(value)
89             ret[path] = (owner, value)
90         return ret
91
92     # owner
93     def setowner(self, path, owner):
94         """change owner for an option
95         """
96         path = self._sqlite_encode_path(path)
97         self.storage.execute("UPDATE value SET owner = ? WHERE path = ?",
98                              (str(owner), path))
99
100     def getowner(self, path, default):
101         """get owner for an option
102         return: owner object
103         """
104         path = self._sqlite_encode_path(path)
105         owner = self.storage.select("SELECT owner FROM value WHERE path = ?",
106                                     (path,))
107         if owner is None:
108             return default
109         else:
110             owner = owner[0]
111             # autocreate owners
112             try:
113                 return getattr(owners, owner)
114             except AttributeError:
115                 owners.add_owner(owner)
116                 return getattr(owners, owner)