tiramisu/tiramisu/storage/sqlite3/value.py

316 lines
13 KiB
Python
Raw Normal View History

2013-08-19 11:01:21 +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-19 11:01:21 +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-19 11:01:21 +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-19 11:01:21 +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-19 11:01:21 +02:00
# ____________________________________________________________
2013-09-07 10:31:39 +02:00
from .sqlite3db import Sqlite3DB
2017-07-04 19:59:42 +02:00
from .storage import delete_session
from ...setting import undefined, owners
from ...i18n import _
2019-02-24 19:03:00 +01:00
from ...log import log
2018-06-09 18:59:40 +02:00
2013-09-07 10:31:39 +02:00
class Values(Sqlite3DB):
__slots__ = ('__weakref__',)
2013-08-19 11:01:21 +02:00
2013-08-20 22:45:11 +02:00
def __init__(self, storage):
2013-08-19 11:01:21 +02:00
"""init plugin means create values storage
"""
2013-09-07 10:31:39 +02:00
super(Values, self).__init__(storage)
2017-07-04 19:59:42 +02:00
2013-08-19 11:01:21 +02:00
# sqlite
2020-01-22 20:46:18 +01:00
async def _sqlite_select(self,
connection,
path,
index):
2017-07-11 22:31:58 +02:00
request = "SELECT value FROM value WHERE path = ? AND session_id = ? "
2017-07-04 19:59:42 +02:00
params = (path, self._session_id)
if index is not None:
2017-07-11 22:31:58 +02:00
request += "and idx = ? "
2017-07-04 19:59:42 +02:00
params = (path, self._session_id, index)
2017-07-11 22:31:58 +02:00
request += "LIMIT 1"
2020-01-22 20:46:18 +01:00
return await connection.select(request, params)
2017-07-16 23:11:12 +02:00
2013-08-19 11:01:21 +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):
2013-08-19 11:01:21 +02:00
"""set value for an option
a specified value must be associated to an owner
"""
2020-01-22 20:46:18 +01:00
log.debug('setvalue %s %s %s %s', path, value, owner, index)
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2017-07-04 19:59:42 +02:00
if index is not None:
2020-01-22 20:46:18 +01:00
if not new:
await self.resetvalue_index(connection,
path,
index)
await connection.execute("INSERT INTO value(path, value, owner, idx, session_id) VALUES "
"(?, ?, ?, ?, ?)", (path, self._sqlite_encode(value),
2019-12-24 15:24:20 +01:00
str(owner),
2020-01-22 20:46:18 +01:00
index,
self._session_id))
else:
if not new:
await self.resetvalue(connection,
path)
await connection.execute("INSERT INTO value(path, value, owner, session_id) VALUES "
"(?, ?, ?, ?)", (path, self._sqlite_encode(value),
str(owner),
self._session_id))
2017-07-04 19:59:42 +02:00
2019-12-24 15:24:20 +01:00
async def hasvalue(self,
2020-01-22 20:46:18 +01:00
connection,
2019-12-24 15:24:20 +01:00
path,
index=None):
2013-08-19 11:01:21 +02:00
"""if opt has a value
return: boolean
"""
2019-03-23 08:26:39 +01:00
log.debug('hasvalue %s %s', path, index)
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2020-01-22 20:46:18 +01:00
return await self._sqlite_select(connection,
path,
index) is not None
2013-08-19 11:01:21 +02:00
2018-06-09 18:59:40 +02:00
2020-01-22 20:46:18 +01:00
async def reduce_index(self,
connection,
path,
index):
2018-06-09 18:59:40 +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
await connection.execute("UPDATE value SET idx = ? WHERE path = ? and idx = ? "
"AND session_id = ?",
(index - 1, path, index, self._session_id))
2018-06-09 18:59:40 +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):
2018-06-09 18:59:40 +02:00
"""remove value means delete value in storage
"""
2020-01-22 20:46:18 +01:00
log.debug('resetvalue_index %s %s', path, index)
2018-06-09 18:59:40 +02:00
path = self._sqlite_encode_path(path)
2020-01-22 20:46:18 +01:00
await connection.execute("DELETE FROM value WHERE path = ? AND session_id = ? AND idx = ?",
(path, self._session_id, index))
2018-06-09 18:59:40 +02:00
2019-12-24 15:24:20 +01:00
async def resetvalue(self,
2020-01-22 20:46:18 +01:00
connection,
path):
2013-08-19 11:01:21 +02:00
"""remove value means delete value in storage
"""
2020-01-22 20:46:18 +01:00
log.debug('resetvalue %s', path)
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2020-01-22 20:46:18 +01:00
await connection.execute("DELETE FROM value WHERE path = ? AND session_id = ?",
(path, self._session_id))
2013-08-19 11:01:21 +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,
index=None):
2013-08-19 11:01:21 +02:00
"""change owner for an option
"""
2019-03-23 08:26:39 +01:00
log.debug('setowner %s %s %s', path, owner, index)
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2017-07-04 19:59:42 +02:00
if index is None:
2020-01-22 20:46:18 +01:00
await connection.execute("UPDATE value SET owner = ? WHERE path = ? AND session_id = ?",
(str(owner), path, self._session_id))
2017-07-04 19:59:42 +02:00
else:
2020-01-22 20:46:18 +01:00
await connection.execute("UPDATE value SET owner = ? WHERE path = ? and idx = ? AND session_id = ?",
(str(owner), path, index, self._session_id))
2013-08-19 11:01:21 +02:00
2019-12-24 15:24:20 +01:00
async def getowner(self,
2020-01-22 20:46:18 +01:00
connection,
path,
default,
index=None,
with_value=False):
2013-08-19 11:01:21 +02:00
"""get owner for an option
return: owner object
"""
2019-03-23 08:26:39 +01:00
log.debug('getowner %s %s %s %s', path, default, index, with_value)
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2017-07-11 22:31:58 +02:00
request = "SELECT owner, value FROM value WHERE path = ? AND session_id = ?"
2017-07-04 19:59:42 +02:00
if index is not None:
request += " AND idx = ?"
params = (path, self._session_id, index)
else:
params = (path, self._session_id)
2017-07-11 22:31:58 +02:00
request += ' LIMIT 1'
2020-01-22 20:46:18 +01:00
owner = await connection.select(request, params)
2013-08-19 11:01:21 +02:00
if owner is None:
2017-07-11 22:31:58 +02:00
if not with_value:
return default
else:
return default, None
2013-08-19 11:01:21 +02:00
else:
# autocreate owners
try:
2017-07-11 22:31:58 +02:00
nowner = getattr(owners, owner[0])
2018-09-29 21:39:58 +02:00
except AttributeError: # pragma: no cover
2017-07-16 10:57:43 +02:00
owners.addowner(owner[0])
2017-07-11 22:31:58 +02:00
nowner = getattr(owners, owner[0])
if not with_value:
return nowner
else:
value = self._sqlite_decode(owner[1])
return nowner, 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")
"""
2019-03-23 08:26:39 +01:00
log.debug('set_information %s %s', key, value)
2018-09-09 22:38:03 +02:00
path = self._sqlite_encode_path(path)
2020-01-22 20:46:18 +01:00
await connection.execute("DELETE FROM information WHERE key = ? AND session_id = ? AND path = ?",
(key, self._session_id, path))
await connection.execute("INSERT INTO information(key, value, session_id, path) VALUES "
"(?, ?, ?, ?)", (key, self._sqlite_encode(value), self._session_id, path))
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")
"""
2019-03-23 08:26:39 +01:00
log.debug('get_information %s %s', key, default)
2018-09-09 22:38:03 +02:00
path = self._sqlite_encode_path(path)
2020-01-22 20:46:18 +01:00
value = await connection.select("SELECT value FROM information WHERE key = ? AND "
"session_id = ? AND path = ?",
(key, self._session_id, path))
if value is None:
2017-07-04 19:59:42 +02:00
if default is undefined:
raise ValueError(_("information's item"
" not found: {0}").format(key))
return default
else:
return self._sqlite_decode(value[0])
2017-07-04 19:59:42 +02:00
2020-01-22 20:46:18 +01:00
async def del_information(self,
connection,
path,
key,
raises):
2019-03-23 08:26:39 +01:00
log.debug('del_information %s %s', key, raises)
2018-09-09 22:38:03 +02:00
path = self._sqlite_encode_path(path)
2020-01-22 20:46:18 +01:00
information = await connection.select("SELECT value FROM information WHERE key = ? "
"AND session_id = ? AND path = ?",
(key, self._session_id, path))
2019-12-24 15:24:20 +01:00
if raises and information is None:
2017-07-04 19:59:42 +02:00
raise ValueError(_("information's item not found {0}").format(key))
2020-01-22 20:46:18 +01:00
await connection.execute("DELETE FROM information WHERE key = ? AND session_id = ? AND path = ?",
(key, self._session_id, path))
2017-07-04 19:59:42 +02:00
2020-01-22 20:46:18 +01:00
async def list_information(self,
connection,
path):
2018-10-07 10:55:52 +02:00
path = self._sqlite_encode_path(path)
2020-01-22 20:46:18 +01:00
rows = await connection.select("SELECT key FROM information WHERE session_id = ? AND path = ?",
(self._session_id, path),
only_one=False)
2019-12-24 15:24:20 +01:00
ret = []
2018-10-07 10:55:52 +02:00
for row in rows:
2019-12-24 15:24:20 +01:00
ret.append(self._sqlite_decode_path(row[0]))
return ret
2018-10-07 10:55:52 +02:00
2020-01-22 20:46:18 +01:00
async def del_informations(self,
connection):
await connection.execute("DELETE FROM information WHERE session_id = ?",
(self._session_id,))
2020-01-22 20:46:18 +01:00
async def exportation(self,
connection):
2019-02-24 19:03:00 +01:00
log.debug('exportation')
2020-01-22 20:46:18 +01:00
rows = await connection.select("SELECT path, value, owner, idx FROM value WHERE "
"session_id = ?;", (self._session_id,), only_one=False)
2017-07-04 19:59:42 +02:00
ret = [[], [], [], []]
for row in rows:
2018-09-15 22:44:49 +02:00
path = self._sqlite_decode_path(row[0])
2017-07-04 19:59:42 +02:00
value = self._sqlite_decode(row[1])
2017-07-11 22:31:58 +02:00
owner = row[2]
2017-07-04 19:59:42 +02:00
index = row[3]
if index is None:
ret[0].append(path)
ret[1].append(index)
ret[2].append(value)
ret[3].append(owner)
else:
if path in ret[0]:
path_idx = ret[0].index(path)
ret[1][path_idx].append(index)
ret[2][path_idx].append(value)
2018-06-09 18:59:40 +02:00
ret[3][path_idx].append(owner)
2017-07-04 19:59:42 +02:00
else:
ret[0].append(path)
ret[1].append([index])
ret[2].append([value])
2018-06-09 18:59:40 +02:00
ret[3].append([owner])
2017-07-04 19:59:42 +02:00
return ret
2020-01-22 20:46:18 +01:00
async def importation(self,
connection,
export):
2019-02-24 19:03:00 +01:00
log.debug('importation')
2018-09-15 22:44:49 +02:00
request = "DELETE FROM value WHERE session_id = ?"
2020-01-22 20:46:18 +01:00
await connection.execute(request, (self._session_id,))
2017-07-04 19:59:42 +02:00
for idx, path in enumerate(export[0]):
2018-09-07 08:42:14 +02:00
path = self._sqlite_encode_path(path)
2017-07-04 19:59:42 +02:00
index = export[1][idx]
value = export[2][idx]
owner = export[3][idx]
2017-07-13 22:04:06 +02:00
if index is None:
2020-01-22 20:46:18 +01:00
await connection.execute("INSERT INTO value(path, value, owner, idx, session_id) VALUES "
"(?, ?, ?, ?, ?)", (path, self._sqlite_encode(value),
str(owner), index,
self._session_id))
2017-07-13 22:04:06 +02:00
else:
for val in zip(index, value, owner):
2020-01-22 20:46:18 +01:00
await connection.execute("INSERT INTO value(path, value, owner, idx, session_id)"
"VALUES (?, ?, ?, ?, ?)", (path,
self._sqlite_encode(val[1]),
str(val[2]), val[0],
self._session_id))
2017-07-04 19:59:42 +02:00
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):
2019-03-23 08:26:39 +01:00
log.debug('get_max_length %s', path)
2020-01-22 20:46:18 +01:00
val_max = await connection.select("SELECT max(idx) FROM value WHERE path = ? AND session_id = ?",
(path, self._session_id), False)
2017-07-04 19:59:42 +02:00
if val_max[0][0] is None:
return 0
return val_max[0][0] + 1