tiramisu/tiramisu/storage/sqlite3/setting.py

200 lines
8.4 KiB
Python
Raw Normal View History

2013-08-19 11:01:21 +02:00
# -*- coding: utf-8 -*-
"default plugin for setting: 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
2019-02-24 19:03:00 +01:00
from ...log import log
2018-06-09 18:59:40 +02:00
2017-07-13 22:04:06 +02:00
class Properties(Sqlite3DB):
2013-08-19 11:01:21 +02:00
__slots__ = tuple()
2013-08-20 22:45:11 +02:00
def __init__(self, storage):
2017-07-13 22:04:06 +02:00
super(Properties, self).__init__(storage)
2013-08-19 11:01:21 +02:00
2014-01-24 09:17:46 +01:00
# properties
2020-01-22 20:46:18 +01:00
async def setproperties(self,
connection,
path,
index,
properties):
await self.delproperties(connection,
path,
index)
await connection.execute("INSERT INTO property(path, tiram_index, properties, session_id) VALUES "
"(?, ?, ?, ?)", (path,
index,
self._sqlite_encode(properties),
self._session_id))
async def getproperties(self,
connection,
path,
index,
default_properties):
2019-12-02 10:41:16 +01:00
sql = 'SELECT properties FROM property WHERE session_id = ? '
params = [self._session_id]
if path is None:
sql += "AND path is NULL "
else:
sql += "AND path = ? "
params.append(path)
if index is None:
sql += "AND tiram_index is NULL LIMIT 1"
else:
sql += "AND tiram_index = ? LIMIT 1"
params.append(index)
2020-01-22 20:46:18 +01:00
value = await connection.select(sql, params)
2013-08-19 11:01:21 +02:00
if value is None:
return set(default_properties)
else:
2013-08-20 09:47:12 +02:00
return set(self._sqlite_decode(value[0]))
2013-08-19 11:01:21 +02:00
2020-01-22 20:46:18 +01:00
async def delproperties(self,
connection,
path,
index):
2019-12-02 10:41:16 +01:00
sql = 'DELETE FROM property WHERE session_id = ? '
params = [self._session_id]
if path is None:
sql += 'AND path is NULL '
else:
sql += 'AND path = ? '
params.append(path)
if index is None:
sql += 'AND tiram_index is NULL'
else:
params.append(index)
sql += 'AND tiram_index = ?'
2020-01-22 20:46:18 +01:00
await connection.execute(sql, params)
2013-08-19 11:01:21 +02:00
2020-01-22 20:46:18 +01:00
async def exportation(self,
connection):
2013-09-22 20:57:52 +02:00
"""return all modified settings in a dictionary
example: {'path1': set(['prop1', 'prop2'])}
"""
ret = {}
2020-01-22 20:46:18 +01:00
for path, tiram_index, properties, _ in await connection.select("SELECT * FROM property "
"WHERE session_id = ?",
(self._session_id,),
only_one=False):
2019-12-02 10:41:16 +01:00
ret.setdefault(path, {})[tiram_index] = self._sqlite_decode(properties)
2013-09-22 20:57:52 +02:00
return ret
2020-01-22 20:46:18 +01:00
async def importation(self,
connection,
properties):
await connection.execute("DELETE FROM property WHERE session_id = ?", (self._session_id,))
2019-12-02 10:41:16 +01:00
for path, indexed_properties in properties.items():
for index, property_ in indexed_properties.items():
2020-01-22 20:46:18 +01:00
await connection.execute("INSERT INTO property(path, tiram_index, properties, session_id) "
"VALUES (?, ?, ?, ?)", (path,
index,
self._sqlite_encode(property_),
self._session_id,
))
def getconnection(self):
return self._storage.getconnection()
2017-07-04 19:59:42 +02:00
2017-07-13 22:04:06 +02:00
class Permissives(Sqlite3DB):
__slots__ = tuple()
# permissive
2020-01-22 20:46:18 +01:00
async def setpermissives(self,
connection,
path,
index,
permissive):
2019-11-19 18:39:44 +01:00
log.debug('setpermissive %s %s %s %s', path, index, permissive, id(self))
2020-01-22 20:46:18 +01:00
await self.delpermissive(connection,
path,
index)
await connection.execute("INSERT INTO permissive(path, tiram_index, permissives, session_id) "
"VALUES (?, ?, ?, ?)", (path,
index,
self._sqlite_encode(permissive),
self._session_id))
async def getpermissives(self,
connection,
path,
index):
2019-12-02 10:41:16 +01:00
sql = 'SELECT permissives FROM permissive WHERE session_id = ? '
params = [self._session_id]
if path is None:
sql += "AND path is NULL "
else:
sql += "AND path = ? "
params.append(path)
if index is None:
sql += "AND tiram_index is NULL LIMIT 1"
else:
sql += "AND tiram_index = ? LIMIT 1"
params.append(index)
2020-01-22 20:46:18 +01:00
permissives = await connection.select(sql, params)
2017-07-13 22:04:06 +02:00
if permissives is None:
2018-06-09 18:59:40 +02:00
ret = frozenset()
2017-07-13 22:04:06 +02:00
else:
2018-06-09 18:59:40 +02:00
ret = frozenset(self._sqlite_decode(permissives[0]))
2019-03-23 08:26:39 +01:00
log.debug('getpermissive %s %s %s', path, ret, id(self))
2018-06-09 18:59:40 +02:00
return ret
2017-07-13 22:04:06 +02:00
2020-01-22 20:46:18 +01:00
async def delpermissive(self,
connection,
path,
index):
2019-12-02 10:41:16 +01:00
sql = 'DELETE FROM permissive WHERE session_id = ? '
params = [self._session_id]
if path is None:
sql += 'AND path is NULL '
else:
sql += 'AND path = ? '
params.append(path)
if index is None:
sql += 'AND tiram_index is NULL'
else:
params.append(index)
sql += 'AND tiram_index = ?'
2020-01-22 20:46:18 +01:00
await connection.execute(sql, params)
2020-01-22 20:46:18 +01:00
async def exportation(self,
connection):
2013-09-22 20:57:52 +02:00
"""return all modified permissives in a dictionary
example: {'path1': set(['perm1', 'perm2'])}
"""
ret = {}
2019-12-24 15:24:20 +01:00
sql = "SELECT path, tiram_index, permissives FROM permissive WHERE session_id = ?"
2020-01-22 20:46:18 +01:00
for path, index, permissives in await connection.select(sql,
(self._session_id,),
only_one=False):
2019-12-02 10:41:16 +01:00
ret.setdefault(path, {})[index] = self._sqlite_decode(permissives)
2013-09-22 20:57:52 +02:00
return ret
2017-07-04 19:59:42 +02:00
2020-01-22 20:46:18 +01:00
async def importation(self,
connection,
permissives):
await connection.execute("DELETE FROM permissive WHERE session_id = ?", (self._session_id,))
2019-12-02 10:41:16 +01:00
for path, indexed_permissives in permissives.items():
for index, permissive in indexed_permissives.items():
2020-01-22 20:46:18 +01:00
await connection.execute("INSERT INTO permissive(path, tiram_index, permissives, session_id) "
"VALUES (?, ?, ?, ?)", (path,
index,
self._sqlite_encode(permissive),
self._session_id,
))