8f4484a19dfa072732f3da545c6174e22938ec63
[tiramisu.git] / tiramisu / storage / sqlite3 / setting.py
1 # -*- coding: utf-8 -*-
2 "default plugin for setting: set it in a simple dictionary"
3 # Copyright (C) 2013-2017 Team tiramisu (see AUTHORS for all contributors)
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 # ____________________________________________________________
18 from .sqlite3db import Sqlite3DB
19
20
21 class Settings(Sqlite3DB):
22     __slots__ = tuple()
23
24     def __init__(self, storage):
25         settings_table = 'CREATE TABLE IF NOT EXISTS property(path TEXT,'
26         settings_table += 'properties text, session_id TEXT, PRIMARY KEY(path, session_id))'
27         permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path TEXT,'
28         permissives_table += 'permissives TEXT, session_id TEXT, PRIMARY KEY(path, session_id))'
29         # should init cache too
30         super(Settings, self).__init__(storage)
31         self._storage.execute(settings_table, commit=False)
32         self._storage.execute(permissives_table)
33
34     # properties
35     def setproperties(self, path, properties):
36         path = self._sqlite_encode_path(path)
37         self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
38                               (path, self._session_id),
39                               False)
40         self._storage.execute("INSERT INTO property(path, properties, session_id) VALUES "
41                               "(?, ?, ?)", (path,
42                                          self._sqlite_encode(properties),
43                                          self._session_id))
44
45     def delproperties(self, path):
46         path = self._sqlite_encode_path(path)
47         self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
48                               (path, self._session_id))
49
50     def getproperties(self, path, default_properties):
51         path = self._sqlite_encode_path(path)
52         value = self._storage.select("SELECT properties FROM property WHERE "
53                                      "path = ? AND session_id = ?", (path, self._session_id))
54         if value is None:
55             return set(default_properties)
56         else:
57             return set(self._sqlite_decode(value[0]))
58
59     def hasproperties(self, path):
60         path = self._sqlite_encode_path(path)
61         return self._storage.select("SELECT properties FROM property WHERE "
62                                     "path = ? AND session_id = ?", (path, self._session_id)
63                                     ) is not None
64
65     def reset_all_properties(self):
66         self._storage.execute("DELETE FROM property WHERE session_id = ?", (self._session_id,))
67
68     def reset_properties(self, path):
69         path = self._sqlite_encode_path(path)
70         self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
71                               (path, self._session_id))
72
73     # permissive
74     def setpermissive(self, path, permissive):
75         path = self._sqlite_encode_path(path)
76         self._storage.execute("DELETE FROM permissive WHERE path = ? AND session_id = ?",
77                               (path, self._session_id),
78                               False)
79         self._storage.execute("INSERT INTO permissive(path, permissives, session_id) "
80                               "VALUES (?, ?, ?)", (path,
81                                                    self._sqlite_encode(permissive),
82                                                    self._session_id))
83
84     def getpermissive(self, path='_none'):
85         permissives = self._storage.select("SELECT permissives FROM "
86                                            "permissive WHERE path = ? AND session_id = ?",
87                                            (path, self._session_id))
88         if permissives is None:
89             return frozenset()
90         else:
91             return frozenset(self._sqlite_decode(permissives[0]))
92
93     def get_modified_properties(self):
94         """return all modified settings in a dictionary
95         example: {'path1': set(['prop1', 'prop2'])}
96         """
97         ret = {}
98         for path, properties, _ in self._storage.select("SELECT * FROM property "
99                                                         "WHERE session_id = ?",
100                                                         (self._session_id,),
101                                                         only_one=False):
102             path = self._sqlite_decode_path(path)
103             ret[path] = self._sqlite_decode(properties)
104         return ret
105
106     def set_modified_properties(self, properties):
107         self._storage.execute("DELETE FROM property", commit=False)
108         for path, property_ in properties.items():
109             self._storage.execute("INSERT INTO property(path, property, session_id) "
110                                   "VALUES (?, ?, ?)", (path,
111                                                        self._sqlite_encode(property_),
112                                                        self._session_id,
113                                                       ), False)
114         self._storage._conn.commit()
115
116     def get_modified_permissives(self):
117         """return all modified permissives in a dictionary
118         example: {'path1': set(['perm1', 'perm2'])}
119         """
120         ret = {}
121         for path, permissives in self._storage.select("SELECT * FROM permissive "
122                                                       "WHERE session_id = ?",
123                                                       (self._session_id,),
124                                                       only_one=False):
125             path = self._sqlite_decode_path(path)
126             ret[path] = self._sqlite_decode(permissives)
127         return ret
128
129     def set_modified_permissives(self, permissives):
130         self._storage.execute("DELETE FROM permissive", commit=False)
131         for path, permissive in permissives.items():
132             self._storage.execute("INSERT INTO permissive(path, permissive, session_id) "
133                                   "VALUES (?, ?, ?)", (path,
134                                                        self._sqlite_encode(permissive),
135                                                        self._session_id,
136                                                       ), False)
137         self._storage._conn.commit()