tiramisu/tiramisu/storage/dictionary/value.py

304 lines
10 KiB
Python
Raw Normal View History

2013-08-14 23:06:31 +02:00
# -*- coding: utf-8 -*-
"default plugin for value: set it in a simple dictionary"
2019-02-12 06:55:47 +01:00
# Copyright (C) 2013-2019 Team tiramisu (see AUTHORS for all contributors)
2013-08-14 23:06:31 +02:00
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
2013-08-14 23:06:31 +02:00
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
2013-08-14 23:06:31 +02:00
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-08-14 23:06:31 +02:00
# ____________________________________________________________
2013-09-22 20:57:52 +02:00
from ..util import Cache
from ...setting import undefined
2015-12-28 22:00:46 +01:00
from ...i18n import _
2019-02-24 19:03:00 +01:00
from ...log import log
2017-12-02 22:53:57 +01:00
2013-08-20 09:47:12 +02:00
class Values(Cache):
__slots__ = ('_values',
'_informations',
'__weakref__')
2013-08-14 23:06:31 +02:00
2013-08-20 22:45:11 +02:00
def __init__(self, storage):
2013-08-14 23:06:31 +02:00
"""init plugin means create values storage
"""
#(('path1',), (index1,), (value1,), ('owner1'))
2015-11-19 22:25:00 +01:00
self._values = (tuple(), tuple(), tuple(), tuple())
self._informations = {}
2013-08-14 23:06:31 +02:00
# should init cache too
2013-08-27 09:46:52 +02:00
super(Values, self).__init__(storage)
2013-08-14 23:06:31 +02:00
2017-07-16 23:11:12 +02:00
def commit(self):
pass
2016-03-09 15:48:14 +01:00
def _setvalue_info(self, nb, idx, value, values, index, vidx):
lst = list(self._values[nb])
if idx is None:
if index is None or nb == 0:
lst.append(value)
else:
lst.append((value,))
else:
if index is None or nb == 0:
lst[idx] = value
else:
if nb == 1:
if index in lst[idx]:
vidx = lst[idx].index(index)
else:
vidx = None
if vidx is None:
tval = list(lst[idx])
tval.append(value)
lst[idx] = tuple(tval)
elif nb != 1:
tval = list(lst[idx])
tval[vidx] = value
lst[idx] = tuple(tval)
lst[idx] = tuple(lst[idx])
values.append(tuple(lst))
return vidx
2013-08-14 23:06:31 +02:00
# value
2018-06-09 18:59:40 +02:00
def setvalue(self,
path,
value,
owner,
index,
commit):
"""set value for a path
2013-08-14 23:06:31 +02:00
a specified value must be associated to an owner
"""
2019-02-24 19:03:00 +01:00
log.debug('setvalue', path, value, owner, index, id(self))
2015-11-19 22:25:00 +01:00
values = []
vidx = None
2013-08-14 23:06:31 +02:00
2015-12-28 22:00:46 +01:00
if path in self._values[0]:
2015-11-19 22:25:00 +01:00
idx = self._values[0].index(path)
2015-12-28 22:00:46 +01:00
else:
2015-11-19 22:25:00 +01:00
idx = None
2016-03-09 15:48:14 +01:00
vidx = self._setvalue_info(0, idx, path, values, index, vidx)
vidx = self._setvalue_info(1, idx, index, values, index, vidx)
if isinstance(value, list):
value = tuple(value)
vidx = self._setvalue_info(2, idx, value, values, index, vidx)
self._setvalue_info(3, idx, owner, values, index, vidx)
2015-11-19 22:25:00 +01:00
self._values = tuple(values)
def hasvalue(self, path, index=None):
"""if path has a value
2013-08-14 23:06:31 +02:00
return: boolean
"""
2017-11-23 16:56:14 +01:00
has_path = path in self._values[0]
2019-02-24 19:03:00 +01:00
log.debug('hasvalue', path, index, has_path, id(self))
2017-11-23 16:56:14 +01:00
if index is None:
return has_path
elif has_path:
path_idx = self._values[0].index(path)
indexes = self._values[1][path_idx]
return index in indexes
return False
2013-08-14 23:06:31 +02:00
2017-10-22 15:10:50 +02:00
def reduce_index(self, path, index):
"""
_values == ((path1, path2), ((idx1_1, idx1_2), None), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2))
"""
2019-02-24 19:03:00 +01:00
log.debug('reduce_index', path, index, id(self))
2017-10-22 15:10:50 +02:00
path_idx = self._values[0].index(path)
2018-06-09 18:59:40 +02:00
# get the "index" position
subidx = self._values[1][path_idx].index(index)
# transform tuple to list
values = list(self._values)
values_idx = list(values[1])
lvalues = list(values_idx[path_idx])
# reduce to one the index
lvalues[subidx] = lvalues[subidx] - 1
# store modification
values_idx[path_idx] = tuple(lvalues)
values[1] = tuple(values_idx)
self._values = tuple(values)
2017-10-22 15:10:50 +02:00
2018-10-31 16:08:22 +01:00
def resetvalue_index(self,
path,
index,
commit):
2019-02-24 19:03:00 +01:00
log.debug('resetvalue_index', path, index, id(self))
2017-10-22 15:10:50 +02:00
def _resetvalue(nb):
values_idx = list(values[nb])
del(values_idx[path_idx])
values[nb] = tuple(values_idx)
def _resetvalue_index(nb):
values_idx = list(values[nb])
lvalues = list(values_idx[path_idx])
del(lvalues[subidx])
values_idx[path_idx] = tuple(lvalues)
values[nb] = tuple(values_idx)
path_idx = self._values[0].index(path)
indexes = self._values[1][path_idx]
if index in indexes:
subidx = indexes.index(index)
values = list(self._values)
if len(values[1][path_idx]) == 1:
_resetvalue(0)
_resetvalue(1)
_resetvalue(2)
_resetvalue(3)
else:
_resetvalue_index(1)
_resetvalue_index(2)
_resetvalue_index(3)
self._values = tuple(values)
2018-06-09 18:59:40 +02:00
def resetvalue(self,
path,
commit):
2013-08-14 23:06:31 +02:00
"""remove value means delete value in storage
"""
2019-02-24 19:03:00 +01:00
log.debug('resetvalue', path, id(self))
2015-11-19 22:25:00 +01:00
def _resetvalue(nb):
lst = list(self._values[nb])
lst.pop(idx)
values.append(tuple(lst))
values = []
2015-12-28 22:00:46 +01:00
if path in self._values[0]:
idx = self._values[0].index(path)
_resetvalue(0)
_resetvalue(1)
_resetvalue(2)
_resetvalue(3)
self._values = tuple(values)
2013-08-14 23:06:31 +02:00
# owner
def setowner(self,
path,
owner,
index=None):
"""change owner for a path
2013-08-14 23:06:31 +02:00
"""
2015-11-19 22:25:00 +01:00
idx = self._values[0].index(path)
2016-03-09 15:48:14 +01:00
if index is None:
vidx = None
else:
vidx = self._values[1][idx].index(index)
values = []
self._setvalue_info(3, idx, owner, values, index, vidx)
lst = list(self._values)
lst[3] = tuple(values[0])
self._values = tuple(lst)
2015-11-19 22:25:00 +01:00
2018-06-09 18:59:40 +02:00
def get_max_length(self,
path):
2015-12-28 22:00:46 +01:00
if path in self._values[0]:
2015-11-19 22:25:00 +01:00
idx = self._values[0].index(path)
2015-12-28 22:00:46 +01:00
else:
2015-11-19 22:25:00 +01:00
return 0
return max(self._values[1][idx]) + 1
2013-08-14 23:06:31 +02:00
2017-11-20 17:01:36 +01:00
def getowner(self,
path,
default,
index=None,
2017-07-11 22:31:58 +02:00
with_value=False):
"""get owner for a path
2013-08-14 23:06:31 +02:00
return: owner object
"""
2017-11-28 22:42:30 +01:00
owner, value = self._getvalue(path,
index,
with_value)
2017-11-23 16:56:14 +01:00
if owner is undefined:
owner = default
2019-02-24 19:03:00 +01:00
log.debug('getvalue', path, index, value, owner, id(self))
2017-07-11 22:31:58 +02:00
if with_value:
2017-11-28 22:42:30 +01:00
return owner, value
2017-07-11 22:31:58 +02:00
else:
return owner
2015-11-19 22:25:00 +01:00
2017-11-20 17:01:36 +01:00
def _getvalue(self,
path,
2017-11-28 22:42:30 +01:00
index,
with_value):
2015-11-19 22:25:00 +01:00
"""
2016-03-09 15:48:14 +01:00
_values == ((path1, path2), ((idx1_1, idx1_2), None), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2))
2015-11-19 22:25:00 +01:00
"""
2017-11-28 22:42:30 +01:00
value = undefined
2015-12-28 22:00:46 +01:00
if path in self._values[0]:
2017-02-03 23:39:24 +01:00
path_idx = self._values[0].index(path)
indexes = self._values[1][path_idx]
if indexes is None:
if index is not None: # pragma: no cover
2017-07-04 19:59:42 +02:00
raise ValueError('index is forbidden for {}'.format(path))
2017-11-28 22:42:30 +01:00
owner = self._values[3][path_idx]
if with_value:
value = self._values[2][path_idx]
2015-11-19 22:25:00 +01:00
else:
2017-02-03 23:39:24 +01:00
if index is None: # pragma: no cover
2017-07-04 19:59:42 +02:00
raise ValueError('index is mandatory for {}'.format(path))
2017-02-03 23:39:24 +01:00
if index in indexes:
subidx = indexes.index(index)
2017-11-28 22:42:30 +01:00
owner = self._values[3][path_idx][subidx]
if with_value:
value = self._values[2][path_idx][subidx]
2015-11-19 22:25:00 +01:00
else:
2017-11-28 22:42:30 +01:00
owner = undefined
2015-12-28 22:00:46 +01:00
else:
2017-11-28 22:42:30 +01:00
owner = undefined
if isinstance(value, tuple):
value = list(value)
2017-11-28 22:42:30 +01:00
return owner, value
2018-09-09 22:38:03 +02:00
def set_information(self, path, key, value):
"""updates the information's attribute
(which is a dictionary)
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
2018-09-09 22:38:03 +02:00
self._informations.setdefault(path, {})
self._informations[path][key] = value
2018-09-09 22:38:03 +02:00
def get_information(self, path, key, default):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
2018-09-09 22:38:03 +02:00
value = self._informations.get(path, {}).get(key, default)
2015-12-28 22:00:46 +01:00
if value is undefined:
raise ValueError(_("information's item"
" not found: {0}").format(key))
return value
2018-09-09 22:38:03 +02:00
def del_information(self, path, key, raises):
if path in self._informations and key in self._informations[path]:
del self._informations[path][key]
else:
if raises:
raise ValueError(_("information's item not found {0}").format(key))
2018-10-07 10:55:52 +02:00
def list_information(self, path):
if path in self._informations:
return self._informations[path].keys()
else:
return []
def del_informations(self):
self._informations = {}
2018-06-09 18:59:40 +02:00
def exportation(self):
return self._values
def importation(self, export):
self._values = export
2018-09-15 22:44:49 +02:00
2017-11-12 14:33:05 +01:00
def delete_session(session_id):
2018-09-13 22:01:27 +02:00
raise ValueError(_('cannot delete none persistent session'))