test api: add DISPLAY variable to display more informations about generated test
api: new format, api.owner.get('path') become api.option('path').owner.get() value._getdefaultvalue: better submulti support
This commit is contained in:
@ -17,9 +17,10 @@ from .option import *
|
||||
from .error import APIError
|
||||
from .api import getapi
|
||||
from .option import __all__ as all_options
|
||||
from .setting import owners, undefined
|
||||
|
||||
|
||||
allfuncs = ['Config', 'getapi', 'APIError']
|
||||
allfuncs = ['Config', 'getapi', 'APIError', 'undefined']
|
||||
allfuncs.extend(all_options)
|
||||
del(all_options)
|
||||
__all__ = tuple(allfuncs)
|
||||
|
438
tiramisu/api.py
438
tiramisu/api.py
@ -18,64 +18,45 @@ from inspect import ismethod, getdoc
|
||||
from .error import APIError
|
||||
from .i18n import _
|
||||
from .setting import owners, undefined
|
||||
|
||||
|
||||
def getter(func):
|
||||
def wrapper(self, path, *args, **kwargs):
|
||||
opt = self._get_obj_by_path(path)
|
||||
if opt.impl_is_master_slaves('slave'):
|
||||
get = self._slave_get
|
||||
else:
|
||||
get = self._simple_get
|
||||
return get(path, *args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
||||
def setter(func):
|
||||
def wrapper(self, path, *args, **kwargs):
|
||||
opt = self._get_obj_by_path(path)
|
||||
if opt.impl_is_master_slaves('slave'):
|
||||
return self._slave_set(path, *args, **kwargs)
|
||||
elif opt.impl_is_master_slaves('master'):
|
||||
return self._master_set(path, *args, **kwargs)
|
||||
else:
|
||||
return self._simple_set(path, *args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
||||
def resetter(func):
|
||||
def wrapper(self, path, *args, **kwargs):
|
||||
opt = self._get_obj_by_path(path)
|
||||
if opt.impl_is_master_slaves('slave'):
|
||||
reset = self._slave_reset
|
||||
else:
|
||||
reset = self._simple_reset
|
||||
return reset(path, *args, **kwargs)
|
||||
return wrapper
|
||||
try:
|
||||
from .value import Multi
|
||||
except:
|
||||
Multi = list
|
||||
|
||||
|
||||
class CommonTiramisu(object):
|
||||
icon = '\u2937'
|
||||
tmpl_help = u' {} {}: {}'
|
||||
allow_unrestraint = False
|
||||
slave_need_index = True
|
||||
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
def __init__(self,
|
||||
opt,
|
||||
path,
|
||||
index,
|
||||
config,
|
||||
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
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
if not self.allow_unrestraint:
|
||||
self._unrestraint_not_allowed(force_unrestraint)
|
||||
|
||||
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')
|
||||
|
||||
def _unrestraint_not_allowed(self, force_unrestraint):
|
||||
if force_unrestraint:
|
||||
name = self.__class__.__name__[11:].lower()
|
||||
name = self.__class__.__name__[14:].lower()
|
||||
raise APIError(_('{} cannot be unrestraint').format(name))
|
||||
|
||||
def _get_obj_by_path(self, path, index=None):
|
||||
validate = not self.force_unrestraint
|
||||
return self.config.unwrap_from_path(path,
|
||||
validate=validate,
|
||||
validate_properties=validate,
|
||||
force_permissive=self.force_permissive,
|
||||
index=index)
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'help':
|
||||
return self._help()
|
||||
@ -95,183 +76,136 @@ class CommonTiramisu(object):
|
||||
return '\n'.join(txt)
|
||||
|
||||
|
||||
class TiramisuAPIOption(CommonTiramisu):
|
||||
class TiramisuOptionOption(CommonTiramisu):
|
||||
"""get information from an option"""
|
||||
allow_unrestraint = True
|
||||
slave_need_index = False
|
||||
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuAPIOption, self).__init__(config, force_permissive, force_unrestraint)
|
||||
self.config = config
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
def __init__(self, opt, path, index, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuOptionOption, self).__init__(opt,
|
||||
path,
|
||||
index,
|
||||
config,
|
||||
force_permissive,
|
||||
force_unrestraint)
|
||||
if config:
|
||||
self.values = self.config.cfgimpl_get_values()
|
||||
|
||||
def ismulti(self, path):
|
||||
def ismulti(self):
|
||||
"""test if option could have multi value"""
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_is_multi()
|
||||
return self.opt.impl_is_multi()
|
||||
|
||||
def ismasterslaves(self, path):
|
||||
def ismasterslaves(self):
|
||||
"""test if option is a master or a slave"""
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_is_master_slaves()
|
||||
return self.opt.impl_is_master_slaves()
|
||||
|
||||
def ismaster(self, path):
|
||||
def ismaster(self):
|
||||
"""test if option is a master"""
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_is_master_slaves('master')
|
||||
return self.opt.impl_is_master_slaves('master')
|
||||
|
||||
def isslave(self, path):
|
||||
def isslave(self):
|
||||
"""test if option is a slave"""
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_is_master_slaves('slave')
|
||||
return self.opt.impl_is_master_slaves('slave')
|
||||
|
||||
def getname(self, path):
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_getname()
|
||||
def getname(self):
|
||||
return self.opt.impl_getname()
|
||||
|
||||
def getdoc(self, path):
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_get_display_name()
|
||||
def getdoc(self):
|
||||
return self.opt.impl_get_display_name()
|
||||
|
||||
|
||||
class TiramisuAPIOwner(CommonTiramisu):
|
||||
class TiramisuOptionOwner(CommonTiramisu):
|
||||
"""manager option's owner"""
|
||||
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuAPIOwner, self).__init__(config, force_permissive, force_unrestraint)
|
||||
self.config = config
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
def __init__(self, opt, path, index, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuOptionOwner, self).__init__(opt,
|
||||
path,
|
||||
index,
|
||||
config,
|
||||
force_permissive,
|
||||
force_unrestraint)
|
||||
if config:
|
||||
self.values = self.config.cfgimpl_get_values()
|
||||
|
||||
@getter
|
||||
def get(self):
|
||||
"""get owner for a specified option"""
|
||||
pass
|
||||
return self.values.getowner(self.opt,
|
||||
index=self.index,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def _simple_get(self, path):
|
||||
##FIXME should not be here, just for check property
|
||||
#self.config.getattr(path,
|
||||
# force_permissive=self.force_permissive)
|
||||
# FIXME doublons aussi
|
||||
opt = self._get_obj_by_path(path)
|
||||
return self.values.getowner(opt,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def _slave_get(self, path, index):
|
||||
##FIXME should not be here, just for check property
|
||||
#self.config.getattr(path,
|
||||
# index=index,
|
||||
# force_permissive=self.force_permissive)
|
||||
# FIXME doublons aussi
|
||||
opt = self._get_obj_by_path(path,
|
||||
index=index)
|
||||
return self.values.getowner(opt,
|
||||
index=index,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def isdefault(self, path):
|
||||
def isdefault(self):
|
||||
"""is option has defaut value"""
|
||||
#FIXME isdefault for slave should have index!
|
||||
opt = self._get_obj_by_path(path)
|
||||
#FIXME should not be here, just for check property
|
||||
self.config.getattr(path,
|
||||
force_permissive=self.force_permissive)
|
||||
return self.values.is_default_owner(opt,
|
||||
return self.values.is_default_owner(self.opt,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
@setter
|
||||
def set(self):
|
||||
def set(self, owner):
|
||||
"""get owner for a specified option"""
|
||||
pass
|
||||
|
||||
def _simple_set(self, path, owner):
|
||||
#FIXME doublons, opt est deja dans le setter
|
||||
opt = self._get_obj_by_path(path)
|
||||
try:
|
||||
obj_owner = getattr(owners, owner)
|
||||
except AttributeError:
|
||||
owners.addowner(owner)
|
||||
obj_owner = getattr(owners, owner)
|
||||
self.values.setowner(opt,
|
||||
self.values.setowner(self.opt,
|
||||
obj_owner,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def _master_set(self, path, owner):
|
||||
return self._simple_set(path, owner)
|
||||
|
||||
def _slave_set(self, path, index, owner):
|
||||
#FIXME doublons, opt est deja dans le setter
|
||||
opt = self._get_obj_by_path(path, index)
|
||||
try:
|
||||
obj_owner = getattr(owners, owner)
|
||||
except AttributeError:
|
||||
owners.addowner(owner)
|
||||
obj_owner = getattr(owners, owner)
|
||||
self.values.setowner(opt,
|
||||
obj_owner,
|
||||
index,
|
||||
self.index,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
|
||||
class TiramisuAPIProperty(CommonTiramisu):
|
||||
class TiramisuOptionProperty(CommonTiramisu):
|
||||
"""manager option's property"""
|
||||
#allow_unrestraint = True
|
||||
slave_need_index = False
|
||||
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuAPIProperty, self).__init__(config, force_permissive, force_unrestraint)
|
||||
self.config = config
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
def __init__(self, opt, path, index, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuOptionProperty, self).__init__(opt,
|
||||
path,
|
||||
index,
|
||||
config,
|
||||
force_permissive,
|
||||
force_unrestraint)
|
||||
if config:
|
||||
self.settings = config.cfgimpl_get_settings()
|
||||
|
||||
@getter
|
||||
def get(self, path):
|
||||
"""get property for a specified option"""
|
||||
def get(self):
|
||||
self._test_slave_index()
|
||||
return self.settings.getproperties(self.opt, self.path, index=self.index, obj=False)
|
||||
|
||||
def _simple_get(self, path):
|
||||
opt = self._get_obj_by_path(path)
|
||||
return self.settings.getproperties(opt, path, obj=False)
|
||||
|
||||
def _slave_get(self, path, index):
|
||||
opt = self._get_obj_by_path(path)
|
||||
return self.settings.getproperties(opt, path, index=index, obj=False)
|
||||
|
||||
def set(self, path, properties):
|
||||
def set(self, properties):
|
||||
"""set properties for a specified option"""
|
||||
opt = self._get_obj_by_path(path)
|
||||
self.settings.setproperties(set(properties),
|
||||
opt,
|
||||
path)
|
||||
self.opt,
|
||||
self.path)
|
||||
|
||||
def reset(self, path):
|
||||
def reset(self):
|
||||
"""reset all personalised properties
|
||||
"""
|
||||
self._get_obj_by_path(path)
|
||||
self.settings.reset(_path=path)
|
||||
self.settings.reset(_path=self.path)
|
||||
|
||||
|
||||
class TiramisuAPIPermissive(CommonTiramisu):
|
||||
class TiramisuOptionPermissive(CommonTiramisu):
|
||||
"""manager option's property"""
|
||||
allow_unrestraint = True
|
||||
slave_need_index = False
|
||||
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuAPIPermissive, self).__init__(config, force_permissive, force_unrestraint)
|
||||
self.config = config
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
def __init__(self, opt, path, index, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuOptionPermissive, self).__init__(opt,
|
||||
path,
|
||||
index,
|
||||
config,
|
||||
force_permissive,
|
||||
force_unrestraint)
|
||||
if config:
|
||||
self.settings = config.cfgimpl_get_settings()
|
||||
|
||||
def get(self, path):
|
||||
def get(self):
|
||||
"""get permissive value for a specified path"""
|
||||
setting_properties = self.settings.getproperties(None, None, obj=False)
|
||||
return self.settings.getpermissive(setting_properties, path)
|
||||
return self.settings.getpermissive(setting_properties, self.path)
|
||||
|
||||
def set(self, path, permissive):
|
||||
self.settings.setpermissive(permissive, opt=None, path=path)
|
||||
def set(self, permissive):
|
||||
self.settings.setpermissive(permissive, opt=self.opt, path=self.path)
|
||||
|
||||
#def reset(self, path):
|
||||
# """reset all personalised properties
|
||||
@ -280,73 +214,108 @@ class TiramisuAPIPermissive(CommonTiramisu):
|
||||
# self.settings.reset(_path=path)
|
||||
|
||||
|
||||
class TiramisuAPIValue(CommonTiramisu):
|
||||
class TiramisuOptionValue(CommonTiramisu):
|
||||
"""manager option's value"""
|
||||
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuAPIValue, self).__init__(config, force_permissive, force_unrestraint)
|
||||
def __init__(self, opt, path, index, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuOptionValue, self).__init__(opt,
|
||||
path,
|
||||
index,
|
||||
config,
|
||||
force_permissive,
|
||||
force_unrestraint)
|
||||
|
||||
def append(self, value=undefined):
|
||||
if not self.opt.impl_is_master_slaves('master'):
|
||||
raise APIError('append is only allowed for a master')
|
||||
multi = self.config.getattr(self.path,
|
||||
force_permissive=self.force_permissive)
|
||||
multi.append(value)
|
||||
self.set(multi)
|
||||
|
||||
def get(self):
|
||||
value = self.config.getattr(self.path,
|
||||
index=self.index,
|
||||
force_permissive=self.force_permissive)
|
||||
if isinstance(value, Multi):
|
||||
value = list(value)
|
||||
return value
|
||||
|
||||
def set(self, value):
|
||||
"""set a value for a specified option"""
|
||||
values = self.config.cfgimpl_get_values()
|
||||
if isinstance(value, list):
|
||||
while undefined in value:
|
||||
idx = value.index(undefined)
|
||||
value[idx] = values._getdefaultvalue(self.opt,
|
||||
self.path,
|
||||
True,
|
||||
idx,
|
||||
undefined,
|
||||
True)
|
||||
else:
|
||||
if value == undefined:
|
||||
value = values._getdefaultvalue(self.opt,
|
||||
self.path,
|
||||
True,
|
||||
self.index,
|
||||
undefined,
|
||||
True)
|
||||
self.config.setattr(self.path,
|
||||
value,
|
||||
index=self.index,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def reset(self):
|
||||
"""reset value for a value"""
|
||||
if self.index is None:
|
||||
self.config.cfgimpl_get_values().reset(self.opt,
|
||||
path=self.path,
|
||||
force_permissive=self.force_permissive)
|
||||
else:
|
||||
#FIXME ... _p_ ...
|
||||
self.config.cfgimpl_get_values()._p_.resetvalue_index(self.path, self.index)
|
||||
|
||||
|
||||
class TiramisuOption(object):
|
||||
icon = '\u2937'
|
||||
tmpl_help = ' {} {}: {}'
|
||||
|
||||
def __init__(self, opt, path, index, config, force_permissive=False, force_unrestraint=False):
|
||||
self.opt = opt
|
||||
self.path = path
|
||||
self.index = index
|
||||
self.config = config
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
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]
|
||||
func_name = module_name[len(self.prefix):].lower()
|
||||
self.registers[func_name] = module
|
||||
|
||||
def append(self, path, value=undefined):
|
||||
opt = self._get_obj_by_path(path)
|
||||
if not opt.impl_is_master_slaves('master'):
|
||||
raise APIError('append is only allowed for a master')
|
||||
multi = self.config.getattr(path,
|
||||
force_permissive=self.force_permissive)
|
||||
multi.append(value, force_permissive=self.force_permissive)
|
||||
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)
|
||||
|
||||
@getter
|
||||
def get(self):
|
||||
"""get value for a specified option"""
|
||||
pass
|
||||
|
||||
def _simple_get(self, path):
|
||||
return self.config.getattr(path,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def _slave_get(self, path, index):
|
||||
return self.config.getattr(path,
|
||||
index=index,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
@setter
|
||||
def set(self):
|
||||
"""set a value for a specified option"""
|
||||
pass
|
||||
|
||||
def _simple_set(self, path, value):
|
||||
self.config.setattr(path,
|
||||
value,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def _master_set(self, path, index, value):
|
||||
multi = self.config.getattr(path,
|
||||
force_permissive=self.force_permissive)
|
||||
multi[index] = value
|
||||
|
||||
def _slave_set(self, path, index, value):
|
||||
self.config.setattr(path,
|
||||
value,
|
||||
index=index,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
@resetter
|
||||
def reset(self, path):
|
||||
"""reset value for a value"""
|
||||
pass
|
||||
|
||||
def _simple_reset(self, path):
|
||||
opt = self._get_obj_by_path(path)
|
||||
self.config.cfgimpl_get_values().reset(opt,
|
||||
path=path,
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def _slave_reset(self, path, index):
|
||||
self._get_obj_by_path(path)
|
||||
#FIXME ... _p_ ...
|
||||
self.config.cfgimpl_get_values()._p_.resetvalue_index(path, index)
|
||||
def __getattr__(self, subfunc):
|
||||
if subfunc in self.registers:
|
||||
return self.registers[subfunc](self.opt,
|
||||
self.path,
|
||||
self.index,
|
||||
self.config,
|
||||
self.force_permissive,
|
||||
self.force_unrestraint)
|
||||
elif subfunc == 'help':
|
||||
return self._help()
|
||||
else:
|
||||
raise APIError(_('please specify a valid sub function'))
|
||||
|
||||
|
||||
class TiramisuAPI(object):
|
||||
@ -361,20 +330,25 @@ class TiramisuAPI(object):
|
||||
self.config.read_write()
|
||||
self.config.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
#/FIXME ?
|
||||
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]
|
||||
func_name = module_name[11:].lower()
|
||||
self.registers[func_name] = module
|
||||
|
||||
def option(self, path, index=None):
|
||||
validate = not self.force_unrestraint
|
||||
opt = self.config.unwrap_from_path(path,
|
||||
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')
|
||||
return TiramisuOption(opt,
|
||||
path,
|
||||
index,
|
||||
self.config,
|
||||
force_permissive=self.force_permissive,
|
||||
force_unrestraint=self.force_unrestraint)
|
||||
|
||||
def __getattr__(self, subfunc):
|
||||
if subfunc in self.registers:
|
||||
return self.registers[subfunc](self.config,
|
||||
self.force_permissive,
|
||||
self.force_unrestraint)
|
||||
elif subfunc == 'forcepermissive':
|
||||
if subfunc == 'forcepermissive':
|
||||
return TiramisuAPI(self.config, force_permissive=True, force_unrestraint=self.force_unrestraint)
|
||||
elif subfunc == 'unrestraint':
|
||||
return TiramisuAPI(self.config, force_permissive=self.force_permissive, force_unrestraint=True)
|
||||
|
@ -103,11 +103,23 @@ class Values(object):
|
||||
if opt.impl_is_multi() and index is not None:
|
||||
if value == []:
|
||||
value = opt.impl_getdefault_multi()
|
||||
if submulti_index is undefined and opt.impl_is_submulti():
|
||||
if value is None:
|
||||
value = []
|
||||
elif not isinstance(value, list):
|
||||
value = [value]
|
||||
else:
|
||||
if len(value) > index:
|
||||
value = value[index]
|
||||
else:
|
||||
value = opt.impl_getdefault_multi()
|
||||
if submulti_index is undefined and opt.impl_is_submulti():
|
||||
if value is None:
|
||||
value = []
|
||||
elif not isinstance(value, list):
|
||||
value = [value]
|
||||
if opt.impl_is_submulti() and not isinstance(value, list) and submulti_index is undefined:
|
||||
value = [value]
|
||||
return value
|
||||
|
||||
def _getvalue(self, opt, path, self_properties, index, submulti_index,
|
||||
|
Reference in New Issue
Block a user