tiramisu/tiramisu/api.py

1063 lines
40 KiB
Python
Raw Normal View History

2017-10-22 09:48:08 +02:00
# -*- coding: utf-8 -*-
2018-01-26 07:33:47 +01:00
# Copyright (C) 2017-2018 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
2018-03-31 21:06:19 +02:00
from .error import APIError, ConfigError, SlaveError
2017-10-22 09:48:08 +02:00
from .i18n import _
from .setting import ConfigBag, owners, undefined, FORBIDDEN_SET_PROPERTIES
2017-12-13 22:15:34 +01:00
from .option import ChoiceOption
2017-11-28 22:42:30 +01:00
from time import time
from copy import deepcopy
2017-12-13 22:15:34 +01:00
TIRAMISU_VERSION = 3
try:
from .value import Multi
except:
Multi = list
2017-10-22 09:48:08 +02:00
2017-11-28 22:42:30 +01:00
COUNT_TIME = False
2018-03-24 22:37:48 +01:00
#COUNT_TIME = {}
2017-11-28 22:42:30 +01:00
2018-04-07 20:15:19 +02:00
EXCLUDE_HELP = ('help', '_get_option', '_test_slave_index')
2017-11-28 22:42:30 +01:00
def count(func):
global MOD_COUNT_TIME
2017-12-13 22:15:34 +01:00
class_name = func.__str__().split()[1].split('.')[0]
2017-11-28 22:42:30 +01:00
func_name = func.__name__
2018-04-11 16:36:15 +02:00
def wrapper(*args, **kwargs): # pragma: no cover
2017-11-28 22:42:30 +01:00
time1 = time()
ret = func(*args, **kwargs)
time2 = time()
diff = (time2 - time1) * 1000.0
2017-12-13 22:15:34 +01:00
MOD_COUNT_TIME[class_name][func_name]['max'] = max(MOD_COUNT_TIME[class_name][func_name]['max'], diff)
MOD_COUNT_TIME[class_name][func_name]['min'] = min(MOD_COUNT_TIME[class_name][func_name]['min'], diff)
MOD_COUNT_TIME[class_name][func_name]['total'] += diff
MOD_COUNT_TIME[class_name][func_name]['nb'] += 1
2017-11-28 22:42:30 +01:00
#print('%s function took %0.3f ms' % (func_name, diff))
#print(COUNT_TIME)
return ret
2018-04-11 16:36:15 +02:00
if COUNT_TIME is not False: # pragma: no cover
2017-12-13 22:15:34 +01:00
COUNT_TIME.setdefault(class_name, {})
COUNT_TIME[class_name][func_name] = {'max': 0,
'min': 1000,
'nb': 0,
'total': 0}
2017-11-28 22:42:30 +01:00
MOD_COUNT_TIME = deepcopy(COUNT_TIME)
return wrapper
return func
def display_count():
2018-04-11 16:36:15 +02:00
if COUNT_TIME is not False: # pragma: no cover
2017-11-28 22:42:30 +01:00
global MOD_COUNT_TIME
#print(MOD_COUNT_TIME)
2017-12-13 22:15:34 +01:00
print()
for class_name in MOD_COUNT_TIME:
print('>', class_name)
for func in MOD_COUNT_TIME[class_name]:
print('=>', func)
print('==> nb:', MOD_COUNT_TIME[class_name][func]['nb'])
if MOD_COUNT_TIME[class_name][func]['nb'] != 0:
print('==> min:', MOD_COUNT_TIME[class_name][func]['min'])
print('==> max:', MOD_COUNT_TIME[class_name][func]['max'])
print('==> moy:', MOD_COUNT_TIME[class_name][func]['total'] / MOD_COUNT_TIME[class_name][func]['nb'])
2017-11-28 22:42:30 +01:00
MOD_COUNT_TIME = deepcopy(COUNT_TIME)
2017-12-19 23:11:45 +01:00
2018-04-07 20:15:19 +02:00
class TiramisuHelp:
icon = '\u2937'
tmpl_help = '{0}{1} {2}: \n{0} {3}\n'
def help(self,
init=True,
space="",
root='',
_display=True,
_valid=False):
options = []
if init and isinstance(self, TiramisuAPI):
options.append(self.tmpl_help.format(space, self.icon, root + 'unrestraint', _('access to option without property restriction')))
options.append(self.tmpl_help.format(space, self.icon, root + 'forcepermissive', _('access to option without verifying permissive property')))
root = '[unrestraint.|forcepermissive.]'
modules = list(self.registers.keys())
modules.sort()
for module_name in modules:
module = self.registers[module_name]
instance_module = module(None)
if isinstance(instance_module, TiramisuDispatcher):
2018-04-11 16:36:15 +02:00
if _valid and not getdoc(module.__call__): # pragma: no cover
2018-04-07 20:15:19 +02:00
raise Exception('unknown doc for {}'.format('__call__'))
module_doc = _(getdoc(module.__call__))
module_signature = signature(module.__call__)
module_args = [str(module_signature.parameters[key]) for key in list(module_signature.parameters.keys())[1:]]
module_args = '(' + ', '.join(module_args) + ')'
options.append(self.tmpl_help.format(space, self.icon, root + module_name + module_args, module_doc))
if hasattr(module, 'subhelp'):
instance_submodule = module.subhelp(None, None, None, None, None)
options.extend(instance_submodule.help(init=False, space=space + ' ', root=root + module_name + module_args + '.'))
else:
root = root + '[config(path).]'
if isinstance(instance_module, CommonTiramisuOption):
2018-04-11 16:36:15 +02:00
if _valid and not getdoc(module): # pragma: no cover
2018-04-07 20:15:19 +02:00
raise Exception('unknown doc for {}'.format(module.__class__.__name__))
module_doc = _(getdoc(module))
options.append(self.tmpl_help.format(space, self.icon, root + module_name, module_doc))
if isinstance(instance_module, TiramisuContext):
2018-04-11 16:36:15 +02:00
if _valid and not getdoc(module): # pragma: no cover
2018-04-07 20:15:19 +02:00
raise Exception('unknown doc for {}'.format(module.__class__.__name__))
module_doc = _(getdoc(module))
options.append(self.tmpl_help.format(space, self.icon, root + module_name, module_doc))
options.extend(instance_module.help(init=False, space=space + ' ', root=root + '{}.'.format(module_name)))
funcs = dir(self)
funcs.sort()
for func_name in funcs:
if not func_name.startswith('__') and not func_name in EXCLUDE_HELP:
func = getattr(self, func_name)
if ismethod(func):
module_signature = signature(func)
module_args = list(module_signature.parameters.keys())
module_args = [str(module_signature.parameters[key]) for key in module_signature.parameters.keys()]
module_args = '(' + ', '.join(module_args) + ')'
if func_name.startswith('_'):
func_name = func_name[1:]
2018-04-11 16:36:15 +02:00
if _valid and not getdoc(func): # pragma: no cover
2018-04-07 20:15:19 +02:00
raise Exception('unknown doc for {}'.format(func.__name__))
options.append(self.tmpl_help.format(space, self.icon, root + func_name + module_args, _(getdoc(func))))
if init:
2018-04-11 16:36:15 +02:00
if _display: # pragma: no cover
2018-04-07 20:15:19 +02:00
print('\n'.join(options))
else:
return options
class CommonTiramisu(TiramisuHelp):
2018-01-01 21:32:39 +01:00
allow_optiondescription = True
2018-04-07 20:15:19 +02:00
registers = {}
2018-01-01 21:32:39 +01:00
2018-01-05 23:32:00 +01:00
def _get_option(self):
2018-01-01 21:32:39 +01:00
option = self.config_bag.option
if option is None:
2018-04-05 21:20:39 +02:00
option = self.subconfig.cfgimpl_get_description().impl_getchild(self._name,
2018-01-01 21:32:39 +01:00
self.config_bag,
self.subconfig)
self.config_bag.option = option
2018-04-05 21:13:24 +02:00
if self.config_bag.setting_properties:
self.config_bag.config.cfgimpl_get_settings().validate_properties(self._path,
2018-04-05 21:13:24 +02:00
self.index,
self.config_bag)
2018-04-06 23:51:25 +02:00
if self.index is not None:
if option.impl_is_optiondescription() or not option.impl_is_master_slaves('slave'):
raise APIError('index must be set only with a slave option')
if self.index >= self.subconfig.cfgimpl_get_length():
raise SlaveError(_('index "{}" is higher than the master length "{}" '
'for option "{}"').format(self.index,
self.subconfig.cfgimpl_get_length(),
option.impl_get_display_name()))
2018-01-01 21:32:39 +01:00
if not self.allow_optiondescription and option.impl_is_optiondescription():
raise APIError(_('option must not be an optiondescription'))
return option
class CommonTiramisuOption(CommonTiramisu):
2017-12-13 22:15:34 +01:00
allow_optiondescription = False
slave_need_index = True
def __init__(self,
2017-12-27 15:48:49 +01:00
name,
2018-04-07 20:15:19 +02:00
path=None,
index=None,
subconfig=None,
config_bag=None):
self._path = path
self.index = index
2017-12-19 23:11:45 +01:00
self.config_bag = config_bag
2018-04-05 21:20:39 +02:00
self._name = name
2017-12-27 15:48:49 +01:00
self.subconfig = subconfig
2018-04-07 20:15:19 +02:00
if config_bag is not None and self.slave_need_index:
self._test_slave_index()
2017-10-25 08:46:22 +02:00
def _test_slave_index(self):
2018-01-05 23:32:00 +01:00
option = self._get_option()
2018-03-24 22:37:48 +01:00
if not option.impl_is_optiondescription():
if self.index is None and option.impl_is_master_slaves('slave'):
raise APIError('index must be set with a slave option')
elif self.index is not None and not option.impl_is_master_slaves('slave'):
raise APIError('index must be set only with a slave option')
2017-10-22 09:48:08 +02:00
def __getattr__(self, name):
2018-04-07 20:15:19 +02:00
if not hasattr(CommonTiramisuOption, name):
raise APIError(_('unknown method {}').format(name))
2017-10-22 09:48:08 +02:00
else:
2018-04-07 20:15:19 +02:00
super().__getattribute__(name)
2017-10-22 09:48:08 +02:00
class TiramisuOptionOption(CommonTiramisuOption):
2018-04-07 20:15:19 +02:00
"""manage option"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
slave_need_index = False
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def get(self):
2018-04-07 20:15:19 +02:00
"""get Tiramisu option"""
2018-01-05 23:32:00 +01:00
return self._get_option()
@count
def _ismulti(self):
2017-10-22 09:48:08 +02:00
"""test if option could have multi value"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_is_multi()
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def _issubmulti(self):
"""test if option could have submulti value"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_is_submulti()
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
def ismasterslaves(self):
2017-10-22 09:48:08 +02:00
"""test if option is a master or a slave"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_is_master_slaves()
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def _ismaster(self):
2017-10-22 09:48:08 +02:00
"""test if option is a master"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_is_master_slaves('master')
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def _isslave(self):
2017-10-22 09:48:08 +02:00
"""test if option is a slave"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_is_master_slaves('slave')
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def doc(self):
2018-04-07 20:15:19 +02:00
"""get option document"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_get_display_name()
2017-10-25 08:46:22 +02:00
2018-04-05 21:20:39 +02:00
@count
def name(self):
2018-04-07 20:15:19 +02:00
"""get option name"""
2018-04-05 21:20:39 +02:00
self._get_option()
return self._name
def path(self):
"""get option path"""
self._get_option()
return self._path
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def _default(self):
2018-04-07 20:15:19 +02:00
"""get default value for an option (not for optiondescription)"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_getdefault()
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def _defaultmulti(self):
2018-04-07 20:15:19 +02:00
"""get default value when added a value for a multi option (not for optiondescription)"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_getdefault_multi()
2017-12-19 23:11:45 +01:00
@count
def has_dependency(self, self_is_dep=True):
2018-04-07 20:15:19 +02:00
"""test if option has dependency"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_has_dependency(self_is_dep)
2017-12-19 23:11:45 +01:00
2018-01-05 23:32:00 +01:00
@count
def _consistencies(self):
2018-04-07 20:15:19 +02:00
"""get consistencies for an option (not for optiondescription)"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
return option.get_consistencies()
@count
def _callbacks(self):
2018-04-07 20:15:19 +02:00
"""get callbacks for an option (not for optiondescription)"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
return option.impl_get_callback()
@count
def requires(self):
2018-04-07 20:15:19 +02:00
"""get requires for an option"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
return option.impl_getrequires()
def __getattr__(self, name):
if not self._get_option().impl_is_optiondescription() and name != 'get_option':
subkey = '_' + name
if subkey in dir(self):
func = getattr(self, subkey)
if callable(func):
return func
raise APIError(_('{} is unknown').format(name))
def isoptiondescription(self):
2018-04-07 20:15:19 +02:00
"""test if option is an optiondescription"""
2018-01-05 23:32:00 +01:00
return self._get_option().impl_is_optiondescription()
class TiramisuOptionOwner(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's owner"""
2017-11-12 20:11:56 +01:00
def __init__(self,
2017-12-27 15:48:49 +01:00
name,
2018-04-07 20:15:19 +02:00
path=None,
index=None,
subconfig=None,
config_bag=None):
2017-11-20 17:01:36 +01:00
2018-03-31 21:06:19 +02:00
super().__init__(name,
path,
index,
subconfig,
config_bag)
2018-04-07 20:15:19 +02:00
if config_bag:
self.values = self.config_bag.config.cfgimpl_get_values()
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2017-10-22 09:48:08 +02:00
def get(self):
"""get owner for a specified option"""
2018-03-31 21:06:19 +02:00
option = self._get_option()
return self.values.getowner(self._path,
2017-12-19 23:11:45 +01:00
self.index,
self.config_bag)
2017-11-28 22:42:30 +01:00
@count
def isdefault(self):
2017-10-22 09:48:08 +02:00
"""is option has defaut value"""
2018-01-05 23:32:00 +01:00
self._get_option()
return self.values.is_default_owner(self._path,
2017-12-19 23:11:45 +01:00
self.index,
self.config_bag)
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
def set(self, owner):
2017-10-22 09:48:08 +02:00
"""get owner for a specified option"""
2018-01-05 23:32:00 +01:00
self._get_option()
2017-12-13 22:15:34 +01:00
if TIRAMISU_VERSION == 2:
if owner in ['default', 'forced', 'meta']:
raise ConfigError()
2017-10-22 09:48:08 +02:00
try:
obj_owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
obj_owner = getattr(owners, owner)
self.values.setowner(self._path,
2017-12-19 23:11:45 +01:00
self.index,
obj_owner,
self.config_bag)
2017-10-22 09:48:08 +02:00
class TiramisuOptionProperty(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's property"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
slave_need_index = False
2017-11-12 20:11:56 +01:00
def __init__(self,
2017-12-27 15:48:49 +01:00
name,
2018-04-07 20:15:19 +02:00
path=None,
index=None,
subconfig=None,
config_bag=None):
2018-03-31 21:06:19 +02:00
super().__init__(name,
path,
index,
subconfig,
config_bag)
2018-04-07 20:15:19 +02:00
if config_bag:
self.settings = config_bag.config.cfgimpl_get_settings()
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2018-01-26 07:33:47 +01:00
def get(self, apply_requires=True):
2018-04-07 20:15:19 +02:00
"""get properties for an option"""
2018-01-05 23:32:00 +01:00
self._get_option()
2018-01-26 07:33:47 +01:00
if apply_requires:
self._test_slave_index()
properties = self.settings.getproperties(self._path,
2017-12-19 23:11:45 +01:00
self.index,
2018-01-26 07:33:47 +01:00
self.config_bag,
apply_requires)
2017-12-13 22:15:34 +01:00
if TIRAMISU_VERSION == 2:
properties = properties.get()
return set(properties)
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
def add(self, prop):
2018-04-07 20:15:19 +02:00
"""add new property for an option"""
2018-01-05 23:32:00 +01:00
self._get_option()
if prop in FORBIDDEN_SET_PROPERTIES:
raise ConfigError(_('cannot add this property: "{0}"').format(
' '.join(prop)))
props = self.settings.getproperties(self._path,
None,
self.config_bag,
apply_requires=False)
self.settings.setproperties(self._path,
props | {prop},
self.config_bag)
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
def pop(self, prop):
2018-04-07 20:15:19 +02:00
"""remove new property for an option"""
2018-01-05 23:32:00 +01:00
self._get_option()
props = self.settings.getproperties(self._path,
self.index,
self.config_bag,
apply_requires=False)
self.settings.setproperties(self._path,
props - {prop},
self.config_bag)
2017-12-13 22:15:34 +01:00
@count
def reset(self):
2018-04-07 20:15:19 +02:00
"""reset all personalised properties"""
2018-01-05 23:32:00 +01:00
self._get_option()
2018-04-09 21:37:49 +02:00
self.settings.reset(self.config_bag.option,
self._path,
2018-04-09 21:37:49 +02:00
self.config_bag)
2017-10-22 09:48:08 +02:00
class TiramisuOptionPermissive(CommonTiramisuOption):
"""manager option's property"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
slave_need_index = False
2017-11-12 20:11:56 +01:00
def __init__(self,
2017-12-28 11:47:29 +01:00
name,
2018-04-07 20:15:19 +02:00
path=None,
index=None,
subconfig=None,
config_bag=None):
2018-03-31 21:06:19 +02:00
super().__init__(name,
path,
index,
subconfig,
config_bag)
2018-04-07 20:15:19 +02:00
if config_bag:
self.settings = config_bag.config.cfgimpl_get_settings()
2017-12-13 22:15:34 +01:00
@count
def get(self):
2018-04-07 20:15:19 +02:00
"""get permissives value"""
2017-12-13 22:15:34 +01:00
if TIRAMISU_VERSION == 2:
2018-02-21 21:08:40 +01:00
args = [self.setting_properties, self._path]
2017-12-13 22:15:34 +01:00
else:
args = [self._get_option(), self._path]
2017-12-13 22:15:34 +01:00
return self.settings.getpermissive(*args)
2017-12-13 22:15:34 +01:00
@count
2017-12-28 11:47:29 +01:00
def set(self, permissives):
2018-04-07 20:15:19 +02:00
"""set permissives value"""
2017-12-13 22:15:34 +01:00
if TIRAMISU_VERSION == 2:
2018-03-12 11:58:49 +01:00
permissives = tuple(permissives)
2018-02-21 21:08:40 +01:00
path = self._path
2018-03-12 11:58:49 +01:00
opt = self._opt
self.settings.setpermissive(opt=opt,
path=path,
2018-04-09 21:37:49 +02:00
config_bag=self.config_bag,
2018-03-12 11:58:49 +01:00
permissive=permissives)
2018-02-21 21:08:40 +01:00
else:
path = self._path
2018-03-12 11:58:49 +01:00
opt = self._get_option()
self.settings.setpermissive(opt=opt,
path=path,
2018-04-09 21:37:49 +02:00
config_bag=self.config_bag,
2018-03-12 11:58:49 +01:00
permissives=permissives)
2017-12-13 22:15:34 +01:00
@count
2018-04-11 16:36:15 +02:00
def reset(self):
2018-04-07 20:15:19 +02:00
"""reset all personalised permissive"""
2018-04-11 16:36:15 +02:00
self.set(frozenset())
2017-12-13 22:15:34 +01:00
class TiramisuOptionInformation(CommonTiramisuOption):
2018-04-07 20:15:19 +02:00
"""manage option informations"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
2018-01-26 07:33:47 +01:00
slave_need_index = False
2017-12-13 22:15:34 +01:00
@count
def get(self, name, default=undefined):
2018-04-07 20:15:19 +02:00
"""get information for a key name"""
2018-01-05 23:32:00 +01:00
option = self._get_option()
2017-12-27 15:48:49 +01:00
return option.impl_get_information(name, default)
2017-12-13 22:15:34 +01:00
2018-04-07 20:15:19 +02:00
@count
def set(self, name, value):
"""set information for a key name"""
self.config_bag.config.impl_set_information(name, value)
@count
def reset(self, name):
"""remove information for a key name"""
self.config_bag.config.impl_del_information(name)
2017-12-13 22:15:34 +01:00
class TiramisuOptionValue(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's value"""
slave_need_index = False
2017-10-22 09:48:08 +02:00
2017-11-28 22:42:30 +01:00
@count
2017-10-22 09:48:08 +02:00
def get(self):
2018-04-07 20:15:19 +02:00
"""get option's value"""
2018-01-05 23:32:00 +01:00
self._get_option()
self._test_slave_index()
2017-12-19 23:11:45 +01:00
settings = self.config_bag.config.cfgimpl_get_settings()
2018-04-05 21:20:39 +02:00
value = self.subconfig.getattr(self._name,
2017-12-27 15:48:49 +01:00
self.index,
self.config_bag)
if isinstance(value, Multi):
value = list(value)
return value
2017-10-22 09:48:08 +02:00
2017-11-28 22:42:30 +01:00
@count
def set(self, value):
"""set a value for a specified option"""
2018-01-05 23:32:00 +01:00
self._get_option()
self._test_slave_index()
2017-12-19 23:11:45 +01:00
values = self.config_bag.config.cfgimpl_get_values()
if isinstance(value, list):
while undefined in value:
idx = value.index(undefined)
value[idx] = values.getdefaultvalue(self._path,
2017-12-19 23:11:45 +01:00
idx,
self.config_bag)
else:
if value == undefined:
value = values.getdefaultvalue(self._path,
2017-12-19 23:11:45 +01:00
self.index,
self.config_bag)
2018-04-05 21:20:39 +02:00
self.subconfig.setattr(self._name,
2017-12-27 15:48:49 +01:00
self.index,
value,
self.config_bag)
2017-10-22 09:48:08 +02:00
2017-11-28 22:42:30 +01:00
@count
2018-01-01 21:32:39 +01:00
def _pop(self, index):
2018-04-07 20:15:19 +02:00
"""pop value for a master option (only for master option)"""
2018-01-05 23:32:00 +01:00
self._get_option()
self.config_bag.config.delattr(self._path,
2017-12-19 23:11:45 +01:00
index,
self.config_bag)
2017-11-12 20:11:56 +01:00
2017-12-13 22:15:34 +01:00
@count
def reset(self):
2017-10-22 09:48:08 +02:00
"""reset value for a value"""
2018-01-05 23:32:00 +01:00
self._get_option()
self._test_slave_index()
self.config_bag.config.delattr(self._path,
2017-12-19 23:11:45 +01:00
self.index,
self.config_bag)
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2018-01-01 21:32:39 +01:00
def _len(self):
2018-04-07 20:15:19 +02:00
"""length of slave option (only for slave option)"""
2018-01-05 23:32:00 +01:00
self._get_option()
subconfig_path = self._path.rsplit('.', 1)[0]
2018-03-31 21:06:19 +02:00
if self.config_bag.setting_properties is not None:
self.config_bag.config.cfgimpl_get_settings().validate_properties(self._path,
2018-03-31 21:06:19 +02:00
self.index,
self.config_bag)
2018-01-26 07:33:47 +01:00
config_bag = self.config_bag.copy('nooption')
subconfig = config_bag.config.getattr(subconfig_path,
None,
config_bag)
return subconfig.cfgimpl_get_length()
2017-12-13 22:15:34 +01:00
def __getattr__(self, name):
2018-01-05 23:32:00 +01:00
if name == 'list' and isinstance(self._get_option(), ChoiceOption):
2018-01-01 21:32:39 +01:00
return self._list
2018-01-05 23:32:00 +01:00
elif name == 'pop' and self._get_option().impl_is_master_slaves('master'):
2018-01-01 21:32:39 +01:00
return self._pop
2018-01-05 23:32:00 +01:00
elif name == 'len' and self._get_option().impl_is_master_slaves('slave'):
2018-01-01 21:32:39 +01:00
return self._len
2017-12-19 23:11:45 +01:00
raise APIError(_('{} is unknown').format(name))
2017-12-13 22:15:34 +01:00
@count
def _list(self):
2018-04-07 20:15:19 +02:00
"""all values available for an option (only for choiceoption)"""
2017-12-19 23:11:45 +01:00
return self.config_bag.option.impl_get_values(self.config_bag)
2017-12-13 22:15:34 +01:00
def registers(registers, prefix):
for module_name in globals().keys():
if module_name != prefix and module_name.startswith(prefix):
module = globals()[module_name]
func_name = module_name[len(prefix):].lower()
registers[func_name] = module
2017-10-22 09:48:08 +02:00
2018-01-01 21:32:39 +01:00
class TiramisuOption(CommonTiramisu):
2017-11-12 20:11:56 +01:00
def __init__(self,
2017-12-27 15:48:49 +01:00
name,
2018-04-07 20:15:19 +02:00
path=None,
index=None,
subconfig=None,
config_bag=None):
2017-11-12 20:11:56 +01:00
2018-04-05 21:20:39 +02:00
self._name = name
2017-12-27 15:48:49 +01:00
self.subconfig = subconfig
self._path = path
self.index = index
2017-12-19 23:11:45 +01:00
self.config_bag = config_bag
2017-10-22 09:48:08 +02:00
self.registers = {}
2017-12-13 22:15:34 +01:00
registers(self.registers, self.__class__.__name__)
2017-10-22 09:48:08 +02:00
def __getattr__(self, subfunc):
if subfunc in self.registers:
2018-04-05 21:20:39 +02:00
return self.registers[subfunc](self._name,
self._path,
self.index,
2017-12-27 15:48:49 +01:00
self.subconfig,
2017-12-19 23:11:45 +01:00
self.config_bag)
2018-01-05 23:32:00 +01:00
elif subfunc == 'make_dict' and self._get_option().impl_is_optiondescription():
2018-01-01 21:32:39 +01:00
return self._make_dict
2018-04-10 12:33:51 +02:00
elif subfunc == 'find' and self._get_option().impl_is_optiondescription():
return self._find
2018-01-05 23:32:00 +01:00
elif subfunc == 'list' and self._get_option().impl_is_optiondescription():
2018-01-01 21:32:39 +01:00
return self._list
2018-01-05 23:32:00 +01:00
elif subfunc == 'group_type' and self._get_option().impl_is_optiondescription():
2018-01-01 21:32:39 +01:00
return self._group_type
else:
2017-12-13 22:15:34 +01:00
raise APIError(_('please specify a valid sub function ({})').format(subfunc))
@count
2018-01-01 21:32:39 +01:00
def _make_dict(self,
flatten=False,
withvalue=undefined,
withoption=None,
fullpath=False):
2018-04-07 20:15:19 +02:00
"""return dict with path as key and value for an optiondescription (only for optiondescription)"""
return self.config_bag.config.getattr(self._path,
2017-12-19 23:11:45 +01:00
None,
self.config_bag).make_dict(config_bag=self.config_bag,
flatten=flatten,
fullpath=fullpath,
withoption=withoption,
withvalue=withvalue)
2018-04-10 12:33:51 +02:00
def _find(self,
name,
value=undefined,
2018-04-12 23:04:33 +02:00
type=None,
2018-04-10 12:33:51 +02:00
first=False):
"""find an option by name (only for optiondescription)"""
if not first:
ret = []
for path in self.config_bag.config.find(byname=name,
byvalue=value,
2018-04-12 23:04:33 +02:00
bytype=type,
_subpath=self._path,
2018-04-10 12:33:51 +02:00
config_bag=self.config_bag):
config_bag = self.config_bag.copy('nooption')
subconfig, name = config_bag.config.cfgimpl_get_home_by_path(path,
config_bag)
t_option = TiramisuOption(name,
path,
None, # index for a slave ?
subconfig,
config_bag)
if first:
return t_option
ret.append(t_option)
return ret
2018-01-01 21:32:39 +01:00
@count
2018-04-07 20:15:19 +02:00
def _group_type(self):
"""get type for an optiondescription (only for optiondescription)"""
2018-01-05 23:32:00 +01:00
return self._get_option().impl_get_group_type()
2018-01-01 21:32:39 +01:00
@count
def _list(self,
type='all',
group_type=None):
2018-04-07 20:15:19 +02:00
"""list options in an optiondescription (only for optiondescription)"""
2018-01-01 21:32:39 +01:00
if type == 'optiondescription':
return self.config_bag.config.getattr(self._path,
2018-01-01 21:32:39 +01:00
None,
self.config_bag
).iter_groups(self.config_bag, group_type)
elif type == 'all':
return self.config_bag.config.getattr(self._path,
2018-01-01 21:32:39 +01:00
None,
self.config_bag
).cfgimpl_get_children(self.config_bag)
else:
raise APIError(_('unknown list type {}').format(type))
2018-04-07 20:15:19 +02:00
class TiramisuContext(TiramisuHelp):
2017-11-23 16:56:14 +01:00
def __init__(self,
2017-12-19 23:11:45 +01:00
config_bag):
self.config_bag = config_bag
2018-04-07 20:15:19 +02:00
self.registers = {}
registers(self.registers, self.__class__.__name__)
2017-12-13 22:15:34 +01:00
class TiramisuContextInformation(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage configuration informations"""
2017-12-13 22:15:34 +01:00
@count
def get(self, name, default=undefined):
2018-04-07 20:15:19 +02:00
"""get information for a key name"""
2017-12-19 23:11:45 +01:00
return self.config_bag.config.impl_get_information(name, default)
2017-12-13 22:15:34 +01:00
@count
def set(self, name, value):
2018-04-07 20:15:19 +02:00
"""set information for a key name"""
2017-12-19 23:11:45 +01:00
self.config_bag.config.impl_set_information(name, value)
2017-12-13 22:15:34 +01:00
@count
def reset(self, name):
2018-04-07 20:15:19 +02:00
"""remove information for a key name"""
2017-12-19 23:11:45 +01:00
self.config_bag.config.impl_del_information(name)
2017-12-13 22:15:34 +01:00
class TiramisuContextValue(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manager value"""
2017-12-13 22:15:34 +01:00
@count
def mandatory_warnings(self):
2018-04-07 20:15:19 +02:00
"""return path of options with mandatory property without any value"""
2017-12-19 23:11:45 +01:00
return self.config_bag.config.cfgimpl_get_values().mandatory_warnings(self.config_bag)
2017-12-13 22:15:34 +01:00
2018-01-03 21:07:51 +01:00
def set(self,
path,
value,
index=None,
only_config=undefined,
force_default=undefined,
force_default_if_same=undefined,
force_dont_change_value=undefined):
2018-04-07 20:15:19 +02:00
"""set values for a GroupConfig or a MetaConfig"""
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
return self.config_bag.config.set_value(path,
index,
value,
self.config_bag,
**kwargs)
@count
2018-01-03 21:07:51 +01:00
def reset(self,
path):
2018-04-07 20:15:19 +02:00
"""reset value for a GroupConfig or a MetaConfig"""
2018-01-03 21:07:51 +01:00
self.config_bag.config.reset(path, self.config_bag)
@count
def exportation(self):
2018-04-07 20:15:19 +02:00
"""export all values"""
2018-04-04 16:47:28 +02:00
return self.config_bag.config.cfgimpl_get_values()._p_.exportation()
@count
def importation(self, values):
2018-04-07 20:15:19 +02:00
"""import values"""
return self.config_bag.config.cfgimpl_get_values()._p_.importation(values)
2017-12-13 22:15:34 +01:00
class TiramisuContextOwner(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manager value"""
2017-12-13 22:15:34 +01:00
@count
def get(self):
2018-04-07 20:15:19 +02:00
"""get default owner"""
2017-12-19 23:11:45 +01:00
return self.config_bag.config.cfgimpl_get_settings().getowner()
2017-12-23 12:29:45 +01:00
@count
def set(self, owner):
2018-04-07 20:15:19 +02:00
"""set default 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)
self.config_bag.config.cfgimpl_get_settings().setowner(obj_owner)
2017-12-13 22:15:34 +01:00
class TiramisuContextProperty(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage configuration properties"""
2017-12-13 22:15:34 +01:00
@count
def read_only(self):
2018-04-07 20:15:19 +02:00
"""set configuration to read only mode"""
2017-12-19 23:11:45 +01:00
settings = self.config_bag.config.cfgimpl_get_settings()
settings.read_only()
self.config_bag.setting_properties = settings.get_context_properties()
2017-12-13 22:15:34 +01:00
@count
def read_write(self):
2018-04-07 20:15:19 +02:00
"""set configuration to read and write mode"""
2017-12-19 23:11:45 +01:00
settings = self.config_bag.config.cfgimpl_get_settings()
settings.read_write()
2017-12-13 22:15:34 +01:00
# #FIXME ?
settings.set_context_permissive(frozenset(['hidden']))
2017-12-19 23:11:45 +01:00
self.config_bag.setting_properties = settings.get_context_properties()
2017-12-13 22:15:34 +01:00
#/FIXME ?
@count
def add(self, prop):
2018-04-07 20:15:19 +02:00
"""add a configuration property"""
2017-12-13 22:15:34 +01:00
props = self.get()
props.add(prop)
2017-12-28 11:47:29 +01:00
self.set(frozenset(props))
2017-12-19 23:11:45 +01:00
self.config_bag.setting_properties = self.config_bag.config.cfgimpl_get_settings().get_context_properties()
2017-12-13 22:15:34 +01:00
@count
def pop(self, prop):
2018-04-07 20:15:19 +02:00
"""remove a configuration property"""
2017-12-13 22:15:34 +01:00
props = self.get()
2018-04-03 14:27:20 +02:00
if prop in props:
props.remove(prop)
self.set(frozenset(props))
self.config_bag.setting_properties = self.config_bag.config.cfgimpl_get_settings().get_context_properties()
2017-12-13 22:15:34 +01:00
@count
def get(self):
2018-04-07 20:15:19 +02:00
"""get all configuration properties"""
2017-12-19 23:11:45 +01:00
return set(self.config_bag.setting_properties)
2017-12-13 22:15:34 +01:00
@count
def set(self, props):
2018-04-07 20:15:19 +02:00
"""personalise configuration properties"""
2017-12-28 11:47:29 +01:00
self.config_bag.config.cfgimpl_get_settings().set_context_properties(props)
2017-12-19 23:11:45 +01:00
self.config_bag.setting_properties = self.config_bag.config.cfgimpl_get_settings().get_context_properties()
2017-12-13 22:15:34 +01:00
@count
2017-12-23 20:21:07 +01:00
def reset(self):
2018-04-07 20:15:19 +02:00
"""remove configuration properties"""
2018-04-09 21:37:49 +02:00
self.config_bag.config.cfgimpl_get_settings().reset(None,
None,
None)
2017-12-23 20:21:07 +01:00
@count
def exportation(self):
2018-04-07 20:15:19 +02:00
"""export configuration properties"""
return self.config_bag.config.cfgimpl_get_settings()._p_.exportation()
@count
def importation(self, properties):
2018-04-07 20:15:19 +02:00
"""import configuration properties"""
return self.config_bag.config.cfgimpl_get_settings()._p_.importation(properties)
2017-12-13 22:15:34 +01:00
class TiramisuContextPermissive(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage configuration permissives"""
@count
def get(self):
"""get configuration permissives"""
2018-04-11 08:40:59 +02:00
return self.config_bag.config.cfgimpl_get_settings().get_context_permissive()
2017-12-13 22:15:34 +01:00
@count
def set(self, permissives):
2018-04-07 20:15:19 +02:00
"""set configuration permissives"""
2017-12-19 23:11:45 +01:00
self.config_bag.config.cfgimpl_get_settings().set_context_permissive(permissives)
2017-12-13 22:15:34 +01:00
@count
def exportation(self):
2018-04-07 20:15:19 +02:00
"""export configuration permissives"""
return self.config_bag.config.cfgimpl_get_settings()._pp_.exportation()
@count
def importation(self, permissives):
2018-04-07 20:15:19 +02:00
"""import configuration permissives"""
2018-04-11 08:40:59 +02:00
self.config_bag.config.cfgimpl_get_settings()._pp_.importation(permissives)
2017-12-13 22:15:34 +01:00
2017-11-23 16:56:14 +01:00
class TiramisuContextOption(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage option"""
2017-11-23 16:56:14 +01:00
2017-12-13 22:15:34 +01:00
@count
2017-11-23 16:56:14 +01:00
def find(self,
name,
2018-04-10 12:33:51 +02:00
value=undefined,
2018-04-12 23:04:33 +02:00
type=None,
2018-04-07 20:15:19 +02:00
first=False):
"""find an option by name"""
2018-04-10 12:33:51 +02:00
if not first:
ret = []
for path in self.config_bag.config.find(byname=name,
byvalue=value,
2018-04-12 23:04:33 +02:00
bytype=type,
#_subpath=self._path,
2018-04-10 12:33:51 +02:00
config_bag=self.config_bag):
config_bag = self.config_bag.copy('nooption')
subconfig, name = config_bag.config.cfgimpl_get_home_by_path(path,
config_bag)
t_option = TiramisuOption(name,
path,
None, # index for a slave ?
subconfig,
config_bag)
if first:
return t_option
ret.append(t_option)
return ret
2017-11-23 16:56:14 +01:00
2018-04-07 20:15:19 +02:00
#@count
#def get(self, path):
# """"""
# config_bag = self.config_bag.copy()
# config_bag.validate = False
# config_bag.force_unrestraint = True
# config_bag.setting_properties = None
# return self.config_bag.config.unwrap_from_path(path,
# config_bag)
2017-11-23 16:56:14 +01:00
2017-12-13 22:15:34 +01:00
@count
def make_dict(self,
flatten=False,
withvalue=undefined,
withoption=None,
fullpath=False):
2018-04-07 20:15:19 +02:00
"""return dict with path as key and value"""
2017-12-19 23:11:45 +01:00
return self.config_bag.config.make_dict(self.config_bag,
flatten=flatten,
fullpath=fullpath,
withoption=withoption,
withvalue=withvalue)
2017-12-13 22:15:34 +01:00
2018-01-01 21:32:39 +01:00
@count
def list(self,
type='all',
group_type=None,
recursive=False):
2018-04-07 20:15:19 +02:00
"""list content of an optiondescription"""
2018-01-01 21:32:39 +01:00
if type == 'optiondescription':
2018-04-04 22:00:10 +02:00
if recursive:
raise APIError(_('not implemented yet'))
else:
return self.config_bag.config.iter_groups(self.config_bag, group_type)
2018-01-01 21:32:39 +01:00
elif type == 'all':
2018-04-04 22:00:10 +02:00
if group_type:
raise APIError(_('not implemented yet'))
if recursive:
if not self.config_bag.force_unrestraint:
raise APIError(_('not implemented yet'))
else:
return self.config_bag.config.cfgimpl_get_description()._cache_paths[1]
else:
return self.config_bag.config.cfgimpl_get_children(self.config_bag)
2018-01-01 21:32:39 +01:00
else:
raise APIError(_('unknown list type {}').format(type))
2017-12-13 22:15:34 +01:00
2018-01-03 21:07:51 +01:00
class TiramisuContextConfig(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""configuration methods"""
def find(self,
name,
2018-04-10 12:33:51 +02:00
value=undefined,
2018-04-07 20:15:19 +02:00
first=False):
"""find a path from option name and optionnaly a value to MetaConfig or GroupConfig"""
if first:
return self.config_bag.config.find_firsts(byname=name,
2018-04-10 12:33:51 +02:00
byvalue=value,
2018-04-07 20:15:19 +02:00
config_bag=self.config_bag)
else:
raise APIError('not implemented yet')
2018-01-03 21:07:51 +01:00
2018-04-07 20:15:19 +02:00
class TiramisuDispatcher:
pass
2018-01-03 21:07:51 +01:00
2018-04-07 20:15:19 +02:00
class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
2018-01-03 21:07:51 +01:00
def __call__(self, path):
2018-04-07 20:15:19 +02:00
"""select a child Tiramisu configuration (only with MetaConfig or GroupConfig)"""
2018-01-03 21:07:51 +01:00
config = self.config_bag.config
2018-03-19 08:33:53 +01:00
if path is None:
return TiramisuAPI(config,
force_permissive=self.config_bag.force_permissive,
force_unrestraint=self.config_bag.force_unrestraint)
spaths = path.split('.')
2018-01-03 21:07:51 +01:00
for spath in spaths:
config = config.getconfig(spath)
return TiramisuAPI(config,
force_permissive=self.config_bag.force_permissive,
force_unrestraint=self.config_bag.force_unrestraint)
2018-04-07 20:15:19 +02:00
class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
subhelp = TiramisuOption
2017-11-23 16:56:14 +01:00
def __call__(self, path, index=None):
2018-04-07 20:15:19 +02:00
"""select a option (index only for slave option)"""
2017-12-19 23:11:45 +01:00
config_bag = self.config_bag.copy()
validate = not config_bag.force_unrestraint
if not validate:
config_bag.setting_properties = None
2017-12-27 15:48:49 +01:00
subconfig, name = config_bag.config.cfgimpl_get_home_by_path(path,
config_bag)
return TiramisuOption(name,
path,
index,
2017-12-27 15:48:49 +01:00
subconfig,
2017-12-19 23:11:45 +01:00
config_bag)
2018-04-07 20:15:19 +02:00
class TiramisuAPI(TiramisuHelp):
2017-11-23 16:56:14 +01:00
def __init__(self,
config,
force_permissive=False,
force_unrestraint=False):
self._config = config
self.force_permissive = force_permissive
self.force_unrestraint = force_unrestraint
2017-12-13 22:15:34 +01:00
self.registers = {}
registers(self.registers, 'TiramisuContext')
registers(self.registers, 'TiramisuDispatcher')
2017-11-23 16:56:14 +01:00
def __getattr__(self, subfunc):
if subfunc == 'forcepermissive':
2017-12-19 23:11:45 +01:00
return TiramisuAPI(config=self._config,
2017-11-12 20:11:56 +01:00
force_permissive=True,
force_unrestraint=self.force_unrestraint)
2018-01-03 21:07:51 +01:00
elif subfunc == 'unrestraint':
2017-12-19 23:11:45 +01:00
return TiramisuAPI(config=self._config,
2017-12-13 22:15:34 +01:00
force_permissive=self.force_permissive,
force_unrestraint=True)
elif subfunc in self.registers:
2017-12-19 23:11:45 +01:00
config_bag = ConfigBag(config=self._config,
force_permissive=self.force_permissive,
force_unrestraint=self.force_unrestraint)
return self.registers[subfunc](config_bag)
2017-10-22 09:48:08 +02:00
else:
2017-11-23 16:56:14 +01:00
raise APIError(_('please specify a valid sub function ({})').format(subfunc))
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
@count
2017-10-22 09:48:08 +02:00
def getapi(config):
"""instanciate TiramisuAPI
:param config: Config object
:type descr: an instance of ``config.Config``
"""
return TiramisuAPI(config)