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)
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.
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
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
21 class Settings(Sqlite3DB):
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)
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),
40 self._storage.execute("INSERT INTO property(path, properties, session_id) VALUES "
42 self._sqlite_encode(properties),
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))
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))
55 return set(default_properties)
57 return set(self._sqlite_decode(value[0]))
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)
65 def reset_all_properties(self):
66 self._storage.execute("DELETE FROM property WHERE session_id = ?", (self._session_id,))
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))
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),
79 self._storage.execute("INSERT INTO permissive(path, permissives, session_id) "
80 "VALUES (?, ?, ?)", (path,
81 self._sqlite_encode(permissive),
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:
91 return frozenset(self._sqlite_decode(permissives[0]))
93 def get_modified_properties(self):
94 """return all modified settings in a dictionary
95 example: {'path1': set(['prop1', 'prop2'])}
98 for path, properties, _ in self._storage.select("SELECT * FROM property "
99 "WHERE session_id = ?",
102 path = self._sqlite_decode_path(path)
103 ret[path] = self._sqlite_decode(properties)
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_),
114 self._storage._conn.commit()
116 def get_modified_permissives(self):
117 """return all modified permissives in a dictionary
118 example: {'path1': set(['perm1', 'perm2'])}
121 for path, permissives in self._storage.select("SELECT * FROM permissive "
122 "WHERE session_id = ?",
125 path = self._sqlite_decode_path(path)
126 ret[path] = self._sqlite_decode(permissives)
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),
137 self._storage._conn.commit()