tiramisu/tiramisu/storage/sqlite3/value.py

246 lines
10 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"
2017-07-04 19:59:42 +02:00
# Copyright (C) 2013-2017 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 _
2013-08-19 11:01:21 +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
def getsession(self):
pass
2013-08-19 11:01:21 +02:00
# sqlite
2017-07-04 19:59:42 +02:00
def _sqlite_select(self, 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"
2017-07-04 19:59:42 +02:00
return self._storage.select(request, params)
2013-08-19 11:01:21 +02:00
# value
2017-07-04 19:59:42 +02:00
def setvalue(self, path, value, owner, index, session):
2013-08-19 11:01:21 +02:00
"""set value for an option
a specified value must be associated to an owner
"""
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:
self._storage.execute("DELETE FROM value WHERE path = ? AND idx = ? AND "
"session_id = ?", (path, index, self._session_id),
commit=False)
self._storage.execute("INSERT INTO value(path, value, owner, idx, session_id) VALUES "
"(?, ?, ?, ?, ?)", (path, self._sqlite_encode(value),
2017-07-11 22:31:58 +02:00
str(owner),
2017-07-04 19:59:42 +02:00
index,
self._session_id),
commit=True)
else:
self._storage.execute("DELETE FROM value WHERE path = ? AND session_id = ?",
(path, self._session_id),
commit=False)
self._storage.execute("INSERT INTO value(path, value, owner, session_id) VALUES "
"(?, ?, ?, ?)", (path, self._sqlite_encode(value),
2017-07-11 22:31:58 +02:00
str(owner),
2017-07-04 19:59:42 +02:00
self._session_id),
commit=True)
def getvalue(self, path, session, index=None):
2013-08-19 11:01:21 +02:00
"""get value for an option
return: only value, not the owner
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2017-07-04 19:59:42 +02:00
values = self._sqlite_select(path, index)
if values is None:
return values
return self._sqlite_decode(values[0])
2013-08-19 11:01:21 +02:00
2017-07-04 19:59:42 +02:00
def hasvalue(self, path, index=None):
2013-08-19 11:01:21 +02:00
"""if opt has a value
return: boolean
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2017-07-04 19:59:42 +02:00
return self._sqlite_select(path, index) is not None
2013-08-19 11:01:21 +02:00
2017-07-04 19:59:42 +02:00
def resetvalue(self, path, session):
2013-08-19 11:01:21 +02:00
"""remove value means delete value in storage
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2017-07-04 19:59:42 +02:00
self._storage.execute("DELETE FROM value WHERE path = ? AND session_id = ?", (path, self._session_id))
2013-08-19 11:01:21 +02:00
def get_modified_values(self):
2013-08-19 11:01:21 +02:00
"""return all values in a dictionary
example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
"""
ret = {}
2017-07-04 19:59:42 +02:00
for path, value, owner, index, _ in self._storage.select("SELECT * FROM value WHERE "
"session_id = ?",
(self._session_id,),
only_one=False):
2013-08-21 23:21:28 +02:00
path = self._sqlite_decode_path(path)
2017-07-11 22:31:58 +02:00
owner = getattr(owners, owner)
2013-08-20 09:47:12 +02:00
value = self._sqlite_decode(value)
2017-07-04 19:59:42 +02:00
if isinstance(value, list):
value = tuple(value)
if index is None:
ret[path] = (owner, value)
else:
if path in ret:
ret[path][0][str(index)] = owner
ret[path][1][str(index)] = value
else:
ret[path] = ({str(index): owner}, {str(index): value})
2013-08-19 11:01:21 +02:00
return ret
# owner
2017-07-04 19:59:42 +02:00
def setowner(self, path, owner, session, index=None):
2013-08-19 11:01:21 +02:00
"""change owner for an option
"""
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:
self._storage.execute("UPDATE value SET owner = ? WHERE path = ? AND session_id = ?",
2017-07-11 22:31:58 +02:00
(str(owner), path, self._session_id))
2017-07-04 19:59:42 +02:00
else:
self._storage.execute("UPDATE value SET owner = ? WHERE path = ? and idx = ? AND session_id = ?",
2017-07-11 22:31:58 +02:00
(str(owner), path, index, self._session_id))
2013-08-19 11:01:21 +02:00
2017-07-11 22:31:58 +02:00
def getowner(self, path, default, session, index=None, only_default=False, with_value=False):
2013-08-19 11:01:21 +02:00
"""get owner for an option
return: owner object
"""
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'
2017-07-04 19:59:42 +02:00
owner = self._storage.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])
except AttributeError:
2017-07-11 22:31:58 +02:00
owners.addowner(towner)
nowner = getattr(owners, owner[0])
if not with_value:
return nowner
else:
value = self._sqlite_decode(owner[1])
return nowner, value
def set_information(self, 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")
"""
2017-07-04 19:59:42 +02:00
self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ?",
(key, self._session_id),
False)
2017-07-04 19:59:42 +02:00
self._storage.execute("INSERT INTO information(key, value, session_id) VALUES "
"(?, ?, ?)", (key, self._sqlite_encode(value), self._session_id))
2017-07-04 19:59:42 +02:00
def get_information(self, key, default):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
2017-07-04 19:59:42 +02:00
value = self._storage.select("SELECT value FROM information WHERE key = ? AND "
"session_id = ?",
(key, self._session_id))
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
def del_information(self, key, raises):
if raises and self._storage.select("SELECT value FROM information WHERE key = ? "
"AND session_id = ?",
(key, self._session_id)) is None:
raise ValueError(_("information's item not found {0}").format(key))
self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ?",
(key, self._session_id))
def exportation(self, session, fake=False):
rows = self._storage.select("SELECT path, value, owner, idx FROM value WHERE "
"session_id = ?;", (self._session_id,), only_one=False)
ret = [[], [], [], []]
for row in rows:
path = row[0]
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)
else:
ret[0].append(path)
ret[1].append([index])
ret[2].append([value])
ret[3].append(owner)
return ret
def importation(self, export):
self._storage.execute("DELETE FROM value WHERE session_id = ?", (self._session_id,),
commit=False)
for idx, path in enumerate(export[0]):
index = export[1][idx]
value = export[2][idx]
owner = export[3][idx]
self._storage.execute("INSERT INTO value(path, value, owner, idx, session_id) VALUES "
"(?, ?, ?, ?, ?)", (path, self._sqlite_encode(value),
2017-07-11 22:31:58 +02:00
str(owner), index,
2017-07-04 19:59:42 +02:00
self._session_id))
self._storage._conn.commit()
def get_max_length(self, path, session):
val_max = self._storage.select("SELECT max(idx) FROM value WHERE path = ? AND session_id = ?",
(path, self._session_id), False)
if val_max[0][0] is None:
return 0
return val_max[0][0] + 1