2017-10-22 09:48:08 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2017 Team tiramisu (see AUTHORS for all contributors)
|
|
|
|
#
|
|
|
|
# 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
|
2017-11-13 22:45:53 +01:00
|
|
|
from .error import APIError, PropertiesOptionError
|
2017-10-22 09:48:08 +02:00
|
|
|
from .i18n import _
|
|
|
|
from .setting import owners, undefined
|
2017-11-28 22:42:30 +01:00
|
|
|
from time import time
|
|
|
|
from copy import deepcopy
|
2017-11-05 15:40:27 +01:00
|
|
|
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
|
|
|
|
#COUNT_TIME = {}
|
|
|
|
|
|
|
|
|
|
|
|
def count(func):
|
|
|
|
global MOD_COUNT_TIME
|
|
|
|
func_name = func.__name__
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
time1 = time()
|
|
|
|
ret = func(*args, **kwargs)
|
|
|
|
time2 = time()
|
|
|
|
diff = (time2 - time1) * 1000.0
|
|
|
|
MOD_COUNT_TIME[func_name]['max'] = max(MOD_COUNT_TIME[func_name]['max'], diff)
|
|
|
|
MOD_COUNT_TIME[func_name]['min'] = min(MOD_COUNT_TIME[func_name]['min'], diff)
|
|
|
|
MOD_COUNT_TIME[func_name]['total'] += diff
|
|
|
|
MOD_COUNT_TIME[func_name]['nb'] += 1
|
|
|
|
#print('%s function took %0.3f ms' % (func_name, diff))
|
|
|
|
#print(COUNT_TIME)
|
|
|
|
return ret
|
|
|
|
if COUNT_TIME is not False:
|
|
|
|
COUNT_TIME[func_name] = {'max': 0,
|
|
|
|
'min': 1000,
|
|
|
|
'nb': 0,
|
|
|
|
'total': 0}
|
|
|
|
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)
|
|
|
|
for func in MOD_COUNT_TIME:
|
|
|
|
print('>', func)
|
|
|
|
print('=> nb:', MOD_COUNT_TIME[func]['nb'])
|
|
|
|
if MOD_COUNT_TIME[func]['nb'] != 0:
|
|
|
|
print('=> min:', MOD_COUNT_TIME[func]['min'])
|
|
|
|
print('=> max:', MOD_COUNT_TIME[func]['max'])
|
|
|
|
print('=> moy:', MOD_COUNT_TIME[func]['total'] / MOD_COUNT_TIME[func]['nb'])
|
|
|
|
MOD_COUNT_TIME = deepcopy(COUNT_TIME)
|
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
class CommonTiramisuOption(object):
|
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-11-05 15:40:27 +01:00
|
|
|
slave_need_index = True
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
2017-11-12 20:11:56 +01:00
|
|
|
setting_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
|
|
|
self.opt = opt
|
|
|
|
self.path = path
|
|
|
|
self.index = index
|
|
|
|
if self.slave_need_index:
|
|
|
|
self._test_slave_index()
|
|
|
|
self.config = config
|
2017-11-12 20:11:56 +01:00
|
|
|
self.setting_properties = setting_properties
|
2017-11-05 15:40:27 +01:00
|
|
|
self.force_permissive = force_permissive
|
|
|
|
self.force_unrestraint = force_unrestraint
|
2017-10-25 08:46:22 +02:00
|
|
|
if not self.allow_unrestraint:
|
|
|
|
self._unrestraint_not_allowed(force_unrestraint)
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def _test_slave_index(self):
|
|
|
|
if self.index is None and self.opt.impl_is_master_slaves('slave'):
|
|
|
|
raise APIError('index must be set with a slave option')
|
|
|
|
|
2017-10-25 08:46:22 +02:00
|
|
|
def _unrestraint_not_allowed(self, force_unrestraint):
|
|
|
|
if force_unrestraint:
|
2017-11-05 15:40:27 +01:00
|
|
|
name = self.__class__.__name__[14:].lower()
|
2017-10-25 08:46:22 +02:00
|
|
|
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:
|
2017-11-16 21:30:07 +01:00
|
|
|
if not hasattr(CommonTiramisuOption, name):
|
2017-10-22 09:48:08 +02:00
|
|
|
raise APIError(_('unknown method {}').format(name))
|
|
|
|
else:
|
2017-11-16 21:30:07 +01:00
|
|
|
super(CommonTiramisuOption, self).__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)
|
|
|
|
|
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
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-11-05 15:40:27 +01:00
|
|
|
slave_need_index = False
|
|
|
|
|
2017-11-12 20:11:56 +01:00
|
|
|
def __init__(self,
|
|
|
|
opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
|
|
|
setting_properties,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
2017-11-05 15:40:27 +01:00
|
|
|
super(TiramisuOptionOption, self).__init__(opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
2017-11-12 20:11:56 +01:00
|
|
|
setting_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
force_permissive,
|
|
|
|
force_unrestraint)
|
2017-10-22 09:48:08 +02:00
|
|
|
if config:
|
|
|
|
self.values = self.config.cfgimpl_get_values()
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def ismulti(self):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""test if option could have multi value"""
|
2017-11-05 15:40:27 +01:00
|
|
|
return self.opt.impl_is_multi()
|
2017-11-16 21:30:07 +01:00
|
|
|
|
|
|
|
def issubmulti(self):
|
|
|
|
"""test if option could have submulti value"""
|
|
|
|
return self.opt.impl_is_submulti()
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def ismasterslaves(self):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""test if option is a master or a slave"""
|
2017-11-05 15:40:27 +01:00
|
|
|
return self.opt.impl_is_master_slaves()
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def ismaster(self):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""test if option is a master"""
|
2017-11-05 15:40:27 +01:00
|
|
|
return self.opt.impl_is_master_slaves('master')
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def isslave(self):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""test if option is a slave"""
|
2017-11-05 15:40:27 +01:00
|
|
|
return self.opt.impl_is_master_slaves('slave')
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def getname(self):
|
|
|
|
return self.opt.impl_getname()
|
2017-10-25 08:46:22 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def getdoc(self):
|
|
|
|
return self.opt.impl_get_display_name()
|
2017-10-25 08:46:22 +02:00
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
def getdefault(self):
|
|
|
|
return self.opt.impl_getdefault()
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
def getdefaultmulti(self):
|
|
|
|
return self.opt.impl_getdefault_multi()
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
|
|
|
setting_properties,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
super(TiramisuOptionOwner, self).__init__(opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
2017-11-12 20:11:56 +01:00
|
|
|
setting_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
force_permissive,
|
|
|
|
force_unrestraint)
|
2017-10-22 09:48:08 +02:00
|
|
|
if config:
|
|
|
|
self.values = self.config.cfgimpl_get_values()
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
"""get owner for a specified option"""
|
2017-11-05 15:40:27 +01:00
|
|
|
return self.values.getowner(self.opt,
|
2017-11-20 17:01:36 +01:00
|
|
|
self.path,
|
|
|
|
self.setting_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
index=self.index,
|
|
|
|
force_permissive=self.force_permissive)
|
|
|
|
|
2017-11-28 22:42:30 +01:00
|
|
|
@count
|
2017-11-05 15:40:27 +01:00
|
|
|
def isdefault(self):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""is option has defaut value"""
|
2017-11-05 15:40:27 +01:00
|
|
|
return self.values.is_default_owner(self.opt,
|
2017-11-20 17:01:36 +01:00
|
|
|
self.path,
|
|
|
|
self.setting_properties,
|
2017-11-12 20:11:56 +01:00
|
|
|
index=self.index,
|
2017-10-22 09:48:08 +02:00
|
|
|
force_permissive=self.force_permissive)
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def set(self, owner):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""get owner for a specified option"""
|
|
|
|
try:
|
|
|
|
obj_owner = getattr(owners, owner)
|
|
|
|
except AttributeError:
|
|
|
|
owners.addowner(owner)
|
|
|
|
obj_owner = getattr(owners, owner)
|
2017-12-04 20:05:36 +01:00
|
|
|
self.values.setowner(self.opt,
|
|
|
|
self.path,
|
2017-10-22 09:48:08 +02:00
|
|
|
obj_owner,
|
2017-11-20 17:01:36 +01:00
|
|
|
self.index)
|
2017-10-22 09:48:08 +02:00
|
|
|
|
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
class TiramisuOptionProperty(CommonTiramisuOption):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""manager option's property"""
|
2017-11-05 15:40:27 +01:00
|
|
|
#allow_unrestraint = True
|
|
|
|
slave_need_index = False
|
|
|
|
|
2017-11-12 20:11:56 +01:00
|
|
|
def __init__(self,
|
|
|
|
opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
|
|
|
setting_properties,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
2017-11-05 15:40:27 +01:00
|
|
|
super(TiramisuOptionProperty, self).__init__(opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
2017-11-12 20:11:56 +01:00
|
|
|
setting_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
force_permissive,
|
|
|
|
force_unrestraint)
|
2017-10-22 09:48:08 +02:00
|
|
|
if config:
|
|
|
|
self.settings = config.cfgimpl_get_settings()
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def get(self):
|
|
|
|
self._test_slave_index()
|
2017-11-12 20:11:56 +01:00
|
|
|
return self.settings.getproperties(self.opt,
|
|
|
|
self.path,
|
2017-11-20 17:01:36 +01:00
|
|
|
self.setting_properties,
|
|
|
|
index=self.index)
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def set(self, properties):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""set properties for a specified option"""
|
2017-11-20 17:01:36 +01:00
|
|
|
self.settings.setproperties(self.opt,
|
|
|
|
self.path,
|
|
|
|
properties)
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def reset(self):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""reset all personalised properties
|
|
|
|
"""
|
2017-12-04 20:05:36 +01:00
|
|
|
self.settings.reset(opt=self.opt,
|
|
|
|
path=self.path)
|
2017-10-22 09:48:08 +02:00
|
|
|
|
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
class TiramisuOptionPermissive(CommonTiramisuOption):
|
2017-11-03 21:52:13 +01:00
|
|
|
"""manager option's property"""
|
2017-11-05 15:40:27 +01:00
|
|
|
allow_unrestraint = True
|
|
|
|
slave_need_index = False
|
|
|
|
|
2017-11-12 20:11:56 +01:00
|
|
|
def __init__(self,
|
|
|
|
opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
|
|
|
setting_properties,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
2017-11-05 15:40:27 +01:00
|
|
|
super(TiramisuOptionPermissive, self).__init__(opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
2017-11-12 20:11:56 +01:00
|
|
|
setting_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
force_permissive,
|
|
|
|
force_unrestraint)
|
2017-11-03 21:52:13 +01:00
|
|
|
if config:
|
|
|
|
self.settings = config.cfgimpl_get_settings()
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def get(self):
|
2017-11-03 21:52:13 +01:00
|
|
|
"""get permissive value for a specified path"""
|
2017-12-04 20:05:36 +01:00
|
|
|
return self.settings.getpermissive(self.opt,
|
|
|
|
self.path)
|
2017-11-03 21:52:13 +01:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def set(self, permissive):
|
2017-11-20 17:01:36 +01:00
|
|
|
self.settings.setpermissive(self.opt,
|
|
|
|
self.path,
|
|
|
|
permissive)
|
2017-11-03 21:52:13 +01:00
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
def reset(self, path):
|
|
|
|
"""reset all personalised permissive
|
|
|
|
"""
|
|
|
|
self.set(tuple())
|
2017-11-03 21:52:13 +01:00
|
|
|
|
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
class TiramisuOptionValue(CommonTiramisuOption):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""manager option's value"""
|
2017-11-16 21:30:07 +01:00
|
|
|
slave_need_index = False
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-12 20:11:56 +01:00
|
|
|
def __init__(self,
|
|
|
|
opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
|
|
|
setting_properties,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
super(TiramisuOptionValue, self).__init__(opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
2017-11-12 20:11:56 +01:00
|
|
|
setting_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
force_permissive,
|
|
|
|
force_unrestraint)
|
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):
|
2017-11-16 21:30:07 +01:00
|
|
|
self._test_slave_index()
|
2017-11-12 20:11:56 +01:00
|
|
|
settings = self.config.cfgimpl_get_settings()
|
2017-11-05 15:40:27 +01:00
|
|
|
value = self.config.getattr(self.path,
|
|
|
|
index=self.index,
|
2017-11-12 20:11:56 +01:00
|
|
|
setting_properties=self.setting_properties,
|
2017-10-22 09:48:08 +02:00
|
|
|
force_permissive=self.force_permissive)
|
2017-11-05 15:40:27 +01:00
|
|
|
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
|
2017-11-05 15:40:27 +01:00
|
|
|
def set(self, value):
|
|
|
|
"""set a value for a specified option"""
|
2017-11-16 21:30:07 +01:00
|
|
|
self._test_slave_index()
|
2017-11-05 15:40:27 +01:00
|
|
|
values = self.config.cfgimpl_get_values()
|
|
|
|
if isinstance(value, list):
|
|
|
|
while undefined in value:
|
|
|
|
idx = value.index(undefined)
|
2017-11-12 20:11:56 +01:00
|
|
|
value[idx] = values.getdefaultvalue(self.opt,
|
|
|
|
self.path,
|
2017-11-28 22:42:30 +01:00
|
|
|
self.setting_properties,
|
2017-11-12 20:11:56 +01:00
|
|
|
idx)
|
2017-11-05 15:40:27 +01:00
|
|
|
else:
|
|
|
|
if value == undefined:
|
2017-11-12 20:11:56 +01:00
|
|
|
value = values.getdefaultvalue(self.opt,
|
|
|
|
self.path,
|
2017-11-28 22:42:30 +01:00
|
|
|
self.setting_properties,
|
2017-11-12 20:11:56 +01:00
|
|
|
self.index)
|
2017-11-05 15:40:27 +01:00
|
|
|
self.config.setattr(self.path,
|
2017-10-22 09:48:08 +02:00
|
|
|
value,
|
2017-11-05 15:40:27 +01:00
|
|
|
index=self.index,
|
2017-11-28 22:42:30 +01:00
|
|
|
setting_properties=self.setting_properties,
|
2017-10-22 09:48:08 +02:00
|
|
|
force_permissive=self.force_permissive)
|
|
|
|
|
2017-11-28 22:42:30 +01:00
|
|
|
@count
|
2017-11-12 20:11:56 +01:00
|
|
|
def pop(self, index):
|
|
|
|
"""pop value for a specified master values
|
|
|
|
"""
|
2017-11-16 21:30:07 +01:00
|
|
|
self._test_slave_index()
|
|
|
|
#FIXME only for master
|
2017-11-12 20:11:56 +01:00
|
|
|
self.config.delattr(self.path,
|
|
|
|
index=index,
|
|
|
|
setting_properties=self.setting_properties,
|
|
|
|
force_permissive=self.force_permissive)
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
def reset(self):
|
2017-10-22 09:48:08 +02:00
|
|
|
"""reset value for a value"""
|
2017-11-16 21:30:07 +01:00
|
|
|
self._test_slave_index()
|
2017-11-12 20:11:56 +01:00
|
|
|
self.config.delattr(self.path,
|
|
|
|
index=self.index,
|
|
|
|
setting_properties=self.setting_properties,
|
|
|
|
force_permissive=self.force_permissive)
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
def len(self):
|
|
|
|
#FIXME only for slave
|
|
|
|
subconfig_path = self.path.rsplit('.', 1)[0]
|
|
|
|
subconfig = self.config.getattr(subconfig_path,
|
|
|
|
setting_properties=self.setting_properties,
|
|
|
|
force_permissive=self.force_permissive)
|
|
|
|
return subconfig.cfgimpl_get_length()
|
|
|
|
|
2017-10-22 09:48:08 +02:00
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
class TiramisuOption(object):
|
2017-10-22 09:48:08 +02:00
|
|
|
icon = '\u2937'
|
|
|
|
tmpl_help = ' {} {}: {}'
|
|
|
|
|
2017-11-12 20:11:56 +01:00
|
|
|
def __init__(self,
|
|
|
|
opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config,
|
|
|
|
setting_properties,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
|
|
|
|
2017-11-05 15:40:27 +01:00
|
|
|
self.opt = opt
|
|
|
|
self.path = path
|
|
|
|
self.index = index
|
2017-10-22 09:48:08 +02:00
|
|
|
self.config = config
|
2017-11-12 20:11:56 +01:00
|
|
|
self.setting_properties = setting_properties
|
2017-10-22 09:48:08 +02:00
|
|
|
self.force_permissive = force_permissive
|
2017-10-25 08:46:22 +02:00
|
|
|
self.force_unrestraint = force_unrestraint
|
2017-10-22 09:48:08 +02:00
|
|
|
self.registers = {}
|
|
|
|
self.prefix = self.__class__.__name__
|
|
|
|
for module_name in globals().keys():
|
|
|
|
if module_name != self.prefix and module_name.startswith(self.prefix):
|
|
|
|
module = globals()[module_name]
|
2017-11-05 15:40:27 +01:00
|
|
|
func_name = module_name[len(self.prefix):].lower()
|
2017-10-22 09:48:08 +02:00
|
|
|
self.registers[func_name] = module
|
|
|
|
|
2017-11-05 15:40:27 +01: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:
|
2017-11-05 15:40:27 +01:00
|
|
|
return self.registers[subfunc](self.opt,
|
|
|
|
self.path,
|
|
|
|
self.index,
|
|
|
|
self.config,
|
2017-11-12 20:11:56 +01:00
|
|
|
self.setting_properties,
|
2017-10-25 08:46:22 +02:00
|
|
|
self.force_permissive,
|
|
|
|
self.force_unrestraint)
|
2017-11-05 15:40:27 +01:00
|
|
|
elif subfunc == 'help':
|
|
|
|
return self._help()
|
|
|
|
else:
|
|
|
|
raise APIError(_('please specify a valid sub function'))
|
|
|
|
|
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
class TiramisuContext(object):
|
2017-11-23 16:56:14 +01:00
|
|
|
def __init__(self,
|
|
|
|
config,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint,
|
|
|
|
setting_properties=None):
|
|
|
|
if setting_properties is None:
|
|
|
|
setting_properties = config.cfgimpl_get_settings().get_context_properties()
|
2017-11-16 21:30:07 +01:00
|
|
|
self.config = config
|
2017-11-23 16:56:14 +01:00
|
|
|
self.force_permissive = force_permissive
|
|
|
|
self.force_unrestraint = force_unrestraint
|
|
|
|
self.setting_properties = setting_properties
|
2017-11-16 21:30:07 +01:00
|
|
|
|
|
|
|
class TiramisuContextOwner(TiramisuContext):
|
|
|
|
def get(self):
|
|
|
|
return self.config.cfgimpl_get_settings().getowner()
|
|
|
|
|
|
|
|
|
2017-11-23 16:56:14 +01:00
|
|
|
class TiramisuContextOption(TiramisuContext):
|
|
|
|
def find_first(self,
|
|
|
|
name,
|
|
|
|
type='option'):
|
|
|
|
check_properties = self.force_unrestraint or self.force_unrestraint
|
|
|
|
return self.config.find_first(byname=name,
|
|
|
|
type_=type,
|
|
|
|
setting_properties=self.setting_properties,
|
|
|
|
force_permissive=self.force_permissive,
|
|
|
|
check_properties=not self.force_unrestraint)
|
|
|
|
|
|
|
|
def find(self,
|
|
|
|
name,
|
|
|
|
type='option'):
|
|
|
|
return self.config.find(byname=name,
|
|
|
|
type_=type,
|
|
|
|
setting_properties=self.setting_properties,
|
|
|
|
force_permissive=self.force_permissive,
|
|
|
|
check_properties=not self.force_unrestraint)
|
|
|
|
|
|
|
|
def get(self, path):
|
|
|
|
return self.config.unwrap_from_path(path,
|
|
|
|
validate=False,
|
|
|
|
validate_properties=False)
|
|
|
|
|
|
|
|
|
|
|
|
class TiramisuOptionDispatcher(TiramisuContextOption):
|
2017-11-12 20:11:56 +01:00
|
|
|
def __init__(self,
|
|
|
|
config,
|
2017-11-23 16:56:14 +01:00
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
|
|
|
self.setting_properties = config.cfgimpl_get_settings().get_context_properties()
|
|
|
|
super(TiramisuOptionDispatcher, self).__init__(config,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint,
|
|
|
|
self.setting_properties)
|
2017-11-05 15:40:27 +01:00
|
|
|
|
2017-11-23 16:56:14 +01:00
|
|
|
def __call__(self, path, index=None):
|
2017-11-05 15:40:27 +01:00
|
|
|
validate = not self.force_unrestraint
|
2017-11-13 22:45:53 +01:00
|
|
|
if validate:
|
2017-11-23 16:56:14 +01:00
|
|
|
s_properties = self.setting_properties
|
2017-11-13 22:45:53 +01:00
|
|
|
else:
|
|
|
|
s_properties = None
|
2017-11-05 15:40:27 +01:00
|
|
|
opt = self.config.unwrap_from_path(path,
|
2017-11-13 22:45:53 +01:00
|
|
|
setting_properties=s_properties,
|
2017-11-05 15:40:27 +01:00
|
|
|
validate=validate,
|
|
|
|
validate_properties=validate,
|
|
|
|
force_permissive=self.force_permissive,
|
|
|
|
index=index)
|
|
|
|
if index is not None and not opt.impl_is_master_slaves('slave'):
|
|
|
|
raise APIError('index must be set only with a slave option')
|
2017-12-04 20:05:36 +01:00
|
|
|
if opt.impl_is_symlinkoption():
|
|
|
|
true_opt = opt.impl_getopt()
|
|
|
|
true_path = true_opt.impl_getpath(self.config)
|
|
|
|
self.config.unwrap_from_path(true_path,
|
|
|
|
setting_properties=s_properties,
|
|
|
|
validate=validate,
|
|
|
|
validate_properties=validate,
|
|
|
|
force_permissive=self.force_permissive,
|
|
|
|
index=index)
|
|
|
|
else:
|
|
|
|
true_opt = None
|
|
|
|
true_path = None
|
2017-11-05 15:40:27 +01:00
|
|
|
return TiramisuOption(opt,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
self.config,
|
2017-11-23 16:56:14 +01:00
|
|
|
self.setting_properties,
|
2017-11-12 20:11:56 +01:00
|
|
|
self.force_permissive,
|
|
|
|
self.force_unrestraint)
|
2017-11-05 15:40:27 +01:00
|
|
|
|
2017-11-16 21:30:07 +01:00
|
|
|
|
2017-11-23 16:56:14 +01:00
|
|
|
class TiramisuContextConfig(TiramisuContext):
|
|
|
|
def make_dict(self):
|
|
|
|
return self.config.make_dict(self.setting_properties)
|
|
|
|
|
|
|
|
|
|
|
|
class TiramisuConfigDispatcher(TiramisuContextConfig):
|
|
|
|
def __init__(self,
|
|
|
|
config,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint):
|
|
|
|
self.setting_properties = config.cfgimpl_get_settings().get_context_properties()
|
|
|
|
super(TiramisuConfigDispatcher, self).__init__(config,
|
|
|
|
force_permissive,
|
|
|
|
force_unrestraint,
|
|
|
|
self.setting_properties)
|
|
|
|
|
|
|
|
def __call__(self, path):
|
|
|
|
if path is None:
|
|
|
|
subconfig = self.config
|
|
|
|
else:
|
|
|
|
subconfig = self.config.getconfig(path)
|
|
|
|
return TiramisuAPI(subconfig,
|
|
|
|
force_permissive=self.force_permissive,
|
|
|
|
force_unrestraint=self.force_unrestraint)
|
|
|
|
|
|
|
|
|
|
|
|
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-11-05 15:40:27 +01:00
|
|
|
def __getattr__(self, subfunc):
|
|
|
|
if subfunc == 'forcepermissive':
|
2017-11-23 16:56:14 +01:00
|
|
|
return TiramisuAPI(self._config,
|
2017-11-12 20:11:56 +01:00
|
|
|
force_permissive=True,
|
|
|
|
force_unrestraint=self.force_unrestraint)
|
2017-10-25 08:46:22 +02:00
|
|
|
elif subfunc == 'unrestraint':
|
2017-11-23 16:56:14 +01:00
|
|
|
return TiramisuAPI(self._config,
|
2017-11-12 20:11:56 +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-11-16 21:30:07 +01:00
|
|
|
elif subfunc == 'owner':
|
2017-11-23 16:56:14 +01:00
|
|
|
return TiramisuContextOwner(self._config,
|
|
|
|
self.force_permissive,
|
|
|
|
self.force_unrestraint)
|
|
|
|
elif subfunc == 'config':
|
|
|
|
return TiramisuConfigDispatcher(self._config,
|
|
|
|
force_permissive=self.force_permissive,
|
|
|
|
force_unrestraint=self.force_unrestraint)
|
|
|
|
elif subfunc == 'option':
|
|
|
|
return TiramisuOptionDispatcher(self._config,
|
|
|
|
force_permissive=self.force_permissive,
|
|
|
|
force_unrestraint=self.force_unrestraint)
|
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):
|
2017-11-03 21:52:13 +01:00
|
|
|
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-11-20 17:01:36 +01:00
|
|
|
def read_only(self):
|
2017-11-23 16:56:14 +01:00
|
|
|
self._config.read_write()
|
2017-11-20 17:01:36 +01:00
|
|
|
|
|
|
|
def read_write(self):
|
2017-11-23 16:56:14 +01:00
|
|
|
settings = self._config.cfgimpl_get_settings()
|
|
|
|
self._config.read_write()
|
2017-11-20 17:01:36 +01:00
|
|
|
# #FIXME ?
|
|
|
|
settings.set_context_permissive(frozenset(['hidden']))
|
|
|
|
#/FIXME ?
|
|
|
|
|
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)
|