many improvment
[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         super(Settings, self).__init__(storage)
26
27     # properties
28     def setproperties(self, path, properties):
29         path = self._sqlite_encode_path(path)
30         self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
31                               (path, self._session_id),
32                               False)
33         self._storage.execute("INSERT INTO property(path, properties, session_id) VALUES "
34                               "(?, ?, ?)", (path,
35                                          self._sqlite_encode(properties),
36                                          self._session_id))
37
38     def delproperties(self, path):
39         path = self._sqlite_encode_path(path)
40         self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
41                               (path, self._session_id))
42
43     def getproperties(self, path, default_properties):
44         path = self._sqlite_encode_path(path)
45         value = self._storage.select("SELECT properties FROM property WHERE "
46                                      "path = ? AND session_id = ? LIMIT 1", (path, self._session_id))
47         if value is None:
48             return set(default_properties)
49         else:
50             return set(self._sqlite_decode(value[0]))
51
52     def hasproperties(self, path):
53         path = self._sqlite_encode_path(path)
54         return self._storage.select("SELECT properties FROM property WHERE "
55                                     "path = ? AND session_id = ? LIMIT 1", (path, self._session_id)
56                                     ) is not None
57
58     def reset_all_properties(self):
59         self._storage.execute("DELETE FROM property WHERE session_id = ?", (self._session_id,))
60
61     def reset_properties(self, path):
62         path = self._sqlite_encode_path(path)
63         self._storage.execute("DELETE FROM property WHERE path = ? AND session_id = ?",
64                               (path, self._session_id))
65
66     # permissive
67     def setpermissive(self, path, permissive):
68         path = self._sqlite_encode_path(path)
69         self._storage.execute("DELETE FROM permissive WHERE path = ? AND session_id = ?",
70                               (path, self._session_id),
71                               False)
72         self._storage.execute("INSERT INTO permissive(path, permissives, session_id) "
73                               "VALUES (?, ?, ?)", (path,
74                                                    self._sqlite_encode(permissive),
75                                                    self._session_id))
76
77     def getpermissive(self, path='_none'):
78         permissives = self._storage.select("SELECT permissives FROM "
79                                            "permissive WHERE path = ? AND session_id = ? LIMIT 1",
80                                            (path, self._session_id))
81         if permissives is None:
82             return frozenset()
83         else:
84             return frozenset(self._sqlite_decode(permissives[0]))
85
86     def get_modified_properties(self):
87         """return all modified settings in a dictionary
88         example: {'path1': set(['prop1', 'prop2'])}
89         """
90         ret = {}
91         for path, properties, _ in self._storage.select("SELECT * FROM property "
92                                                         "WHERE session_id = ?",
93                                                         (self._session_id,),
94                                                         only_one=False):
95             path = self._sqlite_decode_path(path)
96             ret[path] = self._sqlite_decode(properties)
97         return ret
98
99     def set_modified_properties(self, properties):
100         self._storage.execute("DELETE FROM property", commit=False)
101         for path, property_ in properties.items():
102             self._storage.execute("INSERT INTO property(path, property, session_id) "
103                                   "VALUES (?, ?, ?)", (path,
104                                                        self._sqlite_encode(property_),
105                                                        self._session_id,
106                                                       ), False)
107         self._storage._conn.commit()
108
109     def get_modified_permissives(self):
110         """return all modified permissives in a dictionary
111         example: {'path1': set(['perm1', 'perm2'])}
112         """
113         ret = {}
114         for path, permissives in self._storage.select("SELECT * FROM permissive "
115                                                       "WHERE session_id = ?",
116                                                       (self._session_id,),
117                                                       only_one=False):
118             path = self._sqlite_decode_path(path)
119             ret[path] = self._sqlite_decode(permissives)
120         return ret
121
122     def set_modified_permissives(self, permissives):
123         self._storage.execute("DELETE FROM permissive", commit=False)
124         for path, permissive in permissives.items():
125             self._storage.execute("INSERT INTO permissive(path, permissive, session_id) "
126                                   "VALUES (?, ?, ?)", (path,
127                                                        self._sqlite_encode(permissive),
128                                                        self._session_id,
129                                                       ), False)
130         self._storage._conn.commit()