Merge branch 'master' of ssh://git.labs.libre-entreprise.org/gitroot/tiramisu
[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 tiramisu.storage.sqlite3.storage 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         self.storage.execute("DELETE FROM property WHERE path = ?", (path,),
39                              False)
40         self.storage.execute("INSERT INTO property(path, properties) VALUES "
41                              "(?, ?)", (path,
42                                         self._sqlite_encode(properties)))
43
44     def getproperties(self, path, default_properties):
45         value = self.storage.select("SELECT properties FROM property WHERE "
46                                     "path = ?", (path,))
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         return self.storage.select("SELECT properties FROM property WHERE "
54                                    "path = ?", (path,)) is not None
55
56     def reset_all_propertives(self):
57         self.storage.execute("DELETE FROM property")
58
59     def reset_properties(self, path):
60         self.storage.execute("DELETE FROM property WHERE path = ?", (path,))
61
62     def get_properties(self, context):
63         """return all properties in a dictionary
64         """
65         ret = {}
66         for path, properties in self.storage.select("SELECT * FROM property",
67                                                     only_one=False):
68             if path == '_none':
69                 opt = None
70             else:
71                 opt = context.cfgimpl_get_description().impl_get_opt_by_path(
72                     path)
73             properties = self._sqlite_decode(properties)
74             ret[opt] = properties
75         return ret
76
77     # permissive
78     def setpermissive(self, path, permissive):
79         self.storage.execute("DELETE FROM permissive WHERE path = ?", (path,),
80                              False)
81         self.storage.execute("INSERT INTO permissive(path, permissives) "
82                              "VALUES (?, ?)", (path,
83                                                self._sqlite_encode(permissive)
84                                                ))
85
86     def getpermissive(self, path='_none'):
87         permissives = self.storage.select("SELECT permissives FROM "
88                                           "permissive WHERE path = ?",
89                                           (path,))
90         if permissives is None:
91             return frozenset()
92         else:
93             return frozenset(self._sqlite_decode(permissives[0]))