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)
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 ..util import Cache
19 from .util import SqlAlchemyBase
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
27 #____________________________________________________________
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)
35 properties = Column(PickleType)
37 def __init__(self, path, properties):
39 self.properties = properties
42 class _Permissive (SqlAlchemyBase):
43 __tablename__ = 'permissive'
44 id = Column(Integer, primary_key=True)
45 setting = Column(Integer, ForeignKey('settings.id'), nullable=False)
47 permissives = Column(PickleType)
49 def __init__(self, path, permissives):
51 self.permissives = permissives
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 session_id = Column(String, index=True)
60 _props = relationship("_Property",
61 collection_class=attribute_mapped_collection('path'),
62 cascade="all, delete-orphan")
63 _properties = association_proxy("_props", "properties")
64 _perms = relationship("_Permissive",
65 collection_class=attribute_mapped_collection('path'),
66 cascade="all, delete-orphan")
67 _permissives = association_proxy("_perms", "permissives")
69 def __init__(self, session_id, storage):
70 session = self.getsession()
71 self.session_id = session_id
72 super(Settings, self).__init__(storage)
79 def setproperties(self, path, properties):
80 session = self.getsession()
81 self._properties[path] = properties
85 def getproperties(self, path, default_properties):
86 return self._properties.get(path, set(default_properties))
88 def hasproperties(self, path):
89 return path in self._properties
91 def reset_all_properties(self):
92 session = self.getsession()
93 self._properties.clear()
97 def delproperties(self, path):
99 session = self.getsession()
100 del(self._properties[path])
107 def setpermissive(self, path, permissive):
108 session = self.getsession()
109 self._permissives[path] = frozenset(permissive)
112 def getpermissive(self, path=None):
113 ret = self._permissives.get(path, frozenset())
114 #replace None by a frozenset()
115 return {None: frozenset()}.get(ret, ret)
117 def get_modified_properties(self):
118 """return all modified settings in a dictionary
119 example: {'path1': set(['prop1', 'prop2'])}
121 return self._properties
123 def get_modified_permissives(self):
124 """return all modified permissives in a dictionary
125 example: {'path1': set(['perm1', 'perm2'])}
127 return self._permissives
130 def delete_session(session_id, session):
131 print session.query(_Property).all()
132 print session.query(_Permissive).all()
133 settings_id = session.query(Settings).filter_by(session_id=session_id).first().id
134 for val in session.query(_Property).filter_by(settings=settings_id).all():
136 for val in session.query(_Permissive).filter_by(settings=settings_id).all():