storage no more in setting.py, code is now in storage/__init__.py
[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 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 from .cache import Cache
21
22
23 class Settings(Cache):
24     __slots__ = tuple()
25
26     def __init__(self, storage):
27         settings_table = 'CREATE TABLE IF NOT EXISTS property(path text '
28         settings_table += 'primary key, properties text)'
29         permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path text '
30         permissives_table += 'primary key, permissives text)'
31         # should init cache too
32         super(Settings, self).__init__('property', storage)
33         self.storage.execute(settings_table, commit=False)
34         self.storage.execute(permissives_table)
35
36     # propertives
37     def setproperties(self, path, properties):
38         path = self._sqlite_encode_path(path)
39         self.storage.execute("DELETE FROM property WHERE path = ?", (path,),
40                              False)
41         self.storage.execute("INSERT INTO property(path, properties) VALUES "
42                              "(?, ?)", (path,
43                                         self._sqlite_encode(properties)))
44
45     def getproperties(self, path, default_properties):
46         path = self._sqlite_encode_path(path)
47         value = self.storage.select("SELECT properties FROM property WHERE "
48                                     "path = ?", (path,))
49         if value is None:
50             return set(default_properties)
51         else:
52             return set(self._sqlite_decode(value[0]))
53
54     def hasproperties(self, path):
55         path = self._sqlite_encode_path(path)
56         return self.storage.select("SELECT properties FROM property WHERE "
57                                    "path = ?", (path,)) is not None
58
59     def reset_all_propertives(self):
60         self.storage.execute("DELETE FROM property")
61
62     def reset_properties(self, path):
63         path = self._sqlite_encode_path(path)
64         self.storage.execute("DELETE FROM property WHERE path = ?", (path,))
65
66     def get_properties(self, context):
67         """return all properties in a dictionary
68         """
69         ret = {}
70         for path, properties in self.storage.select("SELECT * FROM property",
71                                                     only_one=False):
72             path = self._sqlite_decode_path(path)
73             properties = self._sqlite_decode(properties)
74             ret[path] = properties
75         return ret
76
77     # permissive
78     def setpermissive(self, path, permissive):
79         path = self._sqlite_encode_path(path)
80         self.storage.execute("DELETE FROM permissive WHERE path = ?", (path,),
81                              False)
82         self.storage.execute("INSERT INTO permissive(path, permissives) "
83                              "VALUES (?, ?)", (path,
84                                                self._sqlite_encode(permissive)
85                                                ))
86
87     def getpermissive(self, path='_none'):
88         permissives = self.storage.select("SELECT permissives FROM "
89                                           "permissive WHERE path = ?",
90                                           (path,))
91         if permissives is None:
92             return frozenset()
93         else:
94             return frozenset(self._sqlite_decode(permissives[0]))