6095979f5521c5a5d1bc74ba7c399d81a71ae589
[tiramisu.git] / tiramisu / storage / sqlalchemy / setting.py
1 # -*- coding: utf-8 -*-
2 "default plugin for setting: set it in a simple dictionary"
3 # Copyright (C) 2014 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 ..util import Cache
19 from .util import SqlAlchemyBase
20 import util
21 from sqlalchemy import Column, Integer, String, PickleType, ForeignKey
22 from sqlalchemy.orm import relationship
23 from sqlalchemy.ext.associationproxy import association_proxy
24 from sqlalchemy.orm.collections import attribute_mapped_collection
25
26
27 #____________________________________________________________
28 #
29 # properties|permissives
30 class _Property(SqlAlchemyBase):
31     __tablename__ = 'property'
32     id = Column(Integer, primary_key=True)
33     setting = Column(Integer, ForeignKey('settings.id'), nullable=False)
34     path = Column(String)
35     properties = Column(PickleType)
36
37     def __init__(self, path, properties):
38         self.path = path
39         self.properties = properties
40
41
42 class _Permissive (SqlAlchemyBase):
43     __tablename__ = 'permissive'
44     id = Column(Integer, primary_key=True)
45     setting = Column(Integer, ForeignKey('settings.id'), nullable=False)
46     path = Column(String)
47     permissives = Column(PickleType)
48
49     def __init__(self, path, permissives):
50         self.path = path
51         self.permissives = permissives
52
53
54 #____________________________________________________________
55 #FIXME marche pas le cache ... de toute facon je vais faire un storage separe !
56 class Settings(Cache, SqlAlchemyBase):
57     __tablename__ = 'settings'
58     id = Column(Integer, primary_key=True)
59     _props = relationship("_Property",
60                           collection_class=attribute_mapped_collection('path'),
61                           cascade="all, delete-orphan")
62     _properties = association_proxy("_props", "properties")
63     _perms = relationship("_Permissive",
64                           collection_class=attribute_mapped_collection('path'),
65                           cascade="all, delete-orphan")
66     _permissives = association_proxy("_perms", "permissives")
67
68     #def __init__(self, storage):
69     #    super(Settings, self).__init__(storage)
70
71     # properties
72     def setproperties(self, path, properties):
73         self._properties[path] = properties
74         util.session.commit()
75
76     def getproperties(self, path, default_properties):
77         return self._properties.get(path, set(default_properties))
78
79     def hasproperties(self, path):
80         return path in self._properties
81
82     def reset_all_properties(self):
83         self._properties.clear()
84         util.session.commit()
85
86     def delproperties(self, path):
87         try:
88             del(self._properties[path])
89             util.session.commit()
90         except KeyError:
91             pass
92
93     # permissive
94     def setpermissive(self, path, permissive):
95         self._permissives[path] = frozenset(permissive)
96         util.session.commit()
97
98     def getpermissive(self, path=None):
99         ret = self._permissives.get(path, frozenset())
100         #replace None by a frozenset()
101         return {None: frozenset()}.get(ret, ret)
102
103     def get_modified_properties(self):
104         """return all modified settings in a dictionary
105         example: {'path1': set(['prop1', 'prop2'])}
106         """
107         return self._properties
108
109     def get_modified_permissives(self):
110         """return all modified permissives in a dictionary
111         example: {'path1': set(['perm1', 'perm2'])}
112         """
113         return self._permissives