tiramisu/tiramisu/storage/dictionary/setting.py

128 lines
4.5 KiB
Python
Raw Normal View History

2013-08-14 23:06:31 +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-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
# ____________________________________________________________
2019-11-19 18:39:44 +01:00
from copy import deepcopy
2019-02-24 19:03:00 +01:00
from ...log import log
2017-12-04 20:05:36 +01:00
2013-08-14 23:06:31 +02:00
class Properties:
2020-01-22 20:46:18 +01:00
__slots__ = ('_storage',)
2013-08-14 23:06:31 +02:00
2017-07-04 19:59:42 +02:00
def __init__(self, storage):
2013-08-14 23:06:31 +02:00
# properties attribute: the name of a property enables this property
# key is None for global properties
self._storage = storage
2013-08-14 23:06:31 +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):
2019-11-19 18:39:44 +01:00
log.debug('setproperties %s %s %s', path, index, properties)
2020-01-22 20:46:18 +01:00
self._storage.get_properties().setdefault(path, {})[index] = properties
async def getproperties(self,
connection,
path,
index,
default_properties):
properties = self._storage.get_properties()
if path not in properties:
2019-11-19 18:39:44 +01:00
ret = frozenset(default_properties)
else:
2020-01-22 20:46:18 +01:00
ret = properties[path].get(index, frozenset(default_properties))
2019-11-19 18:39:44 +01:00
log.debug('getproperties %s %s %s', path, index, ret)
2017-12-04 20:05:36 +01:00
return ret
2013-08-14 23:06:31 +02:00
2020-01-22 20:46:18 +01:00
async def delproperties(self,
connection,
path,
index):
2019-03-23 08:26:39 +01:00
log.debug('delproperties %s', path)
2020-01-22 20:46:18 +01:00
properties = self._storage.get_properties()
if path in properties and index in properties[path]:
del(properties[path][index])
2019-11-19 18:39:44 +01:00
2013-08-14 23:06:31 +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'])}
"""
2020-01-22 20:46:18 +01:00
return deepcopy(self._storage.get_properties())
async def importation(self,
connection,
properties):
self._storage.set_properties(properties)
2013-09-22 20:57:52 +02:00
2020-01-22 20:46:18 +01:00
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:
2020-01-22 20:46:18 +01:00
__slots__ = ('_storage',)
2017-07-13 22:04:06 +02:00
def __init__(self, storage):
# permissive properties
self._storage = storage
2017-07-13 22:04:06 +02:00
2020-01-22 20:46:18 +01:00
async def setpermissives(self,
connection,
path,
index,
permissives):
2019-03-23 08:26:39 +01:00
log.debug('setpermissives %s %s', path, permissives)
2020-01-22 20:46:18 +01:00
self._storage.get_permissives().setdefault(path, {})[index] = permissives
async def getpermissives(self,
connection,
path,
index):
permissives = self._storage.get_permissives()
if not path in permissives:
2019-11-19 18:39:44 +01:00
ret = frozenset()
else:
2020-01-22 20:46:18 +01:00
ret = permissives[path].get(index, frozenset())
2019-03-23 08:26:39 +01:00
log.debug('getpermissives %s %s', path, ret)
2017-12-04 20:05:36 +01: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-11-19 18:39:44 +01:00
log.debug('delpermissive %s', path)
2020-01-22 20:46:18 +01:00
permissives = self._storage.get_permissives()
if path in permissives and index in permissives[path]:
del(permissives[path][index])
2019-11-19 18:39:44 +01: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 permissives in a dictionary
example: {'path1': set(['perm1', 'perm2'])}
"""
2020-01-22 20:46:18 +01:00
return deepcopy(self._storage.get_permissives())
async def importation(self,
connection,
permissives):
self._storage.set_permissives(permissives)
2017-07-04 19:59:42 +02:00
2020-01-22 20:46:18 +01:00
def getconnection(self):
return self._storage.getconnection()