tiramisu/tiramisu/storage/dictionary/value.py

329 lines
11 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"
2021-02-24 20:30:04 +01:00
# Copyright (C) 2013-2021 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
# ____________________________________________________________
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
2020-01-22 20:46:18 +01:00
from .storage import delete_session
2017-12-02 22:53:57 +01:00
2019-06-12 08:45:56 +02:00
from copy import deepcopy
2017-12-02 22:53:57 +01:00
class Values:
__slots__ = ('_values',
'_storage',
'__weakref__')
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
"""
self._storage = storage
2013-08-14 23:06:31 +02:00
2019-06-12 08:45:56 +02:00
def _setvalue_info(self, nb, idx, value, index, follower_idx=None):
2020-01-22 20:46:18 +01:00
lst = self._storage.get_values()[nb]
2019-06-12 08:45:56 +02:00
if index is None or nb == 0:
# not follower or path
lst[idx] = value
2016-03-09 15:48:14 +01:00
else:
2019-06-12 08:45:56 +02:00
# follower
if nb == 1 and index in lst[idx]:
follower_idx = lst[idx].index(index)
tval = list(lst[idx])
if follower_idx is None:
tval.append(value)
2016-03-09 15:48:14 +01:00
else:
2019-06-12 08:45:56 +02:00
tval[follower_idx] = value
lst[idx] = tval
return follower_idx
def _add_new_value(self, index, nb, value):
if index is None or nb == 0:
# not follower or path
2020-01-22 20:46:18 +01:00
self._storage.get_values()[nb].append(value)
2019-06-12 08:45:56 +02:00
else:
# follower
2020-01-22 20:46:18 +01:00
self._storage.get_values()[nb].append([value])
2019-06-21 23:04:04 +02:00
def _add_new_value(self, index, nb, value):
if index is None or nb == 0:
# not follower or path
2020-01-22 20:46:18 +01:00
self._storage.get_values()[nb].append(value)
2019-06-21 23:04:04 +02:00
else:
# follower
2020-01-22 20:46:18 +01:00
self._storage.get_values()[nb].append([value])
2019-06-21 23:04:04 +02:00
2013-08-14 23:06:31 +02:00
# value
2019-12-24 15:24:20 +01:00
async def setvalue(self,
2020-01-22 20:46:18 +01:00
connection,
2019-12-24 15:24:20 +01:00
path,
value,
owner,
index,
2020-01-22 20:46:18 +01:00
new=False):
"""set value for a path
2013-08-14 23:06:31 +02:00
a specified value must be associated to an owner
"""
2019-03-23 08:26:39 +01:00
log.debug('setvalue %s %s %s %s %s', path, value, owner, index, id(self))
2013-08-14 23:06:31 +02:00
2019-06-12 08:45:56 +02:00
#if isinstance(value, list):
# value = value
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
if not new and path in values[0]:
idx = values[0].index(path)
2019-06-12 08:45:56 +02:00
self._setvalue_info(0, idx, path, index)
follower_idx = self._setvalue_info(1, idx, index, index)
self._setvalue_info(2, idx, value, index, follower_idx)
self._setvalue_info(3, idx, owner, index, follower_idx)
2015-12-28 22:00:46 +01:00
else:
2019-06-12 08:45:56 +02:00
self._add_new_value(index, 0, path)
self._add_new_value(index, 1, index)
self._add_new_value(index, 2, value)
self._add_new_value(index, 3, owner)
2015-11-19 22:25:00 +01:00
2020-01-22 20:46:18 +01:00
async def hasvalue(self,
connection,
path,
index=None):
"""if path has a value
2013-08-14 23:06:31 +02:00
return: boolean
"""
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
has_path = path in values[0]
2019-03-23 08:26:39 +01:00
log.debug('hasvalue %s %s %s %s', path, index, has_path, id(self))
2017-11-23 16:56:14 +01:00
if index is None:
return has_path
elif has_path:
2020-01-22 20:46:18 +01:00
path_idx = values[0].index(path)
indexes = values[1][path_idx]
2017-11-23 16:56:14 +01:00
return index in indexes
return False
2013-08-14 23:06:31 +02:00
2020-01-22 20:46:18 +01:00
async def reduce_index(self,
connection,
path,
index):
2017-10-22 15:10:50 +02:00
"""
_values == ((path1, path2), ((idx1_1, idx1_2), None), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2))
"""
2019-03-23 08:26:39 +01:00
log.debug('reduce_index %s %s %s', path, index, id(self))
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
path_idx = values[0].index(path)
2018-06-09 18:59:40 +02:00
# get the "index" position
2020-01-22 20:46:18 +01:00
subidx = values[1][path_idx].index(index)
2018-06-09 18:59:40 +02:00
# reduce to one the index
2020-01-22 20:46:18 +01:00
values[1][path_idx][subidx] -= 1
2017-10-22 15:10:50 +02:00
2019-12-24 15:24:20 +01:00
async def resetvalue_index(self,
2020-01-22 20:46:18 +01:00
connection,
2019-12-24 15:24:20 +01:00
path,
2020-01-22 20:46:18 +01:00
index):
2019-03-23 08:26:39 +01:00
log.debug('resetvalue_index %s %s %s', path, index, id(self))
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
2017-10-22 15:10:50 +02:00
def _resetvalue(nb):
2020-01-22 20:46:18 +01:00
del self._storage.get_values()[nb][path_idx]
2017-10-22 15:10:50 +02:00
def _resetvalue_index(nb):
2020-01-22 20:46:18 +01:00
del self._storage.get_values()[nb][path_idx][subidx]
2017-10-22 15:10:50 +02:00
2020-01-22 20:46:18 +01:00
path_idx = values[0].index(path)
indexes = values[1][path_idx]
2017-10-22 15:10:50 +02:00
if index in indexes:
subidx = indexes.index(index)
2020-01-22 20:46:18 +01:00
if len(values[1][path_idx]) == 1:
2017-10-22 15:10:50 +02:00
_resetvalue(0)
_resetvalue(1)
_resetvalue(2)
_resetvalue(3)
else:
_resetvalue_index(1)
_resetvalue_index(2)
_resetvalue_index(3)
2019-12-24 15:24:20 +01:00
async def resetvalue(self,
2020-01-22 20:46:18 +01:00
connection,
path):
2013-08-14 23:06:31 +02:00
"""remove value means delete value in storage
"""
2019-03-23 08:26:39 +01:00
log.debug('resetvalue %s %s', path, id(self))
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
2015-11-19 22:25:00 +01:00
def _resetvalue(nb):
2020-01-22 20:46:18 +01:00
values[nb].pop(idx)
if path in values[0]:
idx = values[0].index(path)
2015-12-28 22:00:46 +01:00
_resetvalue(0)
_resetvalue(1)
_resetvalue(2)
_resetvalue(3)
2013-08-14 23:06:31 +02:00
# owner
2019-12-24 15:24:20 +01:00
async def setowner(self,
2020-01-22 20:46:18 +01:00
connection,
2019-12-24 15:24:20 +01:00
path,
owner,
2020-01-22 20:46:18 +01:00
index):
"""change owner for a path
2013-08-14 23:06:31 +02:00
"""
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
idx = values[0].index(path)
2016-03-09 15:48:14 +01:00
if index is None:
2019-06-12 08:45:56 +02:00
follower_idx = None
2016-03-09 15:48:14 +01:00
else:
2020-01-22 20:46:18 +01:00
follower_idx = values[1][idx].index(index)
2019-06-12 08:45:56 +02:00
self._setvalue_info(3, idx, owner, index, follower_idx)
2015-11-19 22:25:00 +01:00
2019-12-24 15:24:20 +01:00
async def getowner(self,
2020-01-22 20:46:18 +01:00
connection,
2019-12-24 15:24:20 +01:00
path,
default,
index=None,
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-03-23 08:26:39 +01:00
log.debug('getvalue %s %s %s %s %s', 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
"""
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
2017-11-28 22:42:30 +01:00
value = undefined
2020-01-22 20:46:18 +01:00
if path in values[0]:
path_idx = values[0].index(path)
indexes = values[1][path_idx]
2017-02-03 23:39:24 +01:00
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))
2020-01-22 20:46:18 +01:00
owner = values[3][path_idx]
2017-11-28 22:42:30 +01:00
if with_value:
2020-01-22 20:46:18 +01:00
value = 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)
2020-01-22 20:46:18 +01:00
owner = values[3][path_idx][subidx]
2017-11-28 22:42:30 +01:00
if with_value:
2020-01-22 20:46:18 +01:00
value = 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
2020-01-22 20:46:18 +01:00
async def set_information(self,
connection,
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")
"""
2020-01-22 20:46:18 +01:00
informations = self._storage.get_informations()
informations.setdefault(path, {})
informations[path][key] = value
2020-01-22 20:46:18 +01:00
async def get_information(self,
connection,
path,
key,
default):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
2020-01-22 20:46:18 +01:00
value = self._storage.get_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
2020-01-22 20:46:18 +01:00
async def del_information(self,
connection,
path,
key,
raises):
informations = self._storage.get_informations()
if path in informations and key in informations[path]:
del informations[path][key]
else:
if raises:
raise ValueError(_("information's item not found {0}").format(key))
2020-01-22 20:46:18 +01:00
async def list_information(self,
connection,
path):
informations = self._storage.get_informations()
if path in informations:
return informations[path].keys()
2018-10-07 10:55:52 +02:00
else:
return []
2020-01-22 20:46:18 +01:00
async def del_informations(self,
connection):
self._storage.set_informations({})
async def exportation_informations(self,
connection,
):
return deepcopy(self._storage.get_informations())
async def importation_informations(self,
connection,
informations,
):
#deepcopy(informations)
return self._storage.set_informations(informations)
2020-01-22 20:46:18 +01:00
async def exportation(self,
connection):
return deepcopy(self._storage.get_values())
2020-01-22 20:46:18 +01:00
async def importation(self,
connection,
export):
self._storage.set_values(deepcopy(export))
2019-12-24 15:24:20 +01:00
async def get_max_length(self,
2020-01-22 20:46:18 +01:00
connection,
2019-12-24 15:24:20 +01:00
path):
2020-01-22 20:46:18 +01:00
values = self._storage.get_values()
if path in values[0]:
idx = values[0].index(path)
2019-12-24 15:24:20 +01:00
else:
return 0
2020-01-22 20:46:18 +01:00
return max(values[1][idx]) + 1
2018-09-15 22:44:49 +02:00
2020-01-22 20:46:18 +01:00
def getconnection(self):
return self._storage.getconnection()