1 # -*- coding: utf-8 -*-
2 "default plugin for value: set it in a simple dictionary"
3 # Copyright (C) 2013-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 ...setting import undefined
24 __slots__ = ('_values', '_informations', '__weakref__')
26 def __init__(self, storage):
27 """init plugin means create values storage
29 self._values = (tuple(), tuple(), tuple(), tuple())
30 self._informations = {}
31 # should init cache too
32 super(Values, self).__init__(storage)
34 def _setvalue_info(self, nb, idx, value, values, index, vidx):
35 lst = list(self._values[nb])
37 if index is None or nb == 0:
42 if index is None or nb == 0:
47 vidx = lst[idx].index(index)
53 lst[idx] = tuple(tval)
57 lst[idx] = tuple(tval)
58 lst[idx] = tuple(lst[idx])
60 if isinstance(ls, list):
61 raise Exception('pouet')
62 values.append(tuple(lst))
65 def setvalue(self, path, value, owner, index):
66 """set value for a path
67 a specified value must be associated to an owner
72 if path in self._values[0]:
73 idx = self._values[0].index(path)
76 vidx = self._setvalue_info(0, idx, path, values, index, vidx)
77 vidx = self._setvalue_info(1, idx, index, values, index, vidx)
78 if isinstance(value, list):
80 vidx = self._setvalue_info(2, idx, value, values, index, vidx)
81 if isinstance(value, list):
83 self._setvalue_info(3, idx, owner, values, index, vidx)
84 self._values = tuple(values)
86 def getvalue(self, path, index=None):
87 """get value for a path
88 return: only value, not the owner
90 return self._getvalue(path, 2, index)
92 def hasvalue(self, path, index=None):
93 """if path has a value
96 return path in self._values[0]
98 def resetvalue(self, path):
99 """remove value means delete value in storage
102 lst = list(self._values[nb])
104 values.append(tuple(lst))
106 if path in self._values[0]:
107 idx = self._values[0].index(path)
112 self._values = tuple(values)
114 def get_modified_values(self):
115 """return all values in a dictionary
116 example: {'path1': (owner, 'value1'), 'path2': (owner, 'value2')}
119 for idx, path in enumerate(self._values[0]):
120 indexes = self._values[1][idx]
121 value = self._values[2][idx]
122 owner = self._values[3][idx]
123 if indexes is not None:
126 for cpt, index in enumerate(indexes):
127 val[str(index)] = value[cpt]
128 own[str(index)] = owner[cpt]
131 values[path] = (owner, value)
135 def setowner(self, path, owner, index=None):
136 """change owner for a path
138 idx = self._values[0].index(path)
142 vidx = self._values[1][idx].index(index)
144 self._setvalue_info(3, idx, owner, values, index, vidx)
145 lst = list(self._values)
146 lst[3] = tuple(values[0])
147 self._values = tuple(lst)
149 def get_max_length(self, path):
150 if path in self._values[0]:
151 idx = self._values[0].index(path)
154 return max(self._values[1][idx]) + 1
156 def getowner(self, path, default, index=None, only_default=False):
157 """get owner for a path
162 if path in self._values[0]:
166 val = self._getvalue(path, 3, index)
171 value = self._getvalue(path, 3, index)
177 def _getvalue(self, path, nb, index):
179 _values == ((path1, path2), ((idx1_1, idx1_2), None), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2))
181 if path in self._values[0]:
182 idx = self._values[0].index(path)
183 if isinstance(self._values[1][idx], tuple):
185 raise ValueError('index is mandatory')
186 elif index is not None:
187 raise ValueError('index is forbidden')
189 if self._values[1][idx] is None:
191 value = self._values[nb][idx]
193 value = self._values[nb][idx][index]
195 if index is not None:
196 if index in self._values[1][idx]:
197 subidx = self._values[1][idx].index(index)
198 value = self._values[nb][idx][subidx]
203 for i in xrange(0, max(self._values[1][idx])):
204 if i in self._values[1][idx]:
205 value.append(self._values[nb][idx][self._values[1][idx].index(i)])
207 value.append(undefined)
210 if isinstance(value, tuple):
214 def set_information(self, key, value):
215 """updates the information's attribute
216 (which is a dictionary)
218 :param key: information's key (ex: "help", "doc"
219 :param value: information's value (ex: "the help string")
221 self._informations[key] = value
223 def get_information(self, key, default):
224 """retrieves one information's item
226 :param key: the item string (ex: "help")
228 value = self._informations.get(key, default)
229 if value is undefined:
230 raise ValueError(_("information's item"
231 " not found: {0}").format(key))