add unrestraint in option
This commit is contained in:
@ -57,9 +57,22 @@ def resetter(func):
|
||||
class CommonTiramisu(object):
|
||||
icon = '\u2937'
|
||||
tmpl_help = u' {} {}: {}'
|
||||
allow_unrestraint = False
|
||||
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
if not self.allow_unrestraint:
|
||||
self._unrestraint_not_allowed(force_unrestraint)
|
||||
|
||||
def _unrestraint_not_allowed(self, force_unrestraint):
|
||||
if force_unrestraint:
|
||||
name = self.__class__.__name__[11:].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)
|
||||
|
||||
@ -70,7 +83,7 @@ class CommonTiramisu(object):
|
||||
if not hasattr(CommonTiramisu, name):
|
||||
raise APIError(_('unknown method {}').format(name))
|
||||
else:
|
||||
super(CommonTiramisu).__getattribute__(name)
|
||||
super(CommonTiramisu, self).__getattribute__(name)
|
||||
|
||||
def _help(self):
|
||||
txt = []
|
||||
@ -84,10 +97,13 @@ class CommonTiramisu(object):
|
||||
|
||||
class TiramisuAPIOption(CommonTiramisu):
|
||||
"""get information from an option"""
|
||||
allow_unrestraint = True
|
||||
|
||||
def __init__(self, config, force_permissive):
|
||||
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
|
||||
if config:
|
||||
self.values = self.config.cfgimpl_get_values()
|
||||
|
||||
@ -111,13 +127,23 @@ class TiramisuAPIOption(CommonTiramisu):
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_is_master_slaves('slave')
|
||||
|
||||
def getname(self, path):
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_getname()
|
||||
|
||||
def getdoc(self, path):
|
||||
opt = self._get_obj_by_path(path)
|
||||
return opt.impl_get_display_name()
|
||||
|
||||
|
||||
class TiramisuAPIOwner(CommonTiramisu):
|
||||
"""manager option's owner"""
|
||||
|
||||
def __init__(self, config, force_permissive):
|
||||
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
|
||||
if config:
|
||||
self.values = self.config.cfgimpl_get_values()
|
||||
|
||||
@ -194,9 +220,11 @@ class TiramisuAPIOwner(CommonTiramisu):
|
||||
class TiramisuAPIProperty(CommonTiramisu):
|
||||
"""manager option's property"""
|
||||
|
||||
def __init__(self, config, force_permissive):
|
||||
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
|
||||
if config:
|
||||
self.settings = config.cfgimpl_get_settings()
|
||||
|
||||
@ -229,9 +257,11 @@ class TiramisuAPIProperty(CommonTiramisu):
|
||||
class TiramisuAPIValue(CommonTiramisu):
|
||||
"""manager option's value"""
|
||||
|
||||
def __init__(self, config, force_permissive):
|
||||
def __init__(self, config, force_permissive, force_unrestraint):
|
||||
super(TiramisuAPIValue, self).__init__(config, force_permissive, force_unrestraint)
|
||||
self.config = config
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
|
||||
def append(self, path, value=undefined):
|
||||
opt = self._get_obj_by_path(path)
|
||||
@ -288,19 +318,19 @@ class TiramisuAPIValue(CommonTiramisu):
|
||||
force_permissive=self.force_permissive)
|
||||
|
||||
def _slave_reset(self, path, index):
|
||||
#self._get_obj_by_path(path)
|
||||
multi = self.config.getattr(path,
|
||||
force_permissive=self.force_permissive)
|
||||
del(multi[index])
|
||||
self._get_obj_by_path(path)
|
||||
#FIXME ... _p_ ...
|
||||
self.config.cfgimpl_get_values()._p_.resetvalue_index(path, index)
|
||||
|
||||
|
||||
class TiramisuAPI(object):
|
||||
icon = '\u2937'
|
||||
tmpl_help = ' {} {}: {}'
|
||||
|
||||
def __init__(self, config, force_permissive=False):
|
||||
def __init__(self, config, force_permissive=False, force_unrestraint=False):
|
||||
self.config = config
|
||||
self.force_permissive = force_permissive
|
||||
self.force_unrestraint = force_unrestraint
|
||||
#FIXME ?
|
||||
self.config.read_write()
|
||||
self.config.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
@ -316,9 +346,12 @@ class TiramisuAPI(object):
|
||||
def __getattr__(self, subfunc):
|
||||
if subfunc in self.registers:
|
||||
return self.registers[subfunc](self.config,
|
||||
self.force_permissive)
|
||||
self.force_permissive,
|
||||
self.force_unrestraint)
|
||||
elif subfunc == 'permissive':
|
||||
return TiramisuAPI(self.config, force_permissive=True)
|
||||
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)
|
||||
elif subfunc == 'help':
|
||||
return self._help()
|
||||
else:
|
||||
|
@ -194,13 +194,15 @@ class SubConfig(object):
|
||||
reset_all_cache()
|
||||
|
||||
def cfgimpl_get_home_by_path(self, path, force_permissive=False,
|
||||
returns_raise=False, _setting_properties=undefined):
|
||||
returns_raise=False, _setting_properties=undefined,
|
||||
validate_properties=True):
|
||||
""":returns: tuple (config, name)"""
|
||||
path = path.split('.')
|
||||
for step in path[:-1]:
|
||||
self = self.getattr(step,
|
||||
force_permissive=force_permissive,
|
||||
returns_raise=returns_raise,
|
||||
validate_properties=validate_properties,
|
||||
_setting_properties=_setting_properties)
|
||||
if isinstance(self, Exception):
|
||||
return self, None
|
||||
@ -382,7 +384,8 @@ class SubConfig(object):
|
||||
|
||||
def getattr(self, name, force_permissive=False, validate=True,
|
||||
_setting_properties=undefined, _self_properties=undefined, index=None,
|
||||
returns_raise=False, returns_option=False):
|
||||
returns_raise=False, returns_option=False,
|
||||
validate_properties=True):
|
||||
"""
|
||||
attribute notation mechanism for accessing the value of an option
|
||||
:param name: attribute name
|
||||
@ -397,6 +400,7 @@ class SubConfig(object):
|
||||
if '.' in name:
|
||||
homeconfig, name = self.cfgimpl_get_home_by_path(
|
||||
name, force_permissive=force_permissive,
|
||||
validate_properties=validate_properties,
|
||||
returns_raise=returns_raise, _setting_properties=_setting_properties)
|
||||
if isinstance(homeconfig, Exception):
|
||||
cfg = homeconfig
|
||||
@ -405,7 +409,8 @@ class SubConfig(object):
|
||||
validate=validate,
|
||||
_setting_properties=_setting_properties,
|
||||
_self_properties=_self_properties,
|
||||
index=index, returns_raise=returns_raise)
|
||||
index=index, returns_raise=returns_raise,
|
||||
validate_properties=validate_properties)
|
||||
else:
|
||||
option = self.cfgimpl_get_description().__getattr__(name,
|
||||
context=context)
|
||||
@ -417,6 +422,7 @@ class SubConfig(object):
|
||||
force_permissive=force_permissive,
|
||||
setting_properties=_setting_properties,
|
||||
self_properties=_self_properties,
|
||||
validate_properties=validate_properties,
|
||||
index=index)
|
||||
elif option._is_symlinkoption(): # pragma: no dynoptiondescription cover
|
||||
path = context.cfgimpl_get_description().impl_get_path_by_opt(
|
||||
@ -425,18 +431,20 @@ class SubConfig(object):
|
||||
force_permissive=force_permissive,
|
||||
_setting_properties=_setting_properties,
|
||||
_self_properties=_self_properties,
|
||||
validate_properties=validate_properties,
|
||||
index=index, returns_raise=True)
|
||||
elif option.impl_is_optiondescription():
|
||||
props = self.cfgimpl_get_settings().validate_properties(
|
||||
option, True, False, path=subpath,
|
||||
force_permissive=force_permissive,
|
||||
self_properties=_self_properties,
|
||||
setting_properties=_setting_properties)
|
||||
if props:
|
||||
if returns_raise:
|
||||
return props
|
||||
else:
|
||||
raise props
|
||||
if validate_properties:
|
||||
props = self.cfgimpl_get_settings().validate_properties(
|
||||
option, True, False, path=subpath,
|
||||
force_permissive=force_permissive,
|
||||
self_properties=_self_properties,
|
||||
setting_properties=_setting_properties)
|
||||
if props:
|
||||
if returns_raise:
|
||||
return props
|
||||
else:
|
||||
raise props
|
||||
return SubConfig(option, self._impl_context, subpath)
|
||||
else:
|
||||
if validate:
|
||||
@ -454,6 +462,7 @@ class SubConfig(object):
|
||||
force_permissive=force_permissive,
|
||||
setting_properties=_setting_properties,
|
||||
self_properties=_self_properties,
|
||||
validate_properties=validate_properties,
|
||||
index=index)
|
||||
if not returns_raise and isinstance(cfg, Exception):
|
||||
raise cfg
|
||||
@ -727,7 +736,8 @@ class _CommonConfig(SubConfig):
|
||||
return self.cfgimpl_get_values().getowner(opt, index=index,
|
||||
force_permissive=force_permissive)
|
||||
|
||||
def unwrap_from_path(self, path, force_permissive=False, index=None):
|
||||
def unwrap_from_path(self, path, force_permissive=False, index=None,
|
||||
validate_properties=True, validate=True):
|
||||
"""convenience method to extract and Option() object from the Config()
|
||||
and it is **fast**: finds the option directly in the appropriate
|
||||
namespace
|
||||
@ -737,8 +747,14 @@ class _CommonConfig(SubConfig):
|
||||
context = self._cfgimpl_get_context()
|
||||
if '.' in path:
|
||||
self, path = self.cfgimpl_get_home_by_path(path,
|
||||
validate_properties=validate_properties,
|
||||
force_permissive=force_permissive)
|
||||
return self.getattr(path, force_permissive=force_permissive, index=index, returns_option=True)
|
||||
return self.getattr(path,
|
||||
validate_properties=validate_properties,
|
||||
validate=validate,
|
||||
force_permissive=force_permissive,
|
||||
index=index,
|
||||
returns_option=True)
|
||||
|
||||
def cfgimpl_get_path(self, dyn=True):
|
||||
return None
|
||||
|
Reference in New Issue
Block a user