tiramisu/tiramisu/api.py

1887 lines
78 KiB
Python
Raw Normal View History

2017-10-22 09:48:08 +02:00
# -*- coding: utf-8 -*-
2021-02-24 20:30:04 +01:00
# Copyright (C) 2017-2021 Team tiramisu (see AUTHORS for all contributors)
2017-10-22 09:48:08 +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.
#
# 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.
#
# 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/>.
# ____________________________________________________________
2018-04-07 20:15:19 +02:00
from inspect import ismethod, getdoc, signature
2017-11-28 22:42:30 +01:00
from time import time
from typing import List, Set, Any, Optional, Callable, Union, Dict
2019-11-19 18:39:44 +01:00
from warnings import catch_warnings, simplefilter
2019-12-24 15:24:20 +01:00
from functools import wraps
2018-04-28 08:39:07 +02:00
from .error import APIError, ConfigError, LeadershipError, PropertiesOptionError, ValueErrorWarning
2018-04-28 08:39:07 +02:00
from .i18n import _
from .setting import ConfigBag, OptionBag, owners, groups, Undefined, undefined, \
2019-02-24 10:36:42 +01:00
FORBIDDEN_SET_PROPERTIES, SPECIAL_PROPERTIES, EXPIRATION_TIME
2020-01-22 20:46:18 +01:00
from .storage import default_storage
2018-10-31 18:26:20 +01:00
from .config import KernelConfig, SubConfig, KernelGroupConfig, KernelMetaConfig, KernelMixConfig
2019-12-28 12:30:32 +01:00
from .option import RegexpOption, OptionDescription
from .todict import TiramisuDict
2019-12-24 15:24:20 +01:00
from .asyncinit import asyncinit
2017-12-13 22:15:34 +01:00
TIRAMISU_VERSION = 3
2018-04-07 20:15:19 +02:00
class TiramisuHelp:
2018-10-07 10:55:52 +02:00
_tmpl_help = ' {0}\t{1}'
2018-04-07 20:15:19 +02:00
def help(self,
2018-10-07 10:55:52 +02:00
_display: bool=True) -> List[str]:
def display(doc=''):
if _display: # pragma: no cover
print(doc)
2018-04-07 20:15:19 +02:00
options = []
2018-10-07 10:55:52 +02:00
all_modules = dir(self)
modules = []
max_len = 0
force = False
for module_name in all_modules:
if module_name in ['forcepermissive', 'unrestraint']:
force = True
max_len = max(max_len, len('forcepermissive'))
2020-08-04 16:35:40 +02:00
elif module_name != 'help' and not module_name.startswith('_'):
2018-10-07 10:55:52 +02:00
modules.append(module_name)
max_len = max(max_len, len(module_name))
modules.sort()
display(_(getdoc(self)))
display()
if force:
display(_('Settings:'))
display(self._tmpl_help.format('forcepermissive', _('Access to option without verifying permissive properties')).expandtabs(max_len + 10))
display(self._tmpl_help.format('unrestraint', _('Access to option without property restriction')).expandtabs(max_len + 10))
display()
2019-12-24 15:24:20 +01:00
if isinstance(self, TiramisuDispatcherOption):
2018-10-07 10:55:52 +02:00
doc = _(getdoc(self.__call__))
display(_('Call: {}').format(doc))
display()
display(_('Commands:'))
for module_name in modules:
module = getattr(self, module_name)
doc = _(getdoc(module))
display(self._tmpl_help.format(module_name, doc).expandtabs(max_len + 10))
display()
def __dir__(self):
if '_registers' in super().__dir__():
return list(self._registers.keys())
return super().__dir__()
2018-04-07 20:15:19 +02:00
class CommonTiramisu(TiramisuHelp):
2018-10-07 10:55:52 +02:00
_allow_optiondescription = True
2018-12-24 09:30:58 +01:00
_validate_properties = True
2018-01-01 21:32:39 +01:00
2020-01-22 20:46:18 +01:00
async def _get_option(self,
connection) -> Any:
2020-08-15 00:23:48 +02:00
config_bag = self._option_bag.config_bag
2019-12-24 15:24:20 +01:00
if not self._subconfig:
try:
subconfig, name = await config_bag.context.cfgimpl_get_home_by_path(self._option_bag.path,
config_bag,
validate_properties=self._validate_properties)
except AssertionError as err:
raise APIError(str(err))
except Exception as err:
raise err
self._subconfig = subconfig
self._name = name
2018-10-07 10:55:52 +02:00
option = self._option_bag.option
2018-01-01 21:32:39 +01:00
if option is None:
2020-08-15 00:23:48 +02:00
option = await self._subconfig.cfgimpl_get_description().get_child(self._name,
2019-12-24 15:24:20 +01:00
config_bag,
self._subconfig.cfgimpl_get_path())
self._option_bag.option = option
2020-01-22 20:46:18 +01:00
self._option_bag.config_bag.connection = connection
2019-12-24 15:24:20 +01:00
# Calculate option's properties
settings = config_bag.context.cfgimpl_get_settings()
self._option_bag.properties = await settings.getproperties(self._option_bag)
2018-12-24 09:30:58 +01:00
if self._validate_properties:
2019-12-24 15:24:20 +01:00
await settings.validate_properties(self._option_bag)
2018-10-07 10:55:52 +02:00
index = self._option_bag.index
2018-08-03 22:56:04 +02:00
if index is not None:
2019-02-23 19:06:23 +01:00
if option.impl_is_optiondescription() or not option.impl_is_follower():
2019-12-24 15:24:20 +01:00
self._option_bag.option = None
2019-02-23 19:06:23 +01:00
raise APIError('index must be set only with a follower option')
2019-12-24 15:24:20 +01:00
self._length = await self._subconfig.cfgimpl_get_length_leadership(self._option_bag)
2018-08-03 22:56:04 +02:00
if index >= self._length:
2019-12-24 15:24:20 +01:00
self._option_bag.option = None
2019-06-12 08:45:56 +02:00
raise LeadershipError(_('index "{}" is greater than the leadership length "{}" '
2019-02-23 19:06:23 +01:00
'for option "{}"').format(index,
self._length,
option.impl_get_display_name()))
2019-06-12 08:45:56 +02:00
if not self._allow_optiondescription and option.impl_is_optiondescription():
2019-12-24 15:24:20 +01:00
self._option_bag.option = None
2019-06-12 08:45:56 +02:00
raise APIError(_('option must not be an optiondescription'))
2018-01-01 21:32:39 +01:00
return option
class CommonTiramisuOption(CommonTiramisu):
2018-10-07 10:55:52 +02:00
_allow_optiondescription = False
2019-02-23 19:06:23 +01:00
_follower_need_index = True
2018-12-24 09:30:58 +01:00
_validate_properties = False
def __init__(self,
2019-12-24 15:24:20 +01:00
option_bag: OptionBag) -> None:
2018-10-07 10:55:52 +02:00
self._option_bag = option_bag
2019-12-24 15:24:20 +01:00
self._subconfig = None
2017-10-22 09:48:08 +02:00
2018-10-07 10:55:52 +02:00
def __getattr__(self, name):
2019-12-24 15:24:20 +01:00
raise APIError(_('unknown method "{}" in "{}"').format(name, self.__class__.__name__))
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
def option_and_connection(func):
async def wrapped(self, *args, **kwargs):
config_bag = self._option_bag.config_bag
async with config_bag.context.getconnection() as connection:
config_bag.connection = connection
option = await self._get_option(connection)
ret = await func(self, *args, **kwargs)
del config_bag.connection
return ret
return wrapped
2018-10-07 10:55:52 +02:00
class _TiramisuOptionOptionDescription(CommonTiramisuOption):
"""Manage option"""
_allow_optiondescription = True
2019-02-23 19:06:23 +01:00
_follower_need_index = False
_validate_properties = False
2018-01-05 23:32:00 +01:00
2019-06-12 08:45:56 +02:00
def __init__(self,
2019-12-24 15:24:20 +01:00
option_bag: OptionBag):
super().__init__(option_bag)
self._config = option_bag.config_bag.context
2019-06-12 08:45:56 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def get(self):
2018-10-07 10:55:52 +02:00
"""Get Tiramisu option"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def type(self):
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_get_group_type()
2018-12-24 09:30:58 +01:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def isleadership(self):
2019-02-23 19:06:23 +01:00
"""Test if option is a leader or a follower"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_is_leadership()
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def doc(self):
2018-10-07 10:55:52 +02:00
"""Get option document"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_get_display_name()
2017-10-25 08:46:22 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def description(self):
"""Get option description"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_get_information('doc', None)
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def name(self,
follow_symlink: bool=False) -> str:
2018-10-07 10:55:52 +02:00
"""Get option name"""
if not follow_symlink or \
2020-01-22 20:46:18 +01:00
self._option_bag.option.impl_is_optiondescription() or \
not self._option_bag.option.impl_is_symlinkoption():
return self._option_bag.option.impl_getname()
else:
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_getopt().impl_getname()
2018-04-05 21:20:39 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def path(self) -> str:
2018-10-07 10:55:52 +02:00
"""Get option path"""
return self._option_bag.path
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def has_dependency(self, self_is_dep=True):
2018-10-07 10:55:52 +02:00
"""Test if option has dependency"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_has_dependency(self_is_dep)
2018-10-07 10:55:52 +02:00
2020-08-04 16:35:40 +02:00
@option_and_connection
async def dependencies(self):
"""Get dependencies from this option"""
options = []
for option in self._option_bag.option._get_dependencies(self._option_bag.config_bag.context):
options.append(TiramisuOption(option().impl_getpath(),
None,
self._option_bag.config_bag,
))
return options
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def isoptiondescription(self):
2018-10-07 10:55:52 +02:00
"""Test if option is an optiondescription"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_is_optiondescription()
2018-10-07 10:55:52 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def properties(self,
only_raises=False,
uncalculated=False):
"""Get properties for an option"""
settings = self._option_bag.config_bag.context.cfgimpl_get_settings()
if uncalculated:
2019-12-24 15:24:20 +01:00
return await settings.getproperties(self._option_bag,
uncalculated=True)
if not only_raises:
2019-12-24 15:24:20 +01:00
return await settings.getproperties(self._option_bag,
apply_requires=False)
# do not check cache properties/permissives which are not save (unrestraint, ...)
2019-12-24 15:24:20 +01:00
return await settings.calc_raises_properties(self._option_bag,
2019-12-25 20:44:56 +01:00
apply_requires=False,
uncalculated=uncalculated)
2019-03-02 21:31:21 +01:00
def __call__(self,
2019-06-12 08:45:56 +02:00
name: str,
2019-03-02 21:31:21 +01:00
index: Optional[int]=None) -> 'TiramisuOption':
"""Select an option by path"""
2019-06-12 08:45:56 +02:00
path = self._option_bag.path + '.' + name
2019-12-24 15:24:20 +01:00
return TiramisuOption(path,
2019-03-02 21:31:21 +01:00
index,
self._option_bag.config_bag)
2018-10-07 10:55:52 +02:00
2019-12-24 15:24:20 +01:00
class TiramisuOptionOption(_TiramisuOptionOptionDescription):
2018-10-07 10:55:52 +02:00
"""Manage option"""
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def ismulti(self):
2018-10-07 10:55:52 +02:00
"""Test if option could have multi value"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_is_multi()
2018-10-07 10:55:52 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def issubmulti(self):
2018-10-07 10:55:52 +02:00
"""Test if option could have submulti value"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_is_submulti()
2018-10-07 10:55:52 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def isleader(self):
2019-02-23 19:06:23 +01:00
"""Test if option is a leader"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_is_leader()
2018-10-07 10:55:52 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def isfollower(self):
2019-02-23 19:06:23 +01:00
"""Test if option is a follower"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_is_follower()
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def issymlinkoption(self) -> bool:
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_is_symlinkoption()
2018-12-24 09:30:58 +01:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def default(self):
2018-10-07 10:55:52 +02:00
"""Get default value for an option (not for optiondescription)"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_getdefault()
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def defaultmulti(self):
2018-10-07 10:55:52 +02:00
"""Get default value when added a value for a multi option (not for optiondescription)"""
2020-01-22 20:46:18 +01:00
option = self._option_bag.option
2019-06-21 23:04:04 +02:00
ret = option.impl_getdefault_multi()
if ret is None and option.impl_is_multi() and option.impl_has_callback() and not self.isfollower():
callback, callback_params = option.impl_get_callback()
values = self._option_bag.config_bag.context.cfgimpl_get_values()
2019-12-24 15:24:20 +01:00
value = await values.carry_out_calculation(self._option_bag,
callback,
callback_params)
2019-06-21 23:04:04 +02:00
if not isinstance(value, list):
ret = value
return ret
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def callbacks(self):
2018-10-07 10:55:52 +02:00
"""Get callbacks for an option (not for optiondescription)"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_get_callback()
2018-01-05 23:32:00 +01:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def validator(self):
2019-07-04 20:43:47 +02:00
"""Get validator for an option (not for optiondescription)"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_get_validator()
2019-07-04 20:43:47 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def type(self):
2020-01-22 20:46:18 +01:00
return self._option_bag.option.get_type()
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def pattern(self) -> str:
2020-01-22 20:46:18 +01:00
option = self._option_bag.option
2019-04-22 10:51:44 +02:00
type = option.get_type()
if isinstance(option, RegexpOption):
return option._regexp.pattern
if type == 'integer':
2019-05-09 20:32:43 +02:00
# FIXME negative too!
2019-04-22 10:51:44 +02:00
return r'^[0-9]+$'
if type == 'domainname':
return option.impl_get_extra('_domain_re').pattern
if type in ['ip', 'network', 'netmask']:
#FIXME only from 0.0.0.0 to 255.255.255.255
return r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
2018-01-05 23:32:00 +01:00
class TiramisuOptionOwner(CommonTiramisuOption):
2018-12-24 09:30:58 +01:00
#FIXME optiondescription must not have Owner!
2018-10-07 10:55:52 +02:00
"""Manage option's owner"""
2017-10-22 09:48:08 +02:00
2017-11-12 20:11:56 +01:00
def __init__(self,
2019-12-24 15:24:20 +01:00
option_bag: OptionBag) -> None:
2017-11-20 17:01:36 +01:00
2019-12-24 15:24:20 +01:00
super().__init__(option_bag)
2018-08-15 08:35:22 +02:00
if option_bag is not None:
# for help()
2018-10-07 10:55:52 +02:00
self._values = self._option_bag.config_bag.context.cfgimpl_get_values()
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def get(self):
2018-10-07 10:55:52 +02:00
"""Get owner for a specified option"""
2019-12-24 15:24:20 +01:00
return await self._values.getowner(self._option_bag)
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def isdefault(self):
2018-10-07 10:55:52 +02:00
"""Is option has defaut value"""
2019-12-24 15:24:20 +01:00
return await self._values.is_default_owner(self._option_bag)
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def set(self, owner):
2018-10-07 10:55:52 +02:00
"""Get owner for a specified option"""
2017-10-22 09:48:08 +02:00
try:
obj_owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
obj_owner = getattr(owners, owner)
2019-12-24 15:24:20 +01:00
await self._values.setowner(obj_owner,
self._option_bag)
2017-10-22 09:48:08 +02:00
class TiramisuOptionProperty(CommonTiramisuOption):
2018-10-07 10:55:52 +02:00
"""Manage option's property"""
_allow_optiondescription = True
2019-02-23 19:06:23 +01:00
_follower_need_index = False
2019-12-24 15:24:20 +01:00
_validate_properties = False
2017-11-12 20:11:56 +01:00
def __init__(self,
2019-12-24 15:24:20 +01:00
option_bag: OptionBag) -> None:
super().__init__(option_bag)
2018-08-15 08:35:22 +02:00
if option_bag and option_bag.config_bag:
2018-10-07 10:55:52 +02:00
self._settings = option_bag.config_bag.context.cfgimpl_get_settings()
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def get(self,
2019-12-25 20:44:56 +01:00
only_raises=False,
uncalculated=False):
2018-10-07 10:55:52 +02:00
"""Get properties for an option"""
2018-10-29 21:01:01 +01:00
if not only_raises:
2019-06-12 08:45:56 +02:00
return self._option_bag.properties
# do not check cache properties/permissives which are not save (unrestraint, ...)
2020-01-22 20:46:18 +01:00
ret = await self._settings.calc_raises_properties(self._option_bag,
uncalculated=uncalculated)
return ret
2017-10-22 09:48:08 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def add(self, prop):
2018-10-07 10:55:52 +02:00
"""Add new property for an option"""
if prop in FORBIDDEN_SET_PROPERTIES:
raise ConfigError(_('cannot add this property: "{0}"').format(
' '.join(prop)))
2020-01-22 20:46:18 +01:00
props = await self._settings._p_.getproperties(self._option_bag.config_bag.connection,
self._option_bag.path,
2019-12-24 15:24:20 +01:00
self._option_bag.index,
2020-01-22 20:46:18 +01:00
self._option_bag.option.impl_getproperties())
2019-12-24 15:24:20 +01:00
await self._settings.setproperties(self._option_bag.path,
props | {prop},
self._option_bag,
self._option_bag.config_bag.context)
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def pop(self, prop):
2018-10-07 10:55:52 +02:00
"""Remove new property for an option"""
2020-01-22 20:46:18 +01:00
props = await self._settings._p_.getproperties(self._option_bag.config_bag.connection,
self._option_bag.path,
2019-12-24 15:24:20 +01:00
self._option_bag.index,
2020-01-22 20:46:18 +01:00
self._option_bag.option.impl_getproperties())
2019-12-24 15:24:20 +01:00
await self._settings.setproperties(self._option_bag.path,
props - {prop},
self._option_bag,
self._option_bag.config_bag.context)
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def reset(self):
2018-10-07 10:55:52 +02:00
"""Reset all personalised properties"""
2019-12-24 15:24:20 +01:00
await self._settings.reset(self._option_bag,
2020-01-22 20:46:18 +01:00
self._option_bag.config_bag)
2017-10-22 09:48:08 +02:00
class TiramisuOptionPermissive(CommonTiramisuOption):
2018-10-07 10:55:52 +02:00
"""Manage option's permissive"""
_allow_optiondescription = True
2019-02-23 19:06:23 +01:00
_follower_need_index = False
2017-11-12 20:11:56 +01:00
def __init__(self,
2019-12-24 15:24:20 +01:00
option_bag: OptionBag) -> None:
super().__init__(option_bag)
2018-08-15 08:35:22 +02:00
if option_bag and option_bag.config_bag:
2018-10-07 10:55:52 +02:00
self._settings = option_bag.config_bag.context.cfgimpl_get_settings()
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def get(self):
2018-10-07 10:55:52 +02:00
"""Get permissives value"""
2019-12-24 15:24:20 +01:00
return await self._settings.getpermissives(self._option_bag)
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def set(self, permissives):
2018-10-07 10:55:52 +02:00
"""Set permissives value"""
2019-12-24 15:24:20 +01:00
await self._settings.setpermissives(self._option_bag,
permissives=permissives)
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def reset(self):
2018-10-07 10:55:52 +02:00
"""Reset all personalised permissive"""
2019-12-24 15:24:20 +01:00
await self._settings.reset_permissives(self._option_bag,
2020-01-22 20:46:18 +01:00
self._option_bag.config_bag)
2017-12-13 22:15:34 +01:00
class TiramisuOptionInformation(CommonTiramisuOption):
2018-10-07 10:55:52 +02:00
"""Manage option's informations"""
_allow_optiondescription = True
2019-02-23 19:06:23 +01:00
_follower_need_index = False
2017-12-13 22:15:34 +01:00
2019-12-24 15:24:20 +01:00
def __init__(self,
option_bag: OptionBag) -> None:
super().__init__(option_bag)
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def get(self, key, default=undefined):
2018-10-07 10:55:52 +02:00
"""Get information"""
values = self._option_bag.config_bag.context.cfgimpl_get_values()
2021-03-06 19:23:35 +01:00
return await values.get_information(self._option_bag.config_bag,
self._option_bag,
key,
default,
)
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def set(self, key, value):
2018-10-07 10:55:52 +02:00
"""Set information"""
path = self._option_bag.path
values = self._option_bag.config_bag.context.cfgimpl_get_values()
2021-03-06 19:23:35 +01:00
await values.set_information(self._option_bag.config_bag,
self._option_bag,
2020-01-22 20:46:18 +01:00
key,
value,
2021-03-06 19:23:35 +01:00
)
2018-04-07 20:15:19 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def reset(self,
2021-05-18 18:53:14 +02:00
key: str,
) -> None:
2018-10-07 10:55:52 +02:00
"""Remove information"""
path = self._option_bag.path
values = self._option_bag.config_bag.context.cfgimpl_get_values()
2020-01-22 20:46:18 +01:00
await values.del_information(self._option_bag.config_bag.connection,
key,
2019-12-24 15:24:20 +01:00
path=path)
2018-04-07 20:15:19 +02:00
2020-01-22 20:46:18 +01:00
@option_and_connection
2019-12-24 15:24:20 +01:00
async def list(self):
2018-10-07 10:55:52 +02:00
"""List information's keys"""
path = self._option_bag.path
values = self._option_bag.config_bag.context.cfgimpl_get_values()
2020-01-22 20:46:18 +01:00
return await values.list_information(self._option_bag.config_bag.connection,
path)
#
# async def len(self):
# """Length of leadership"""
# option = await self._get_option()
# # for example if index is None
# if '_length' not in vars(self):
# self._length = self._subconfig.cfgimpl_get_length()
# return self._length
2017-12-13 22:15:34 +01:00
2017-10-22 09:48:08 +02:00
2019-12-24 15:24:20 +01:00
def option_type(typ):
if not isinstance(typ, list):
types = [typ]
else:
types = typ
def wrapper(func):
@wraps(func)
async def wrapped(*args, **kwargs):
2020-01-22 20:46:18 +01:00
config_bag = args[0]._option_bag.config_bag
async with config_bag.context.getconnection() as connection:
for typ in types:
if typ == 'group':
if args[0]._option_bag.config_bag.context.impl_type == 'group':
config_bag.connection = connection
ret = await func(*args, **kwargs, is_group=True)
del config_bag.connection
return ret
else:
config_bag.connection = connection
option = await args[0]._get_option(connection)
if typ == 'option':
if option.impl_is_optiondescription():
del config_bag.connection
raise APIError(_('please specify a valid sub function ({})').format(func.__name__))
elif typ == 'optiondescription':
if not option.impl_is_optiondescription():
del config_bag.connection
raise APIError(_('please specify a valid sub function ({})').format(func.__name__))
elif typ == 'leader':
if not option.impl_is_leader():
del config_bag.connection
raise APIError(_('please specify a valid sub function ({})').format(func.__name__))
elif typ == 'follower':
if not option.impl_is_follower() and not option.impl_is_leader():
del config_bag.connection
raise APIError(_('please specify a valid sub function ({})').format(func.__name__))
elif typ == 'choice':
if not option.get_type() == 'choice':
del config_bag.connection
raise APIError(_('please specify a valid sub function ({})').format(func.__name__))
ret = await func(*args, **kwargs)
del config_bag.connection
return ret
2019-12-24 15:24:20 +01:00
return wrapped
return wrapper
class TiramisuOptionValue(CommonTiramisuOption):
"""Manage option's value"""
_allow_optiondescription = True
_follower_need_index = True
_validate_properties = True
@option_type('optiondescription')
async def dict(self,
flatten=False,
withwarning: bool=False,
2019-12-28 12:30:32 +01:00
fullpath=False,
leader_to_list=False):
2019-12-24 15:24:20 +01:00
"""Dict with path as key and value"""
name = self._option_bag.option.impl_getname()
subconfig = await self._subconfig.get_subconfig(self._option_bag)
config_bag = self._option_bag.config_bag
if not withwarning and config_bag.properties and 'warnings' in config_bag.properties:
config_bag = config_bag.copy()
config_bag.remove_warnings()
return await subconfig.make_dict(config_bag=config_bag,
flatten=flatten,
2019-12-28 12:30:32 +01:00
fullpath=fullpath,
leader_to_list=leader_to_list)
2019-12-24 15:24:20 +01:00
@option_type('option')
async def get(self):
2018-10-07 10:55:52 +02:00
"""Get option's value"""
2019-12-24 15:24:20 +01:00
if self._option_bag.option.impl_is_follower() and self._option_bag.index is None:
raise APIError('index must be set with a follower option')
return await self._subconfig.getattr(self._name,
self._option_bag)
2018-08-02 22:35:40 +02:00
2019-12-24 15:24:20 +01:00
@option_type('option')
async def set(self, value):
2018-10-07 10:55:52 +02:00
"""Change option's value"""
2019-12-24 15:24:20 +01:00
if self._option_bag.option.impl_is_follower() and \
self._option_bag.index is None:
raise APIError('index must be set with a follower option')
2018-10-07 10:55:52 +02:00
values = self._option_bag.config_bag.context.cfgimpl_get_values()
if isinstance(value, list):
while undefined in value:
idx = value.index(undefined)
2019-03-13 08:49:18 +01:00
soption_bag = self._option_bag.copy()
soption_bag.index = idx
2019-12-24 15:24:20 +01:00
value[idx] = await values.getdefaultvalue(soption_bag)
2018-09-29 08:27:00 +02:00
elif value == undefined:
2019-12-24 15:24:20 +01:00
value = await values.getdefaultvalue(self._option_bag)
await self._subconfig.setattr(value,
self._option_bag)
2018-10-07 10:55:52 +02:00
2019-12-24 15:24:20 +01:00
@option_type(['group', 'option'])
async def reset(self,
is_group: bool=False):
2018-10-07 10:55:52 +02:00
"""Reset value for an option"""
2019-12-24 15:24:20 +01:00
if is_group:
2020-01-22 20:46:18 +01:00
await self._option_bag.config_bag.context.reset(self._option_bag.config_bag.connection,
self._option_bag.path)
2019-12-24 15:24:20 +01:00
else:
if self._option_bag.option.impl_is_follower() and self._option_bag.index is None:
raise APIError('index must be set with a follower option')
await self._subconfig.delattr(self._option_bag)
2017-10-22 09:48:08 +02:00
2019-12-24 15:24:20 +01:00
@option_type('option')
async def default(self):
2018-11-13 12:39:51 +01:00
"""Get default value (default of option or calculated value)"""
2020-01-22 20:46:18 +01:00
option = self._option_bag.option
2018-11-13 12:39:51 +01:00
values = self._option_bag.config_bag.context.cfgimpl_get_values()
2019-02-23 19:06:23 +01:00
if option.impl_is_follower() and self._option_bag.index is None:
2018-11-13 12:39:51 +01:00
value = []
2019-12-24 15:24:20 +01:00
length = await self._subconfig.cfgimpl_get_length_leadership(self._option_bag)
settings = self._option_bag.config_bag.context.cfgimpl_get_settings()
2018-11-13 12:39:51 +01:00
for idx in range(length):
soption_bag = OptionBag()
soption_bag.set_option(option,
idx,
self._option_bag.config_bag)
2019-12-24 15:24:20 +01:00
soption_bag.properties = await settings.getproperties(soption_bag)
value.append(await values.getdefaultvalue(soption_bag))
2019-02-25 08:46:58 +01:00
return value
2018-11-13 12:39:51 +01:00
else:
2019-12-24 15:24:20 +01:00
return await values.getdefaultvalue(self._option_bag)
2018-11-13 12:39:51 +01:00
2019-12-24 15:24:20 +01:00
@option_type('option')
async def valid(self):
try:
2019-11-19 18:39:44 +01:00
with catch_warnings(record=True) as warns:
simplefilter("always", ValueErrorWarning)
2019-12-24 15:24:20 +01:00
await self.get()
for warn in warns:
if isinstance(warns.message, ValueErrorWarning):
return False
except ValueError:
return False
return True
2019-12-24 15:24:20 +01:00
@option_type('choice')
async def list(self):
2019-11-19 18:39:44 +01:00
"""All values available for a ChoiceOption"""
2020-01-22 20:46:18 +01:00
return await self._option_bag.option.impl_get_values(self._option_bag)
2019-11-19 18:39:44 +01:00
2019-12-24 15:24:20 +01:00
@option_type('leader')
async def pop(self, index):
2018-10-07 10:55:52 +02:00
"""Pop a value"""
2019-12-24 15:24:20 +01:00
if self._option_bag.option.impl_is_follower() and self._option_bag.index is None:
raise APIError('index must be set with a follower option')
2019-02-23 19:06:23 +01:00
option_bag = self._option_bag
assert not option_bag.option.impl_is_symlinkoption(), _("can't delete a SymLinkOption")
2019-12-24 15:24:20 +01:00
await option_bag.config_bag.context.cfgimpl_get_values().reset_leadership(index,
option_bag,
self._subconfig)
2018-07-07 17:11:33 +02:00
2019-12-24 15:24:20 +01:00
@option_type('follower')
async def len(self):
2019-02-23 19:06:23 +01:00
"""Length of follower option"""
2018-04-19 08:19:03 +02:00
# for example if index is None
if '_length' not in vars(self):
2019-12-24 15:24:20 +01:00
self._length = await self._subconfig.cfgimpl_get_length_leadership(self._option_bag)
2018-04-19 08:19:03 +02:00
return self._length
2018-10-07 10:55:52 +02:00
def _registers(_registers: Dict[str, type],
prefix: str,
2019-12-24 15:24:20 +01:00
extra_type: Optional[type]=None):
2017-12-13 22:15:34 +01:00
for module_name in globals().keys():
2019-12-24 15:24:20 +01:00
if module_name != prefix and module_name.startswith(prefix): # and \
2017-12-13 22:15:34 +01:00
module = globals()[module_name]
func_name = module_name[len(prefix):].lower()
2018-10-07 10:55:52 +02:00
_registers[func_name] = module
2019-12-24 15:24:20 +01:00
#__________________________________________________________________________________________________
#
2017-12-13 22:15:34 +01:00
2017-10-22 09:48:08 +02:00
2019-12-24 15:24:20 +01:00
class TiramisuConfig(TiramisuHelp):
def __init__(self,
config_bag: ConfigBag,
orig_config_bags: Optional[List[OptionBag]]) -> None:
self._config_bag = config_bag
self._orig_config_bags = orig_config_bags
async def _return_config(self,
2020-01-22 20:46:18 +01:00
config,
storage):
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelConfig):
2020-01-22 20:46:18 +01:00
return await Config(config,
storage=storage)
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelMetaConfig):
2020-01-22 20:46:18 +01:00
return await MetaConfig(config,
storage=storage)
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelMixConfig):
2020-01-22 20:46:18 +01:00
return await MixConfig([],
config,
storage=storage)
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelGroupConfig):
2020-02-18 22:10:33 +01:00
return await GroupConfig(config)
2019-12-24 15:24:20 +01:00
raise Exception(_('unknown config type {}').format(type(config)))
2020-01-22 20:46:18 +01:00
async def _reset_config_properties(self,
connection):
2019-12-24 15:24:20 +01:00
config = self._config_bag.context
settings = config.cfgimpl_get_settings()
2020-01-22 20:46:18 +01:00
properties = await settings.get_context_properties(connection,
config._impl_properties_cache)
permissives = await settings.get_context_permissives(connection)
2019-12-24 15:24:20 +01:00
self._config_bag.properties = properties
self._config_bag.permissives = permissives
if self._orig_config_bags:
for config_bag in self._orig_config_bags:
config_bag.properties = properties
config_bag.permissives = permissives
class TiramisuOption(CommonTiramisu, TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Manage selected option"""
_validate_properties = False
2018-10-07 10:55:52 +02:00
_registers = {}
2017-11-12 20:11:56 +01:00
def __init__(self,
2018-04-28 08:39:07 +02:00
path: Optional[str]=None,
index: Optional[int]=None,
2018-10-07 10:55:52 +02:00
config_bag: Optional[ConfigBag]=None) -> None:
self._option_bag = OptionBag()
2019-12-24 15:24:20 +01:00
self._option_bag.config_bag = config_bag
self._option_bag.path = path
self._option_bag.index = index
self._subconfig = None
self._tiramisu_dict = None
2018-10-07 10:55:52 +02:00
if not self._registers:
_registers(self._registers, 'TiramisuOption')
2017-10-22 09:48:08 +02:00
2018-04-28 08:39:07 +02:00
def __getattr__(self, subfunc: str) -> Any:
2018-10-07 10:55:52 +02:00
if subfunc in self._registers:
2019-12-24 15:24:20 +01:00
return self._registers[subfunc](self._option_bag)
2018-09-22 17:45:52 +02:00
raise APIError(_('please specify a valid sub function ({})').format(subfunc)) # pragma: no cover
2019-08-03 21:03:23 +02:00
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
async def find(self,
name: str,
value=undefined,
type=None,
first: bool=False):
2018-04-10 12:33:51 +02:00
"""find an option by name (only for optiondescription)"""
if not first:
ret = []
2020-01-22 20:46:18 +01:00
option = self._option_bag.option
2019-12-24 15:24:20 +01:00
config_bag = self._option_bag.config_bag
oname = option.impl_getname()
2018-10-07 10:55:52 +02:00
path = self._subconfig._get_subpath(oname)
option_bag = OptionBag()
option_bag.set_option(option,
None,
2019-12-24 15:24:20 +01:00
config_bag)
subconfig = await self._subconfig.get_subconfig(option_bag)
async for path in subconfig.find(byname=name,
byvalue=value,
bytype=type,
_subpath=option_bag.path,
config_bag=config_bag):
t_option = TiramisuOption(path,
2019-02-23 19:06:23 +01:00
None, # index for a follower ?
2019-12-24 15:24:20 +01:00
config_bag)
2018-04-10 12:33:51 +02:00
if first:
return t_option
ret.append(t_option)
return ret
2018-08-01 08:37:58 +02:00
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
async def group_type(self):
2018-10-07 10:55:52 +02:00
"""Get type for an optiondescription (only for optiondescription)"""
2020-01-22 20:46:18 +01:00
return self._option_bag.option.impl_get_group_type()
2019-12-24 15:24:20 +01:00
async def _filter(self,
opt,
subconfig,
config_bag):
settings = config_bag.context.cfgimpl_get_settings()
option_bag = OptionBag()
option_bag.set_option(opt,
2019-08-05 22:31:56 +02:00
None,
config_bag)
2019-12-24 15:24:20 +01:00
option_bag.properties = await settings.getproperties(option_bag)
if opt.impl_is_optiondescription():
2019-12-24 15:24:20 +01:00
await settings.validate_properties(option_bag)
return await subconfig.get_subconfig(option_bag)
await subconfig.getattr(opt.impl_getname(),
option_bag)
@option_type('optiondescription')
async def list(self,
type='option',
group_type=None):
"""List options (by default list only option)"""
assert type in ('all', 'option', 'optiondescription'), _('unknown list type {}').format(type)
assert group_type is None or isinstance(group_type, groups.GroupType), \
_("unknown group_type: {0}").format(group_type)
2019-12-24 15:24:20 +01:00
config_bag = self._option_bag.config_bag
if config_bag.properties and 'warnings' in config_bag.properties:
config_bag = config_bag.copy()
config_bag.remove_warnings()
2020-01-22 20:46:18 +01:00
option = self._option_bag.option
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
option_bag.set_option(option,
None,
config_bag)
2019-12-24 15:24:20 +01:00
subconfig = await self._subconfig.get_subconfig(option_bag)
options = []
for opt in await option.get_children(config_bag):
2018-08-01 08:37:58 +02:00
try:
2019-12-24 15:24:20 +01:00
await self._filter(opt,
subconfig,
config_bag)
2018-08-01 08:37:58 +02:00
except PropertiesOptionError:
continue
if opt.impl_is_optiondescription():
if type == 'option' or (type == 'optiondescription' and \
group_type and opt.impl_get_group_type() != group_type):
2018-08-01 08:37:58 +02:00
continue
2018-09-10 21:59:54 +02:00
elif type == 'optiondescription':
continue
2019-12-24 15:24:20 +01:00
options.append(TiramisuOption(opt.impl_getpath(),
None,
self._option_bag.config_bag))
return options
async def _load_dict(self,
clearable: str="all",
remotable: str="minimum"):
2020-01-22 20:46:18 +01:00
root = self._option_bag.option.impl_getpath()
config = self._option_bag.config_bag.context
self._tiramisu_dict = TiramisuDict(await self._return_config(config,
config._storage),
2019-08-03 21:03:23 +02:00
root=root,
clearable=clearable,
remotable=remotable)
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
async def dict(self,
clearable: str="all",
remotable: str="minimum",
form: List=[],
force: bool=False) -> Dict:
"""convert config and option to tiramisu format"""
if force or self._tiramisu_dict is None:
2019-12-24 15:24:20 +01:00
await self._load_dict(clearable, remotable)
return await self._tiramisu_dict.todict(form)
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
async def updates(self,
body: List) -> Dict:
"""updates value with tiramisu format"""
if self._tiramisu_dict is None:
2019-12-24 15:24:20 +01:00
await self._load_dict()
return await self._tiramisu_dict.set_updates(body)
2020-01-22 20:46:18 +01:00
def connection(func):
async def wrapped(self, *args, **kwargs):
config_bag = self._config_bag
async with config_bag.context.getconnection() as connection:
config_bag.connection = connection
ret = await func(self, *args, **kwargs)
del config_bag.connection
return ret
return wrapped
2019-08-03 21:03:23 +02:00
class TiramisuContextInformation(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Manage config informations"""
2020-01-22 20:46:18 +01:00
@connection
async def get(self,
name,
default=undefined,
):
2018-10-07 10:55:52 +02:00
"""Get an information"""
2021-03-06 19:23:35 +01:00
values = self._config_bag.context.cfgimpl_get_values()
return await values.get_information(self._config_bag,
None,
name,
default,
)
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@connection
async def set(self,
name,
value,
):
2018-10-07 10:55:52 +02:00
"""Set an information"""
await self._config_bag.context.impl_set_information(self._config_bag,
2020-01-22 20:46:18 +01:00
name,
value,
)
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@connection
async def reset(self,
name,
):
2018-10-07 10:55:52 +02:00
"""Remove an information"""
2020-01-22 20:46:18 +01:00
await self._config_bag.context.impl_del_information(self._config_bag.connection,
name,
)
2018-10-07 10:55:52 +02:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def list(self):
2018-10-07 10:55:52 +02:00
"""List information's keys"""
2020-01-22 20:46:18 +01:00
return await self._config_bag.context.impl_list_information(self._config_bag.connection)
2017-12-13 22:15:34 +01:00
2021-05-18 18:53:14 +02:00
@connection
async def exportation(self):
"""Export all informations"""
return await self._config_bag.context.cfgimpl_get_values()._p_.exportation_informations(self._config_bag.connection)
@connection
async def importation(self, informations):
"""Import informations"""
return await self._config_bag.context.cfgimpl_get_values()._p_.importation_informations(self._config_bag.connection, informations)
2017-12-13 22:15:34 +01:00
2019-08-03 21:03:23 +02:00
class TiramisuContextValue(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Manage config value"""
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def mandatory(self):
2018-10-07 10:55:52 +02:00
"""Return path of options with mandatory property without any value"""
2019-12-24 15:24:20 +01:00
options = []
async for option in self._config_bag.context.cfgimpl_get_values().mandatory_warnings(self._config_bag):
options.append(option)
return options
2017-12-13 22:15:34 +01:00
2019-11-19 18:39:44 +01:00
# FIXME should be only for group/meta
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def set(self,
path: str,
value,
index=None,
only_config=undefined,
force_default=undefined,
force_default_if_same=undefined,
force_dont_change_value=undefined):
2018-10-07 10:55:52 +02:00
"""Set a value in config or children for a path"""
2018-01-03 21:07:51 +01:00
kwargs = {}
if only_config is not undefined:
kwargs['only_config'] = only_config
if force_default is not undefined:
kwargs['force_default'] = force_default
if force_default_if_same is not undefined:
kwargs['force_default_if_same'] = force_default_if_same
if force_dont_change_value is not undefined:
kwargs['force_dont_change_value'] = force_dont_change_value
2019-12-24 15:24:20 +01:00
return await self._config_bag.context.set_value(path,
index,
value,
self._config_bag,
**kwargs)
2018-10-31 16:08:22 +01:00
2019-11-19 18:39:44 +01:00
# FIXME should be only for group/meta
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def reset(self,
2018-10-31 16:08:22 +01:00
path: str,
only_children: bool=False):
"""Reset value"""
2019-12-24 15:24:20 +01:00
await self._config_bag.context.reset(path,
only_children,
self._config_bag)
2018-01-03 21:07:51 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def dict(self,
2019-12-25 20:44:56 +01:00
flatten=False,
withwarning: bool=False,
fullpath=False,
leader_to_list=False):
2018-10-07 10:55:52 +02:00
"""Dict with path as key and value"""
2018-11-17 23:14:47 +01:00
config_bag = self._config_bag
2019-12-24 15:24:20 +01:00
if not withwarning and 'warnings' in config_bag.properties:
2018-12-24 09:30:58 +01:00
config_bag = config_bag.copy()
2018-11-17 23:14:47 +01:00
config_bag.remove_warnings()
2019-12-24 15:24:20 +01:00
return await config_bag.context.make_dict(config_bag,
flatten=flatten,
fullpath=fullpath,
2019-12-25 20:44:56 +01:00
leader_to_list=leader_to_list)
2019-12-24 15:24:20 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def exportation(self,
with_default_owner: bool=False):
2018-10-07 10:55:52 +02:00
"""Export all values"""
2020-01-22 20:46:18 +01:00
exportation = await self._config_bag.context.cfgimpl_get_values()._p_.exportation(self._config_bag.connection)
if not with_default_owner:
exportation = [list(exportation[0]), list(exportation[1]), list(exportation[2]), list(exportation[3])]
index = exportation[0].index(None)
exportation[0].pop(index)
exportation[1].pop(index)
exportation[2].pop(index)
exportation[3].pop(index)
return exportation
2018-04-04 16:47:28 +02:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def importation(self, values):
2018-10-07 10:55:52 +02:00
"""Import values"""
2020-01-22 20:46:18 +01:00
cvalues = self._config_bag.context.cfgimpl_get_values()
connection = self._config_bag.connection
2018-09-15 22:44:49 +02:00
if None not in values[0]:
2020-01-22 20:46:18 +01:00
context_owner = await cvalues.get_context_owner(connection)
2018-09-15 22:44:49 +02:00
else:
context_owner = None
2020-01-22 20:46:18 +01:00
await cvalues._p_.importation(connection,
values)
2019-12-24 15:24:20 +01:00
await self._config_bag.context.cfgimpl_reset_cache(None, None)
2018-09-15 22:44:49 +02:00
if context_owner is not None:
2020-01-22 20:46:18 +01:00
await cvalues._p_.setvalue(connection,
None,
None,
context_owner,
None,
True)
class TiramisuContextSession(TiramisuConfig):
"""Manage Config session"""
async def reset(self):
await self._config_bag.context.cfgimpl_get_values()._p_._storage.delete_session()
await self._config_bag.context.cfgimpl_get_settings()._p_._storage.delete_session()
async def list(self):
return await self._config_bag.context.cfgimpl_get_values()._p_._storage.list_sessions()
async def id(self):
"""Get config name"""
return self._config_bag.context.impl_getname()
2017-12-13 22:15:34 +01:00
2019-08-03 21:03:23 +02:00
class TiramisuContextOwner(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Global owner"""
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def get(self):
2018-10-07 10:55:52 +02:00
"""Get owner"""
2020-01-22 20:46:18 +01:00
return await self._config_bag.context.cfgimpl_get_values().get_context_owner(self._config_bag.connection)
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def set(self, owner):
2018-10-07 10:55:52 +02:00
"""Set owner"""
2017-12-23 12:29:45 +01:00
try:
obj_owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
obj_owner = getattr(owners, owner)
2020-01-22 20:46:18 +01:00
values = self._config_bag.context.cfgimpl_get_values()
await values.set_context_owner(self._config_bag.connection,
obj_owner)
2017-12-23 12:29:45 +01:00
2019-08-03 21:03:23 +02:00
class TiramisuContextProperty(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Manage config properties"""
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def read_only(self):
2018-10-07 10:55:52 +02:00
"""Set config to read only mode"""
2019-02-21 19:33:39 +01:00
old_props = self._config_bag.properties
2018-10-07 10:55:52 +02:00
settings = self._config_bag.context.cfgimpl_get_settings()
2020-01-22 20:46:18 +01:00
await settings.read_only(self._config_bag)
await self._reset_config_properties(self._config_bag.connection)
2019-02-21 19:33:39 +01:00
if 'force_store_value' not in old_props and \
'force_store_value' in self._config_bag.properties:
2019-12-24 15:24:20 +01:00
await self._force_store_value()
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def read_write(self):
2018-10-07 10:55:52 +02:00
"""Set config to read and write mode"""
2019-02-21 19:33:39 +01:00
old_props = self._config_bag.properties
2018-10-07 10:55:52 +02:00
settings = self._config_bag.context.cfgimpl_get_settings()
2020-01-22 20:46:18 +01:00
connection = self._config_bag.connection
await settings.read_write(self._config_bag)
or_properties = settings.rw_append - settings.ro_append - SPECIAL_PROPERTIES
2020-01-22 20:46:18 +01:00
permissives = frozenset(await settings.get_context_permissives(connection) | or_properties)
await settings.set_context_permissives(connection,
permissives)
await self._reset_config_properties(connection)
2019-02-21 19:33:39 +01:00
if 'force_store_value' not in old_props and \
'force_store_value' in self._config_bag.properties:
2019-12-24 15:24:20 +01:00
await self._force_store_value()
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def add(self, prop):
2018-10-07 10:55:52 +02:00
"""Add a config property"""
2020-01-22 20:46:18 +01:00
settings = self._config_bag.context.cfgimpl_get_settings()
2019-12-24 15:24:20 +01:00
props = set(await self.get())
2020-01-22 20:46:18 +01:00
if prop not in props:
props.add(prop)
await self._set(self._config_bag.connection, frozenset(props))
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def pop(self, prop):
2018-10-07 10:55:52 +02:00
"""Remove a config property"""
2019-12-24 15:24:20 +01:00
props = set(await self.get())
2018-04-03 14:27:20 +02:00
if prop in props:
props.remove(prop)
2020-01-22 20:46:18 +01:00
await self._set(self._config_bag.connection, frozenset(props))
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
async def get(self,
default=False):
2018-10-07 10:55:52 +02:00
"""Get all config properties"""
2020-01-22 20:46:18 +01:00
if default:
config = self._config_bag.context
async with config.getconnection() as connection:
properties = await config.cfgimpl_get_settings().get_context_properties(connection,
config._impl_properties_cache)
return self._config_bag.properties
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
async def _set(self,
connection,
props):
2018-10-07 10:55:52 +02:00
"""Personalise config properties"""
2019-02-21 19:33:39 +01:00
if 'force_store_value' in props:
force_store_value = 'force_store_value' not in self._config_bag.properties
else:
force_store_value = False
2018-10-07 10:55:52 +02:00
context = self._config_bag.context
2020-01-22 20:46:18 +01:00
await context.cfgimpl_get_settings().set_context_properties(self._config_bag.connection,
props,
self._config_bag.context)
await self._reset_config_properties(connection)
2019-02-21 19:33:39 +01:00
if force_store_value:
2019-12-24 15:24:20 +01:00
await self._force_store_value()
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def reset(self):
2018-10-07 10:55:52 +02:00
"""Remove config properties"""
context = self._config_bag.context
2019-12-24 15:24:20 +01:00
await context.cfgimpl_get_settings().reset(None,
2020-01-22 20:46:18 +01:00
self._config_bag)
await self._reset_config_properties(self._config_bag.connection)
2017-12-23 20:21:07 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def exportation(self):
2018-10-07 10:55:52 +02:00
"""Export config properties"""
2020-01-22 20:46:18 +01:00
return await self._config_bag.context.cfgimpl_get_settings()._p_.exportation(self._config_bag.connection)
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def importation(self, properties):
2018-10-07 10:55:52 +02:00
"""Import config properties"""
2019-11-19 18:39:44 +01:00
if 'force_store_value' in properties.get(None, {}).get(None, []):
2019-02-21 19:33:39 +01:00
force_store_value = 'force_store_value' not in self._config_bag.properties
else:
force_store_value = False
2020-01-22 20:46:18 +01:00
settings = self._config_bag.context.cfgimpl_get_settings()
connection = self._config_bag.connection
await self._config_bag.context.cfgimpl_get_settings()._p_.importation(connection,
properties)
2019-12-24 15:24:20 +01:00
await self._config_bag.context.cfgimpl_reset_cache(None, None)
2020-01-22 20:46:18 +01:00
await self._reset_config_properties(connection)
2019-02-21 19:33:39 +01:00
if force_store_value:
2019-12-24 15:24:20 +01:00
await self._force_store_value()
2019-02-21 19:33:39 +01:00
2019-12-24 15:24:20 +01:00
async def _force_store_value(self):
2019-02-21 19:33:39 +01:00
descr = self._config_bag.context.cfgimpl_get_description()
2019-12-24 15:24:20 +01:00
await descr.impl_build_force_store_values(self._config_bag)
2019-12-24 15:24:20 +01:00
async def setdefault(self,
properties: Set[str],
type: Optional[str]=None,
when: Optional[str]=None) -> None:
if not isinstance(properties, frozenset):
raise TypeError(_('properties must be a frozenset'))
setting = self._config_bag.context.cfgimpl_get_settings()
if type is None and when is None:
setting.default_properties = properties
else:
if when not in ['append', 'remove']:
raise ValueError(_('unknown when {} (must be in append or remove)').format(when))
if type == 'read_only':
if when == 'append':
setting.ro_append = properties
else:
setting.ro_remove = properties
elif type == 'read_write':
if when == 'append':
setting.rw_append = properties
else:
setting.rw_remove = properties
else:
raise ValueError(_('unknown type {}').format(type))
2019-12-24 15:24:20 +01:00
async def getdefault(self,
type: Optional[str]=None,
when: Optional[str]=None) -> Set[str]:
setting = self._config_bag.context.cfgimpl_get_settings()
if type is None and when is None:
return setting.default_properties
if when not in ['append', 'remove']:
raise ValueError(_('unknown when {} (must be in append or remove)').format(when))
if type == 'read_only':
if when == 'append':
return setting.ro_append
else:
return setting.ro_remove
elif type == 'read_write':
if when == 'append':
return setting.rw_append
else:
return setting.rw_remove
else:
raise ValueError(_('unknown type {}').format(type))
2017-12-13 22:15:34 +01:00
2019-08-03 21:03:23 +02:00
class TiramisuContextPermissive(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Manage config permissives"""
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def get(self):
2018-10-07 10:55:52 +02:00
"""Get config permissives"""
2020-01-22 20:46:18 +01:00
return await self._get()
async def _get(self):
return await self._config_bag.context.cfgimpl_get_settings().get_context_permissives(self._config_bag.connection)
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
async def _set(self,
permissives):
2018-10-07 10:55:52 +02:00
"""Set config permissives"""
2020-01-22 20:46:18 +01:00
connection = self._config_bag.connection
await self._config_bag.context.cfgimpl_get_settings().set_context_permissives(connection, permissives)
await self._reset_config_properties(connection)
2017-12-13 22:15:34 +01:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def exportation(self):
2018-10-07 10:55:52 +02:00
"""Export config permissives"""
2020-01-22 20:46:18 +01:00
return await self._config_bag.context.cfgimpl_get_settings()._pp_.exportation(self._config_bag.connection)
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def importation(self, permissives):
2018-10-07 10:55:52 +02:00
"""Import config permissives"""
2020-01-22 20:46:18 +01:00
settings = self._config_bag.context.cfgimpl_get_settings()
connection = self._config_bag.connection
await settings._pp_.importation(connection,
permissives)
2019-12-24 15:24:20 +01:00
await self._config_bag.context.cfgimpl_reset_cache(None,
None)
2020-01-22 20:46:18 +01:00
await self._reset_config_properties(connection)
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def reset(self):
"""Remove config permissives"""
context = self._config_bag.context
2020-01-22 20:46:18 +01:00
settings = context.cfgimpl_get_settings()
connection = self._config_bag.connection
await settings.reset_permissives(None,
self._config_bag)
await self._reset_config_properties(connection)
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def add(self, prop):
"""Add a config permissive"""
2020-01-22 20:46:18 +01:00
props = set(await self._get())
props.add(prop)
2019-12-24 15:24:20 +01:00
await self._set(frozenset(props))
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def pop(self, prop):
"""Remove a config permissive"""
2020-01-22 20:46:18 +01:00
props = set(await self._get())
if prop in props:
props.remove(prop)
2019-12-24 15:24:20 +01:00
await self._set(frozenset(props))
2017-12-13 22:15:34 +01:00
2019-08-03 21:03:23 +02:00
class TiramisuContextOption(TiramisuConfig):
def __init__(self,
*args,
**kwargs) -> None:
self._tiramisu_dict = None
super().__init__(*args, **kwargs)
2018-10-07 10:55:52 +02:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def find(self,
2020-01-22 20:46:18 +01:00
name,
value=undefined,
type=None,
first=False):
2018-10-07 10:55:52 +02:00
"""Find an or a list of options"""
2019-12-24 15:24:20 +01:00
options = []
2020-01-22 20:46:18 +01:00
context = self._config_bag.context
async for path in context.find(byname=name,
byvalue=value,
bytype=type,
config_bag=self._config_bag):
2019-12-24 15:24:20 +01:00
option = TiramisuOption(path,
None,
self._config_bag)
if first:
return option
options.append(option)
return options
async def _filter(self,
opt,
subconfig,
config_bag):
2018-11-17 22:37:08 +01:00
option_bag = OptionBag()
option_bag.set_option(opt,
None,
config_bag)
2019-12-24 15:24:20 +01:00
settings = config_bag.context.cfgimpl_get_settings()
option_bag.properties = await settings.getproperties(option_bag)
2018-11-17 22:37:08 +01:00
if opt.impl_is_optiondescription():
2019-12-24 15:24:20 +01:00
await settings.validate_properties(option_bag)
return await subconfig.get_subconfig(option_bag)
await subconfig.getattr(opt.impl_getname(),
option_bag)
2019-12-24 15:24:20 +01:00
async def _walk(self,
2018-10-31 08:00:19 +01:00
option,
recursive,
type_,
group_type,
config_bag,
subconfig):
2019-12-24 15:24:20 +01:00
options = []
for opt in await option.get_children(config_bag):
2018-10-31 08:00:19 +01:00
try:
2019-12-24 15:24:20 +01:00
subsubconfig = await self._filter(opt,
subconfig,
config_bag)
2018-10-31 08:00:19 +01:00
except PropertiesOptionError:
continue
if opt.impl_is_optiondescription():
if recursive:
2019-12-24 15:24:20 +01:00
options.extend(await self._walk(opt,
recursive,
type_,
group_type,
config_bag,
subsubconfig))
2018-10-31 08:00:19 +01:00
if type_ == 'option' or (type_ == 'optiondescription' and \
group_type and opt.impl_get_group_type() != group_type):
continue
elif type_ == 'optiondescription':
continue
2019-12-24 15:24:20 +01:00
options.append(TiramisuOption(opt.impl_getpath(),
None,
self._config_bag))
return options
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def list(self,
2020-01-22 20:46:18 +01:00
type='option',
group_type=None,
recursive=False):
2018-10-07 10:55:52 +02:00
"""List options (by default list only option)"""
assert type in ('all', 'option', 'optiondescription'), _('unknown list type {}').format(type)
assert group_type is None or isinstance(group_type, groups.GroupType), \
_("unknown group_type: {0}").format(group_type)
config_bag = self._config_bag
if config_bag.properties and 'warnings' in config_bag.properties:
config_bag = config_bag.copy()
2018-11-17 23:14:47 +01:00
config_bag.remove_warnings()
option = config_bag.context.cfgimpl_get_description()
2019-12-24 15:24:20 +01:00
options = []
for opt in await self._walk(option,
recursive,
type,
group_type,
config_bag,
config_bag.context):
options.append(opt)
return options
async def _load_dict(self,
clearable="all",
remotable="minimum"):
2020-01-22 20:46:18 +01:00
self._tiramisu_dict = TiramisuDict(await self._return_config(self._config_bag.context,
self._config_bag.context._storage),
root=None,
clearable=clearable,
remotable=remotable)
2019-12-24 15:24:20 +01:00
async def dict(self,
clearable="all",
remotable="minimum",
form=[],
force=False):
"""convert config and option to tiramisu format"""
if force or self._tiramisu_dict is None:
2019-12-24 15:24:20 +01:00
await self._load_dict(clearable, remotable)
return await self._tiramisu_dict.todict(form)
2019-12-24 15:24:20 +01:00
async def updates(self,
body: List) -> Dict:
"""updates value with tiramisu format"""
if self._tiramisu_dict is None:
2019-12-24 15:24:20 +01:00
await self._load_dict()
return await self._tiramisu_dict.set_updates(body)
2017-12-13 22:15:34 +01:00
2018-10-07 10:55:52 +02:00
class _TiramisuContextConfigReset():
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def reset(self):
2018-10-07 10:55:52 +02:00
"""Remove all datas to current config (informations, values, properties, ...)"""
# Option's values
2020-01-22 20:46:18 +01:00
settings = self._config_bag.context.cfgimpl_get_settings()
connection = self._config_bag.connection
context_owner = await self._config_bag.context.cfgimpl_get_values().get_context_owner(connection)
await self._config_bag.context.cfgimpl_get_values()._p_.importation(connection, ([], [], [], []))
await self._config_bag.context.cfgimpl_get_values()._p_.setvalue(connection,
None,
2019-12-24 15:24:20 +01:00
None,
context_owner,
None,
True)
2018-10-07 10:55:52 +02:00
# Option's informations
2020-01-22 20:46:18 +01:00
await self._config_bag.context.cfgimpl_get_values()._p_.del_informations(connection)
2018-10-07 10:55:52 +02:00
# Option's properties
2020-01-22 20:46:18 +01:00
await self._config_bag.context.cfgimpl_get_settings()._p_.importation(connection, {})
2018-10-07 10:55:52 +02:00
# Option's permissives
2020-01-22 20:46:18 +01:00
await self._config_bag.context.cfgimpl_get_settings()._pp_.importation(connection, {})
2018-10-07 10:55:52 +02:00
# Remove cache
2019-12-24 15:24:20 +01:00
await self._config_bag.context.cfgimpl_reset_cache(None, None)
2018-10-07 10:55:52 +02:00
2018-04-07 20:15:19 +02:00
2019-08-03 21:03:23 +02:00
class _TiramisuContextConfig(TiramisuConfig, _TiramisuContextConfigReset):
2018-10-07 10:55:52 +02:00
"""Actions to Config"""
2020-01-22 20:46:18 +01:00
async def type(self):
"""Type a Config"""
return 'config'
2018-08-14 22:15:40 +02:00
2019-12-24 15:24:20 +01:00
async def copy(self,
session_id=None,
storage=None):
2019-08-05 22:31:56 +02:00
"""Copy current config"""
2020-01-22 20:46:18 +01:00
if storage is None:
storage = self._config_bag.context._storage
async with self._config_bag.context.getconnection() as connection:
config = await self._config_bag.context.duplicate(connection,
session_id,
storage=storage)
return await self._return_config(config,
storage)
2019-12-24 15:24:20 +01:00
async def deepcopy(self,
session_id=None,
storage=None,
metaconfig_prefix=None):
2019-08-05 22:31:56 +02:00
"""Copy current config with all parents"""
2020-01-22 20:46:18 +01:00
if storage is None:
storage = self._config_bag.context._storage
async with self._config_bag.context.getconnection() as connection:
config = await self._config_bag.context.duplicate(connection,
session_id,
storage=storage,
metaconfig_prefix=metaconfig_prefix,
deep=[])
return await self._return_config(config,
storage)
2018-09-05 20:22:16 +02:00
2019-12-24 15:24:20 +01:00
async def metaconfig(self):
2019-08-05 22:31:56 +02:00
"""Get first meta configuration (obsolete please use parents)"""
2019-12-24 15:24:20 +01:00
parent = await self.parents()
if not parent:
2019-08-05 22:31:56 +02:00
return None
2019-12-24 15:24:20 +01:00
return parent[0]
2019-08-05 22:31:56 +02:00
2019-12-24 15:24:20 +01:00
async def parents(self):
2019-08-05 22:31:56 +02:00
"""Get all parents of current config"""
2019-12-24 15:24:20 +01:00
ret = []
2019-08-05 22:31:56 +02:00
for parent in self._config_bag.context.get_parents():
2020-01-22 20:46:18 +01:00
ret.append(await self._return_config(parent,
parent._storage))
2019-12-24 15:24:20 +01:00
return ret
2018-09-05 20:22:16 +02:00
2019-12-24 15:24:20 +01:00
async def path(self):
2019-08-05 22:31:56 +02:00
"""Get path from config (all parents name)"""
2019-02-07 10:39:28 +01:00
return self._config_bag.context.cfgimpl_get_config_path()
2019-02-06 21:47:11 +01:00
2018-08-14 22:15:40 +02:00
2019-08-03 21:03:23 +02:00
class _TiramisuContextGroupConfig(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Actions to GroupConfig"""
2020-01-22 20:46:18 +01:00
async def type(self):
"""Type a Config"""
return 'groupconfig'
2018-08-14 22:15:40 +02:00
2019-12-24 15:24:20 +01:00
async def list(self):
2018-10-07 10:55:52 +02:00
"""List children's config"""
2019-12-24 15:24:20 +01:00
ret = []
2018-10-07 10:55:52 +02:00
for child in self._config_bag.context.cfgimpl_get_children():
2020-01-22 20:46:18 +01:00
ret.append(await self._return_config(child,
child._storage))
2019-12-24 15:24:20 +01:00
return ret
2018-08-14 22:15:40 +02:00
2020-01-22 20:46:18 +01:00
@connection
2019-12-24 15:24:20 +01:00
async def find(self,
name: str,
value=undefined):
"""Find an or a list of config with finding option"""
2019-12-24 15:24:20 +01:00
return await GroupConfig(await self._config_bag.context.find_group(byname=name,
byvalue=value,
config_bag=self._config_bag))
2018-10-07 10:55:52 +02:00
def __call__(self,
path: Optional[str]):
"""select a child Tiramisu config"""
spaths = path.split('.')
config = self._config_bag.context
for spath in spaths:
config = config.getconfig(spath)
2020-02-18 22:10:33 +01:00
if isinstance(config, KernelGroupConfig):
return self._return_config(config,
None)
2020-01-22 20:46:18 +01:00
return self._return_config(config,
config._storage)
2019-05-27 16:11:41 +02:00
2020-02-18 22:10:33 +01:00
2019-12-24 15:24:20 +01:00
async def copy(self,
session_id=None,
storage=None):
2020-01-22 20:46:18 +01:00
if storage is None:
storage = self._config_bag.context._storage
async with self._config_bag.context.getconnection() as connection:
config = await self._config_bag.context.duplicate(connection,
session_id,
storage=storage)
return await self._return_config(config,
storage)
2019-12-24 15:24:20 +01:00
async def deepcopy(self,
session_id=None,
storage=None,
metaconfig_prefix=None):
2020-01-22 20:46:18 +01:00
if storage is None:
2020-02-18 22:10:33 +01:00
storage = self._config_bag.context._storage
2020-01-22 20:46:18 +01:00
async with self._config_bag.context.getconnection() as connection:
config = await self._config_bag.context.duplicate(connection,
session_id,
storage=storage,
metaconfig_prefix=metaconfig_prefix,
deep=[])
return await self._return_config(config,
storage)
2019-12-24 15:24:20 +01:00
async def path(self):
2019-02-07 10:39:28 +01:00
return self._config_bag.context.cfgimpl_get_config_path()
2019-02-06 21:47:11 +01:00
2019-12-24 15:24:20 +01:00
async def get(self,
name: str) -> 'Config':
2020-01-22 20:46:18 +01:00
config = self._config_bag.context.getconfig(name)
return await self._return_config(config,
config._storage)
2018-10-07 10:55:52 +02:00
2018-10-31 18:26:20 +01:00
class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextConfigReset):
"""Actions to MixConfig"""
2020-01-22 20:46:18 +01:00
async def type(self):
"""Type a Config"""
return 'mixconfig'
2020-03-04 15:39:47 +01:00
async def new(self,
session_id,
storage=None,
type='config'):
2020-03-04 15:39:47 +01:00
"""Create and add a new config"""
config = self._config_bag.context
if storage is None:
storage = config._storage
storage_obj = await storage.get()
async with storage_obj.Connection() as connection:
new_config = await config.new_config(connection,
session_id=session_id,
storage=storage,
2020-03-16 15:38:41 +01:00
type_=type,
)
return await self._return_config(new_config,
storage)
async def load(self,
session_id,
storage=None,
type='config',
):
"""Create and add a new config"""
config = self._config_bag.context
if storage is None:
storage = config._storage
storage_obj = await storage.get()
async with storage_obj.Connection() as connection:
new_config = await config.load_config(connection,
session_id=session_id,
storage=storage,
type_=type,
)
2020-03-04 15:39:47 +01:00
return await self._return_config(new_config,
2020-03-16 15:38:41 +01:00
storage)
2020-03-04 15:39:47 +01:00
2019-12-24 15:24:20 +01:00
async def pop(self,
session_id=None,
config=None):
"""Remove config from MetaConfig"""
2019-08-03 21:03:23 +02:00
if __debug__ and None not in [session_id, config]:
raise APIError(_('cannot set session_id and config together'))
2019-12-24 15:24:20 +01:00
pop_config = await self._config_bag.context.pop_config(session_id=session_id, config=config)
2020-01-22 20:46:18 +01:00
return await self._return_config(pop_config,
pop_config._storage)
2019-12-24 15:24:20 +01:00
async def add(self,
config):
"""Add config from MetaConfig"""
2019-12-24 15:24:20 +01:00
await self._config_bag.context.add_config(config)
2018-10-31 08:00:19 +01:00
2019-12-24 15:24:20 +01:00
async def parents(self):
2019-09-02 14:08:05 +02:00
"""Get all parents of current config"""
2019-12-24 15:24:20 +01:00
ret = []
2019-09-02 14:08:05 +02:00
for parent in self._config_bag.context.get_parents():
2020-01-22 20:46:18 +01:00
ret.append(await self._return_config(parent,
parent._storage))
2019-12-24 15:24:20 +01:00
return ret
2019-09-02 14:08:05 +02:00
2018-10-31 08:00:19 +01:00
class _TiramisuContextMetaConfig(_TiramisuContextMixConfig):
2018-10-07 10:55:52 +02:00
"""Actions to MetaConfig"""
2020-01-22 20:46:18 +01:00
async def type(self):
"""Type a Config"""
return 'metaconfig'
2019-08-03 21:03:23 +02:00
class TiramisuContextCache(TiramisuConfig):
2019-12-24 15:24:20 +01:00
async def reset(self):
await self._config_bag.context.cfgimpl_reset_cache(None, None)
2019-02-10 21:14:32 +01:00
2019-12-24 15:24:20 +01:00
async def set_expiration_time(self,
2019-02-25 08:46:58 +01:00
time: int) -> None:
2019-02-24 10:36:42 +01:00
self._config_bag.expiration_time = time
2019-12-24 15:24:20 +01:00
async def get_expiration_time(self) -> int:
2019-02-25 08:46:58 +01:00
return self._config_bag.expiration_time
2019-02-10 21:14:32 +01:00
2018-08-01 08:37:58 +02:00
class TiramisuAPI(TiramisuHelp):
2018-10-07 10:55:52 +02:00
_registers = {}
2018-08-01 08:37:58 +02:00
def __init__(self,
2019-12-24 15:24:20 +01:00
config_bag,
orig_config_bags=None) -> None:
self._config_bag = config_bag
self._orig_config_bags = orig_config_bags
2018-10-07 10:55:52 +02:00
if not self._registers:
_registers(self._registers, 'TiramisuContext')
2018-08-01 08:37:58 +02:00
def __getattr__(self, subfunc: str) -> Any:
2019-12-24 15:24:20 +01:00
if subfunc == 'option':
config_bag = self._config_bag
return TiramisuDispatcherOption(config_bag,
self._orig_config_bags)
elif subfunc == 'forcepermissive':
config_bag = self._config_bag.copy()
2018-08-18 08:06:29 +02:00
config_bag.set_permissive()
2019-12-24 15:24:20 +01:00
if self._orig_config_bags is None:
orig_config_bags = []
else:
orig_config_bags = self._orig_config_bags
orig_config_bags.append(self._config_bag)
return TiramisuAPI(config_bag, orig_config_bags)
2018-08-01 08:37:58 +02:00
elif subfunc == 'unrestraint':
config_bag = self._config_bag.copy()
2018-12-24 09:30:58 +01:00
config_bag.unrestraint()
2019-12-24 15:24:20 +01:00
if self._orig_config_bags is None:
orig_config_bags = []
else:
orig_config_bags = self._orig_config_bags
orig_config_bags.append(self._config_bag)
return TiramisuAPI(config_bag, orig_config_bags)
2018-10-07 10:55:52 +02:00
elif subfunc == 'config':
config_type = self._config_bag.context.impl_type
if config_type == 'group':
config = _TiramisuContextGroupConfig
elif config_type == 'meta':
config = _TiramisuContextMetaConfig
2018-10-31 18:26:20 +01:00
elif config_type == 'mix':
config = _TiramisuContextMixConfig
2018-10-07 10:55:52 +02:00
else:
config = _TiramisuContextConfig
2019-12-24 15:24:20 +01:00
return config(self._config_bag,
self._orig_config_bags)
2018-10-07 10:55:52 +02:00
elif subfunc in self._registers:
config_bag = self._config_bag
2019-12-24 15:24:20 +01:00
# del config_bag.permissives
return self._registers[subfunc](config_bag,
self._orig_config_bags)
2018-12-24 09:30:58 +01:00
raise APIError(_('please specify a valid sub function ({})').format(subfunc))
2018-08-01 08:37:58 +02:00
2018-10-07 10:55:52 +02:00
def __dir__(self):
return list(self._registers.keys()) + ['unrestraint', 'forcepermissive', 'config']
2018-01-03 21:07:51 +01:00
2019-12-24 15:24:20 +01:00
class TiramisuDispatcherOption(TiramisuContextOption):
2018-10-07 10:55:52 +02:00
"""Select an option"""
2018-08-01 08:37:58 +02:00
def __call__(self,
path: str,
index: Optional[int]=None) -> TiramisuOption:
2018-10-07 10:55:52 +02:00
"""Select an option by path"""
2019-12-24 15:24:20 +01:00
return TiramisuOption(path,
index,
2018-10-07 10:55:52 +02:00
self._config_bag)
2017-10-22 09:48:08 +02:00
2019-12-24 15:24:20 +01:00
async def __getattr__(self,
subfunc: str) -> Any:
2019-08-24 08:14:54 +02:00
if subfunc == 'unrestraint':
config_bag = self._config_bag.copy()
config_bag.unrestraint()
return TiramisuDispatcherOption(config_bag)
2017-10-22 09:48:08 +02:00
2019-12-24 15:24:20 +01:00
@asyncinit
2018-08-14 22:15:40 +02:00
class Config(TiramisuAPI):
2018-10-08 15:56:28 +02:00
"""Root config object that enables us to handle the configuration options"""
2019-12-24 15:24:20 +01:00
async def __init__(self,
descr: OptionDescription,
session_id: str=None,
2020-01-22 20:46:18 +01:00
delete_old_session: bool=False,
2019-12-24 15:24:20 +01:00
storage=None,
display_name=None) -> None:
2020-01-22 20:46:18 +01:00
if storage is None:
storage = default_storage
storage_obj = await storage.get()
async with storage_obj.Connection() as connection:
if isinstance(descr, KernelConfig):
config = descr
else:
config = await KernelConfig(descr,
connection=connection,
session_id=session_id,
delete_old_session=delete_old_session,
storage=storage,
display_name=display_name)
settings = config.cfgimpl_get_settings()
properties = await settings.get_context_properties(connection,
config._impl_properties_cache)
permissives = await settings.get_context_permissives(connection)
2019-12-24 15:24:20 +01:00
config_bag = ConfigBag(config,
properties=properties,
permissives=permissives)
super().__init__(config_bag)
2020-01-22 20:46:18 +01:00
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
await self._config_bag.context.cfgimpl_get_values()._p_._storage.delete_session()
await self._config_bag.context.cfgimpl_get_settings()._p_._storage.delete_session()
def __del__(self):
try:
del self._config_bag.context
del self._config_bag
del self._orig_config_bags
except:
pass
2019-12-24 15:24:20 +01:00
@asyncinit
2018-08-14 22:15:40 +02:00
class MetaConfig(TiramisuAPI):
2018-10-08 15:56:28 +02:00
"""MetaConfig object that enables us to handle the sub configuration's options"""
2019-12-24 15:24:20 +01:00
async def __init__(self,
children: 'Config'=[],
session_id: Union[str, None]=None,
2020-01-22 20:46:18 +01:00
delete_old_session: bool=False,
2019-12-24 15:24:20 +01:00
optiondescription: Optional[OptionDescription]=None,
storage=None,
display_name=None) -> None:
2020-01-22 20:46:18 +01:00
if storage is None:
storage = default_storage
storage_obj = await storage.get()
async with storage_obj.Connection() as connection:
if isinstance(children, KernelMetaConfig):
config = children
else:
_children = []
for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config_bag.context)
else:
_children.append(child)
config = await KernelMetaConfig(_children,
connection=connection,
session_id=session_id,
delete_old_session=delete_old_session,
optiondescription=optiondescription,
display_name=display_name,
storage=storage)
settings = config.cfgimpl_get_settings()
properties = await settings.get_context_properties(connection,
config._impl_properties_cache)
permissives = await settings.get_context_permissives(connection)
2019-12-24 15:24:20 +01:00
config_bag = ConfigBag(config,
properties=properties,
permissives=permissives)
super().__init__(config_bag)
2020-01-22 20:46:18 +01:00
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
await self._config_bag.context.cfgimpl_get_values()._p_._storage.delete_session()
await self._config_bag.context.cfgimpl_get_settings()._p_._storage.delete_session()
2019-12-24 15:24:20 +01:00
@asyncinit
2018-10-31 18:26:20 +01:00
class MixConfig(TiramisuAPI):
2018-10-31 08:00:19 +01:00
"""MetaConfig object that enables us to handle the sub configuration's options"""
2019-12-24 15:24:20 +01:00
async def __init__(self,
optiondescription: OptionDescription,
children: List[Config],
session_id: Optional[str]=None,
2020-01-22 20:46:18 +01:00
delete_old_session: bool=False,
2019-12-24 15:24:20 +01:00
storage=None,
display_name: Callable=None) -> None:
2020-01-22 20:46:18 +01:00
if storage is None:
storage = default_storage
storage_obj = await storage.get()
async with storage_obj.Connection() as connection:
if isinstance(children, KernelMixConfig):
config = children
else:
_children = []
for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config_bag.context)
else:
_children.append(child)
config = await KernelMixConfig(optiondescription,
_children,
session_id=session_id,
delete_old_session=delete_old_session,
storage=storage,
connection=connection,
display_name=display_name)
settings = config.cfgimpl_get_settings()
properties = await settings.get_context_properties(connection,
config._impl_properties_cache)
permissives = await settings.get_context_permissives(connection)
2019-12-24 15:24:20 +01:00
config_bag = ConfigBag(config,
properties=properties,
permissives=permissives)
super().__init__(config_bag)
2020-01-22 20:46:18 +01:00
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
await self._config_bag.context.cfgimpl_get_values()._p_._storage.delete_session()
await self._config_bag.context.cfgimpl_get_settings()._p_._storage.delete_session()
2019-12-24 15:24:20 +01:00
@asyncinit
2018-08-14 22:15:40 +02:00
class GroupConfig(TiramisuAPI):
2018-10-08 15:56:28 +02:00
"""GroupConfig that enables us to access the Config"""
2019-12-24 15:24:20 +01:00
async def __init__(self,
children,
session_id: Optional[str]=None) -> None:
2019-06-05 12:33:00 +02:00
if isinstance(children, KernelGroupConfig):
config = children
else:
_children = []
for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config_bag.context)
else:
_children.append(child)
2018-08-14 22:15:40 +02:00
2019-12-24 15:24:20 +01:00
config = await KernelGroupConfig(_children,
session_id=session_id)
config_bag = ConfigBag(config,
properties=None,
permissives=None)
super().__init__(config_bag)