tiramisu/tiramisu/api.py

910 lines
32 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/>.
# ____________________________________________________________
from inspect import ismethod, getdoc
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
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__
def wrapper(*args, **kwargs):
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
if COUNT_TIME is not False:
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():
if COUNT_TIME is not False:
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-01-01 21:32:39 +01:00
class CommonTiramisu(object):
allow_optiondescription = True
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,
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-10-22 09:48:08 +02:00
icon = '\u2937'
tmpl_help = u' {} {}: {}'
2017-10-25 08:46:22 +02:00
allow_unrestraint = False
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,
path,
index,
2017-12-27 15:48:49 +01:00
subconfig,
2017-12-19 23:11:45 +01:00
config_bag):
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
if self.slave_need_index:
self._test_slave_index()
2018-03-31 21:06:19 +02:00
if not self.allow_unrestraint and self.config_bag.force_unrestraint:
2017-12-19 23:11:45 +01:00
self._unrestraint_not_allowed(self.config_bag.force_unrestraint)
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-25 08:46:22 +02:00
def _unrestraint_not_allowed(self, force_unrestraint):
2018-03-31 21:06:19 +02:00
name = self.__class__.__name__[14:].lower()
raise APIError(_('{} cannot be unrestraint').format(name))
2017-10-22 09:48:08 +02:00
def __getattr__(self, name):
if name == 'help':
return self._help()
else:
if not hasattr(CommonTiramisuOption, name):
2017-10-22 09:48:08 +02:00
raise APIError(_('unknown method {}').format(name))
else:
2018-03-31 21:06:19 +02:00
super().__getattribute__(name)
2017-10-22 09:48:08 +02:00
def _help(self):
txt = []
for func_name in dir(self):
if not func_name.startswith('_'):
func = getattr(self, func_name)
if ismethod(func):
txt.append(self.tmpl_help.format(self.icon, func_name, getdoc(func)))
return '\n'.join(txt)
class TiramisuOptionOption(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""get information from an option"""
2017-10-25 08:46:22 +02:00
allow_unrestraint = True
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):
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):
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):
self._get_option()
return self._name
2017-12-13 22:15:34 +01:00
@count
2018-01-05 23:32:00 +01:00
def _default(self):
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):
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-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):
option = self._get_option()
return option.get_consistencies()
@count
def _callbacks(self):
option = self._get_option()
return option.impl_get_callback()
@count
def requires(self):
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):
return self._get_option().impl_is_optiondescription()
class TiramisuOptionOwner(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's owner"""
2018-04-03 14:24:35 +02:00
allow_unrestraint = True
2017-10-22 09:48:08 +02:00
2017-11-12 20:11:56 +01:00
def __init__(self,
2017-12-27 15:48:49 +01:00
name,
2017-11-12 20:11:56 +01:00
path,
index,
2017-12-27 15:48:49 +01:00
subconfig,
2017-12-19 23:11:45 +01:00
config_bag):
2017-11-20 17:01:36 +01:00
2018-03-31 21:06:19 +02:00
super().__init__(name,
path,
index,
subconfig,
config_bag)
2017-12-19 23:11:45 +01:00
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()
2017-12-19 23:11:45 +01:00
return self.values.getowner(self.path,
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()
2017-12-19 23:11:45 +01:00
return self.values.is_default_owner(self.path,
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)
2017-12-19 23:11:45 +01:00
self.values.setowner(self.path,
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
2017-12-19 23:11:45 +01:00
allow_unrestraint = 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,
2017-11-12 20:11:56 +01:00
path,
index,
2017-12-27 15:48:49 +01:00
subconfig,
2017-12-19 23:11:45 +01:00
config_bag):
2018-03-31 21:06:19 +02:00
super().__init__(name,
path,
index,
subconfig,
config_bag)
2017-12-19 23:11:45 +01:00
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-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()
2017-12-19 23:11:45 +01:00
properties = self.settings.getproperties(self.path,
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-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-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):
2017-10-22 09:48:08 +02:00
"""reset all personalised properties
"""
2018-01-05 23:32:00 +01:00
self._get_option()
2017-12-23 20:21:07 +01:00
self.settings.reset(opt=self.config_bag.option,
path=self.path)
2017-10-22 09:48:08 +02:00
class TiramisuOptionPermissive(CommonTiramisuOption):
"""manager option's property"""
allow_unrestraint = True
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,
2017-11-12 20:11:56 +01:00
path,
index,
2017-12-28 11:47:29 +01:00
subconfig,
2017-12-19 23:11:45 +01:00
config_bag):
2018-03-31 21:06:19 +02:00
super().__init__(name,
path,
index,
subconfig,
config_bag)
2017-12-19 23:11:45 +01:00
self.settings = config_bag.config.cfgimpl_get_settings()
2017-12-13 22:15:34 +01:00
@count
def get(self):
"""get permissive value for a specified path"""
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:
2018-01-05 23:32:00 +01:00
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):
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,
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,
permissives=permissives)
2017-12-13 22:15:34 +01:00
@count
2017-11-20 17:01:36 +01:00
def reset(self, path):
"""reset all personalised permissive
"""
self.set(tuple())
2017-12-13 22:15:34 +01:00
class TiramisuOptionInformation(CommonTiramisuOption):
2018-01-05 23:32:00 +01:00
allow_unrestraint = True
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-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
class TiramisuOptionValue(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's value"""
slave_need_index = False
2018-01-05 23:32:00 +01:00
allow_unrestraint = True
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-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)
2017-12-19 23:11:45 +01:00
value[idx] = values.getdefaultvalue(self.path,
idx,
self.config_bag)
else:
if value == undefined:
2017-12-19 23:11:45 +01:00
value = values.getdefaultvalue(self.path,
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):
2017-11-12 20:11:56 +01:00
"""pop value for a specified master values
"""
2018-01-05 23:32:00 +01:00
self._get_option()
2017-12-19 23:11:45 +01:00
self.config_bag.config.delattr(self.path,
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()
2017-12-19 23:11:45 +01:00
self.config_bag.config.delattr(self.path,
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-01-05 23:32:00 +01:00
self._get_option()
2017-12-27 15:48:49 +01:00
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,
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):
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-10-22 09:48:08 +02:00
icon = '\u2937'
tmpl_help = ' {} {}: {}'
2017-11-12 20:11:56 +01:00
def __init__(self,
2017-12-27 15:48:49 +01:00
name,
2017-11-12 20:11:56 +01:00
path,
index,
2017-12-27 15:48:49 +01:00
subconfig,
2017-12-19 23:11:45 +01:00
config_bag):
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 _help(self):
txt = []
for module_name, module in self.registers.items():
module_doc = getdoc(module)
txt.append(self.tmpl_help.format(self.icon, module_name, module_doc))
txt.append(module(None, None).help)
return '\n'.join(txt)
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,
2017-12-27 15:48:49 +01:00
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)
elif subfunc == 'help':
return self._help()
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-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):
2017-12-27 15:48:49 +01:00
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-01-01 21:32:39 +01:00
@count
def group_type(self):
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-01-01 21:32:39 +01:00
if type == 'optiondescription':
return self.config_bag.config.getattr(self.path,
None,
self.config_bag
).iter_groups(self.config_bag, group_type)
elif type == 'all':
return self.config_bag.config.getattr(self.path,
None,
self.config_bag
).cfgimpl_get_children(self.config_bag)
else:
raise APIError(_('unknown list type {}').format(type))
class TiramisuContext(object):
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
2017-12-13 22:15:34 +01:00
class TiramisuContextInformation(TiramisuContext):
@count
def get(self, name, default=undefined):
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):
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):
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):
@count
def mandatory_warnings(self):
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):
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):
self.config_bag.config.reset(path, self.config_bag)
@count
def exportation(self):
2018-04-04 16:47:28 +02:00
return self.config_bag.config.cfgimpl_get_values()._p_.exportation()
@count
def importation(self, values):
return self.config_bag.config.cfgimpl_get_values()._p_.importation(values)
2017-12-13 22:15:34 +01:00
class TiramisuContextOwner(TiramisuContext):
2017-12-13 22:15:34 +01:00
@count
def get(self):
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):
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):
@count
def read_only(self):
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):
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):
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):
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):
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):
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):
self.config_bag.config.cfgimpl_get_settings().reset()
@count
def exportation(self):
return self.config_bag.config.cfgimpl_get_settings()._p_.exportation()
@count
def importation(self, properties):
return self.config_bag.config.cfgimpl_get_settings()._p_.importation(properties)
2017-12-13 22:15:34 +01:00
class TiramisuContextPermissive(TiramisuContext):
@count
def set(self, 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):
return self.config_bag.config.cfgimpl_get_settings()._pp_.exportation()
@count
def importation(self, permissives):
return 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):
2017-12-13 22:15:34 +01:00
@count
2017-11-23 16:56:14 +01:00
def find_first(self,
name,
type='option'):
2017-12-19 23:11:45 +01:00
check_properties = self.config_bag.force_unrestraint or self.config_bag.force_unrestraint
return self.config_bag.config.find_first(byname=name,
type_=type,
config_bag=self.config_bag)
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,
type='option'):
2017-12-19 23:11:45 +01:00
return self.config_bag.config.find(byname=name,
type_=type,
config_bag=self.config_bag)
2017-11-23 16:56:14 +01:00
2018-01-26 07:33:47 +01:00
@count
def get(self, path):
2018-03-31 21:06:19 +02:00
config_bag = self.config_bag.copy()
config_bag.validate = False
config_bag.force_unrestraint = True
config_bag.setting_properties = None
2018-01-26 07:33:47 +01:00
return self.config_bag.config.unwrap_from_path(path,
2018-03-31 21:06:19 +02:00
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):
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-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):
def find_first(self,
name,
byvalue=undefined):
return self.config_bag.config.find_firsts(byname=name,
byvalue=byvalue,
config_bag=self.config_bag)
class TiramisuDispatcherConfig(TiramisuContextConfig):
def __call__(self, path):
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)
2017-12-13 22:15:34 +01:00
class TiramisuDispatcherOption(TiramisuContextOption):
2017-11-23 16:56:14 +01:00
def __call__(self, path, index=None):
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)
2017-11-23 16:56:14 +01:00
class TiramisuAPI(object):
icon = '\u2937'
tmpl_help = ' {} {}: {}'
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)
2017-10-22 09:48:08 +02:00
elif subfunc == 'help':
return self._help()
2017-12-13 22:15:34 +01:00
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
def _help(self):
txt = ['[forcepermissive]']
2017-10-22 09:48:08 +02:00
for module_name, module in self.registers.items():
module_doc = getdoc(module)
txt.append(self.tmpl_help.format(self.icon, module_name, module_doc))
txt.append(module(None, None).help)
return '\n'.join(txt)
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)