112d3a53e5960ce9320b834c5ab2a2699f6c2935
[tiramisu.git] / tiramisu / storage / sqlalchemy / value.py
1 # -*- coding: utf-8 -*-
2 "plugin for value: set it in sqlalchemy"
3 # Copyright (C) 2013-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
19
20 from ..util import Cache
21 from .util import SqlAlchemyBase
22 import util
23 from sqlalchemy import Column, Integer, String, PickleType
24 from tiramisu.setting import owners
25
26
27 #____________________________________________________________
28 #
29 # information
30 class _Vinformation(SqlAlchemyBase):
31     __tablename__ = 'vinformation'
32     id = Column(Integer, primary_key=True)
33     session = Column(String, index=True)
34     path = Column(String, index=True)
35     key = Column(String)
36     value = Column(PickleType)
37
38     def __init__(self, session, key, value):
39         self.session = session
40         self.key = key
41         self.value = value
42
43
44 class Value(SqlAlchemyBase):
45     __tablename__ = 'value'
46     id = Column(Integer, primary_key=True)
47     session = Column(String, index=True)
48     path = Column(String, index=True)
49     key = Column(String)
50     value = Column(PickleType)
51     owner = Column(String)
52
53     def __init__(self, session, path, value, owner):
54         self.session = session
55         self.path = path
56         self.value = value
57         self.owner = owner
58
59
60 class Values(Cache):
61
62     # value
63     def setvalue(self, path, value, owner):
64         """set value for a path
65         a specified value must be associated to an owner
66         """
67         val = util.session.query(Value).filter_by(
68             path=path, session=self._storage.session_id).first()
69         if val is None:
70             util.session.add(Value(self._storage.session_id, path, value,
71                                    owner))
72         else:
73             val.value = value
74             val.owner = owner
75         util.session.commit()
76
77     def getvalue(self, path):
78         """get value for a path
79         return: only value, not the owner
80         """
81         val = util.session.query(Value).filter_by(
82             path=path, session=self._storage.session_id).first()
83         if not val:
84             raise KeyError('no value found')
85         return val.value
86
87     def hasvalue(self, path):
88         """if path has a value
89         return: boolean
90         """
91         return util.session.query(Value).filter_by(
92             path=path, session=self._storage.session_id).first() is not None
93
94     def resetvalue(self, path):
95         """remove value means delete value in storage
96         """
97         val = util.session.query(Value).filter_by(
98             path=path, session=self._storage.session_id).first()
99         if val is not None:
100             util.session.delete(val)
101             util.session.commit()
102
103     def get_modified_values(self):
104         """return all values in a dictionary
105         example: {'path1': (owner, 'value1'), 'path2': (owner, 'value2')}
106         """
107         ret = {}
108         for val in util.session.query(Value).filter_by(
109                 session=self._storage.session_id).all():
110             ret[val.path] = (val.owner, val.value)
111         return ret
112
113     # owner
114     def setowner(self, path, owner):
115         """change owner for a path
116         """
117         val = util.session.query(Value).filter_by(
118             path=path, session=self._storage.session_id).first()
119         if val is None:
120             raise KeyError('no value found')
121         else:
122             val.owner = owner
123         util.session.commit()
124
125     def getowner(self, path, default):
126         """get owner for a path
127         return: owner object
128         """
129         val = util.session.query(Value).filter_by(
130             path=path, session=self._storage.session_id).first()
131         if val is None:
132             return default
133         else:
134             owner = val.owner
135             # autocreate owners
136             try:
137                 return getattr(owners, owner)
138             except AttributeError:
139                 owners.addowner(owner)
140                 return getattr(owners, owner)
141
142     def set_information(self, key, value):
143         """updates the information's attribute
144         (which is a dictionary)
145
146         :param key: information's key (ex: "help", "doc"
147         :param value: information's value (ex: "the help string")
148         """
149         pass
150         val = util.session.query(_Vinformation).filter_by(
151             key=key, session=self._storage.session_id).first()
152         if val is None:
153             util.session.add(_Vinformation(self._storage.session_id, key,
154                                            value))
155         else:
156             val.value = value
157         util.session.commit()
158
159     def get_information(self, key):
160         """retrieves one information's item
161
162         :param key: the item string (ex: "help")
163         """
164         val = util.session.query(_Vinformation).filter_by(
165             key=key, session=self._storage.session_id).first()
166         if not val:
167             raise ValueError("not found")
168         return val.value