metaconfig

This commit is contained in:
2018-01-03 21:07:51 +01:00
parent 4d154a89b5
commit 3ff0701ef2
6 changed files with 893 additions and 101 deletions

View File

@ -12,7 +12,7 @@
#
# 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 .config import Config, MetaConfig
from .config import Config, MetaConfig, GroupConfig
from .option import *
from .error import APIError
from .api import getapi
@ -21,6 +21,7 @@ from .setting import owners, undefined
allfuncs = ['MetaConfig',
'GroupConfig',
'Config'
'getapi',
'APIError',

View File

@ -563,6 +563,33 @@ class TiramisuContextValue(TiramisuContext):
def get_modified(self):
return self.config_bag.config.cfgimpl_get_values().get_modified_values()
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)
def reset(self,
path):
self.config_bag.config.reset(path, self.config_bag)
class TiramisuContextOwner(TiramisuContext):
@count
@ -683,22 +710,36 @@ class TiramisuContextOption(TiramisuContext):
raise APIError(_('unknown list type {}').format(type))
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):
spaths = path.split('.')
config = self.config_bag.config
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)
class TiramisuDispatcherOption(TiramisuContextOption):
def __call__(self, path, index=None):
config_bag = self.config_bag.copy()
validate = not config_bag.force_unrestraint
config_bag.validate = validate
config_bag.validate_properties = validate
#config_bag.validate = validate
#config_bag.validate_properties = validate
if not validate:
config_bag.setting_properties = None
subconfig, name = config_bag.config.cfgimpl_get_home_by_path(path,
config_bag)
#opt = config_bag.config.unwrap_from_path(path,
# index,
# config_bag)
#config_bag.option = opt
#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(name,
path,
index,
@ -726,7 +767,7 @@ class TiramisuAPI(object):
return TiramisuAPI(config=self._config,
force_permissive=True,
force_unrestraint=self.force_unrestraint)
elif subfunc in ['unrestraint', 'config']:
elif subfunc == 'unrestraint':
return TiramisuAPI(config=self._config,
force_permissive=self.force_permissive,
force_unrestraint=True)

View File

@ -71,7 +71,8 @@ class SubConfig(object):
raise ValueError('context must be a Weakref')
self._impl_context = context
self._impl_path = subpath
if config_bag.setting_properties is not None and \
if descr is not None and \
config_bag.setting_properties is not None and \
descr.impl_get_group_type() == groups.master:
master = descr.getmaster()
masterpath = master.impl_getname()
@ -161,6 +162,8 @@ class SubConfig(object):
self = self.getattr(step,
None,
sconfig_bag)
if not isinstance(self, SubConfig):
raise AttributeError(_('unknown option {}').format(path[-1]))
return self, path[-1]
# ______________________________________________________________________
@ -300,9 +303,9 @@ class SubConfig(object):
value)
context = self._cfgimpl_get_context()
if '.' in name: # pragma: optional cover
raise Exception('ah non ...')
#self, name = self.cfgimpl_get_home_by_path(name,
# config_bag)
# when set_value
self, name = self.cfgimpl_get_home_by_path(name,
config_bag)
if config_bag.option is None:
config_bag.option = self.cfgimpl_get_description().impl_getchild(name,
config_bag,
@ -567,6 +570,9 @@ class SubConfig(object):
self.unwrap_from_path(path,
None,
config_bag)
self.cfgimpl_get_settings().validate_properties(path,
None,
config_bag)
except PropertiesOptionError:
continue
if type_ == 'value':
@ -962,12 +968,11 @@ class GroupConfig(_CommonConfig):
properties,
permissives)
self._impl_values = Values(self, values)
self._impl_meta = None
super(GroupConfig, self).__init__(_descr,
weakref.ref(self),
undefined,
True,
False)
self._impl_meta = None
ConfigBag(self),
None)
#undocumented option used only in test script
self._impl_test = False
self._impl_name = session_id
@ -993,40 +998,47 @@ class GroupConfig(_CommonConfig):
path=path,
resetted_opts=copy(resetted_opts))
def set_value(self, path, value, _commit=True):
def set_value(self,
path,
index,
value,
config_bag,
only_config=False,
_commit=True):
"""Setattr not in current GroupConfig, but in each children
"""
ret = []
for child in self._impl_children:
if isinstance(child, MetaConfig):
ret.extend(child.set_value(path,
value,
only_config=True,
_commit=False))
elif isinstance(child, GroupConfig):
ret.extend(child.set_value(path,
value,
_commit=False))
else:
childret = child.setattr(path,
value,
not_raises=True,
_commit=False)
if childret is not None:
ret.append(childret)
try:
if isinstance(child, GroupConfig):
ret.extend(child.set_value(path,
index,
value,
config_bag,
only_config=only_config,
_commit=False))
else:
nconfig_bag = config_bag.copy('nooption')
child.setattr(path,
index,
value,
nconfig_bag,
_commit=False)
except (PropertiesOptionError, ValueError, SlaveError) as err:
ret.append(err)
if _commit:
self.cfgimpl_get_values()._p_.commit()
return ret
def find_firsts(self,
config_bag,
byname=None,
bypath=undefined,
byoption=undefined,
byvalue=undefined,
raise_if_not_found=True,
_sub=False,
check_properties=True):
_sub=False):
"""Find first not in current GroupConfig, but in each children
"""
ret = []
@ -1040,19 +1052,21 @@ class GroupConfig(_CommonConfig):
byvalue=undefined,
byname=byname,
first=True,
config_bag=config_bag,
type_='path',
check_properties=None,
raise_if_not_found=raise_if_not_found)
byname = None
byoption = self.cfgimpl_get_description().impl_get_opt_by_path(bypath)
for child in self._impl_children:
nconfig_bag = config_bag.copy('nooption')
nconfig_bag.option = child
if isinstance(child, GroupConfig):
ret.extend(child.find_firsts(byname=byname,
bypath=bypath,
byoption=byoption,
byvalue=byvalue,
check_properties=check_properties,
config_bag=config_bag,
raise_if_not_found=False,
_sub=True))
elif child._find(None,
@ -1060,8 +1074,8 @@ class GroupConfig(_CommonConfig):
byvalue,
first=True,
type_='path',
config_bag=config_bag,
raise_if_not_found=False,
check_properties=check_properties,
only_path=bypath,
only_option=byoption):
ret.append(child)
@ -1071,15 +1085,17 @@ class GroupConfig(_CommonConfig):
return GroupConfig(self._find_return_results(ret,
raise_if_not_found))
def __str__(self):
ret = ''
for child in self._impl_children:
ret += "({0})\n".format(child._impl_name)
if self._impl_descr is not None:
ret += super(GroupConfig, self).__str__()
return ret
__repr__ = __str__
def impl_getname(self):
return self._impl_name
# def __str__(self):
# ret = ''
# for child in self._impl_children:
# ret += "({0})\n".format(child._impl_name)
# if self._impl_descr is not None:
# ret += super(GroupConfig, self).__str__()
# return ret
#
# __repr__ = __str__
def getconfig(self,
name):
@ -1128,7 +1144,9 @@ class MetaConfig(GroupConfig):
def set_value(self,
path,
index,
value,
config_bag,
force_default=False,
force_dont_change_value=False,
force_default_if_same=False,
@ -1143,7 +1161,10 @@ class MetaConfig(GroupConfig):
'force_dont_change_value cannot be set with'
' only_config'))
return super(MetaConfig, self).set_value(path,
index,
value,
config_bag,
only_config=only_config,
_commit=_commit)
ret = []
if force_default or force_default_if_same or force_dont_change_value:
@ -1151,52 +1172,60 @@ class MetaConfig(GroupConfig):
raise ValueError(_('force_default and force_dont_change_value'
' cannot be set together'))
opt = self.cfgimpl_get_description().impl_get_opt_by_path(path)
setting_properties = self.cfgimpl_get_settings()._getproperties(read_write=False)
for child in self._impl_children:
if force_default_if_same or force_default:
if force_default_if_same:
if not child.cfgimpl_get_values()._contains(path):
child_value = undefined
else:
child_value = child.getattr(path)
if force_default or value == child_value:
child.cfgimpl_get_values().reset(opt,
path,
setting_properties,
validate=False,
_commit=False)
continue
nconfig_bag = config_bag.copy('nooption')
nconfig_bag.option = opt
if force_default_if_same:
if not child.cfgimpl_get_values()._p_.hasvalue(path):
child_value = undefined
else:
child_value = child.getattr(path,
None,
nconfig_bag)
if force_default or (force_default_if_same and value == child_value):
child.cfgimpl_get_values().reset(path,
nconfig_bag,
_commit=False)
continue
if force_dont_change_value:
child_value = child.getattr(path,
setting_properties=setting_properties)
if isinstance(child_value, Exception):
ret.append(child_value)
elif value != child_value:
childret = child.setattr(path,
child_value,
_commit=False,
not_raises=True)
if childret is not None: # pragma: no cover
ret.append(childret)
try:
child_value = child.getattr(path,
None,
nconfig_bag)
if value != child_value:
child.setattr(path,
None,
child_value,
nconfig_bag,
_commit=False)
except (PropertiesOptionError, ValueError, SlaveError) as err:
ret.append(err)
self.setattr(path,
value,
_commit=_commit)
nconfig_bag = config_bag.copy('nooption')
try:
self.setattr(path,
index,
value,
nconfig_bag,
_commit=_commit)
except (PropertiesOptionError, ValueError, SlaveError) as err:
ret.append(err)
return ret
def reset(self, path):
def reset(self, path, config_bag):
#FIXME not working with DynSymLinkOption
#FIXME fonctionne avec sous metaconfig ??
opt = self.cfgimpl_get_description().impl_get_opt_by_path(path)
setting_properties = self.cfgimpl_get_settings()._getproperties(read_write=False)
config_bag.option = opt
config_bag.validate = False
for child in self._impl_children:
child.cfgimpl_get_values().reset(opt,
path,
setting_properties,
validate=False,
sconfig_bag = config_bag.copy('nooption')
sconfig_bag.option = opt
child.cfgimpl_get_values().reset(path,
sconfig_bag,
_commit=False)
self.cfgimpl_get_values().reset(opt,
path,
setting_properties,
validate=False)
self.cfgimpl_get_values().reset(path,
config_bag)
def new_config(self,
session_id,

View File

@ -125,7 +125,6 @@ class ConfigBag(object):
'ori_option',
'properties',
'validate',
'validate_properties',
'setting_properties',
'force_permissive',
'force_unrestraint',
@ -136,8 +135,6 @@ class ConfigBag(object):
def __init__(self, config, **kwargs):
self.default = {'force_permissive': False,
'force_unrestraint': False,
'validate': True,
'validate_properties': True,
'display_warnings': True,
'trusted_cached_properties': True,
}
@ -148,6 +145,8 @@ class ConfigBag(object):
setattr(self, key, value)
def __getattr__(self, key):
if key in ['validate', 'validate_properties']:
return not self.force_unrestraint
if key == 'setting_properties':
if self.force_unrestraint:
return None
@ -364,8 +363,7 @@ class Settings(object):
props = self._p_.getproperties(path,
opt.impl_getproperties())
else:
props = meta.cfgimpl_get_settings().getproperties(opt,
path,
props = meta.cfgimpl_get_settings().getproperties(path,
index,
config_bag)
props |= self.apply_requires(path,

View File

@ -437,7 +437,7 @@ class Values(object):
masterp = master.impl_getpath(context)
# slave could be a "meta" only if master hasn't value
if self._p_.hasvalue(masterp,
index=index):
index=None):
return False
return not meta.cfgimpl_get_values().is_default_owner(path,
index,
@ -478,7 +478,7 @@ class Values(object):
path,
index,
config_bag,
validate_meta=undefined,
validate_meta=True,
only_default=False):
"""get owner of an option
"""
@ -508,13 +508,14 @@ class Values(object):
owners.default,
index=index)
if owner is owners.default and validate_meta is not False:
if validate_meta is undefined:
validate_meta = self._is_meta(path,
index,
config_bag=config_bag,
force_owner_is_default=True)
if validate_meta:
owner = owners.meta
meta = context.cfgimpl_get_meta()
if meta is not None and self._is_meta(path,
index,
config_bag):
owner = meta.cfgimpl_get_values()._getowner(path,
index,
config_bag,
only_default=only_default)
return owner
def setowner(self,