add force_permissive in config's method (iter_all, iter_group, find, find_first and make_dict)
rename _getattr to getattr
This commit is contained in:
parent
712db2875e
commit
cd52729952
|
@ -9,7 +9,7 @@ from py.test import raises
|
|||
from tiramisu.config import Config, SubConfig
|
||||
from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \
|
||||
BoolOption, UnicodeOption, OptionDescription
|
||||
from tiramisu.error import ConflictError, ConfigError
|
||||
from tiramisu.error import ConflictError, ConfigError, PropertiesOptionError
|
||||
import weakref
|
||||
|
||||
|
||||
|
@ -22,7 +22,7 @@ def make_description():
|
|||
intoption = IntOption('int', 'Test int option', default=0)
|
||||
floatoption = FloatOption('float', 'Test float option', default=2.3)
|
||||
stroption = StrOption('str', 'Test string option', default="abc", properties=('mandatory', ))
|
||||
boolop = BoolOption('boolop', 'Test boolean option op', default=True)
|
||||
boolop = BoolOption('boolop', 'Test boolean option op', default=True, properties=('hidden',))
|
||||
wantref_option = BoolOption('wantref', 'Test requires', default=False)
|
||||
wantframework_option = BoolOption('wantframework', 'Test requires',
|
||||
default=False)
|
||||
|
@ -90,6 +90,15 @@ def test_base_config_and_groups():
|
|||
#assert nm._name == 'name'
|
||||
|
||||
|
||||
def test_base_config_force_permissive():
|
||||
descr = make_description()
|
||||
config = Config(descr)
|
||||
config.read_write()
|
||||
config.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
raises(PropertiesOptionError, "config.getattr('boolop')")
|
||||
assert config.getattr('boolop', force_permissive=True) is True
|
||||
|
||||
|
||||
def test_base_config_in_a_tree():
|
||||
"how options are organized into a tree, see :ref:`tree`"
|
||||
descr = make_description()
|
||||
|
|
|
@ -101,9 +101,12 @@ def test_make_dict():
|
|||
"serialization of the whole config to a dict"
|
||||
descr = OptionDescription("opt", "", [
|
||||
OptionDescription("s1", "", [
|
||||
BoolOption("a", "", default=False)]),
|
||||
BoolOption("a", "", default=False),
|
||||
BoolOption("b", "", default=False, properties=('hidden',))]),
|
||||
IntOption("int", "", default=42)])
|
||||
config = Config(descr)
|
||||
config.read_write()
|
||||
config.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
d = config.make_dict()
|
||||
assert d == {"s1.a": False, "int": 42}
|
||||
config.int = 43
|
||||
|
@ -113,6 +116,8 @@ def test_make_dict():
|
|||
d2 = config.make_dict(flatten=True)
|
||||
assert d2 == {'a': True, 'int': 43}
|
||||
raises(ValueError, 'd2 = config.make_dict(withvalue="3")')
|
||||
d = config.make_dict(force_permissive=True)
|
||||
assert d == {"s1.a": True, "s1.b": False, "int": 43}
|
||||
|
||||
|
||||
def test_make_dict_with_disabled():
|
||||
|
@ -132,6 +137,7 @@ def test_find_in_config():
|
|||
descr = make_description()
|
||||
conf = Config(descr)
|
||||
conf.read_only()
|
||||
conf.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
assert conf.find(byname='dummy') == [conf.unwrap_from_path('gc.dummy')]
|
||||
assert conf.find(byname='float') == [conf.unwrap_from_path('gc.float'), conf.unwrap_from_path('float')]
|
||||
assert conf.find_first(byname='bool') == conf.unwrap_from_path('gc.gc2.bool')
|
||||
|
@ -146,6 +152,8 @@ def test_find_in_config():
|
|||
conf.read_write()
|
||||
raises(AttributeError, "assert conf.find(byname='prop')")
|
||||
assert conf.find(byname='prop', check_properties=False) == [conf.unwrap_from_path('gc.gc2.prop'), conf.unwrap_from_path('gc.prop')]
|
||||
assert conf.find(byname='prop', force_permissive=True) == [conf.unwrap_from_path('gc.prop')]
|
||||
assert conf.find_first(byname='prop', force_permissive=True) == conf.unwrap_from_path('gc.prop')
|
||||
#assert conf.find_first(byname='prop') == conf.unwrap_from_path('gc.prop')
|
||||
# combinaison of filters
|
||||
assert conf.find(bytype=BoolOption, byname='dummy') == [conf.unwrap_from_path('gc.dummy')]
|
||||
|
@ -217,6 +225,20 @@ def test_iter_all():
|
|||
break
|
||||
|
||||
|
||||
def test_iter_all_force_permissive():
|
||||
s = StrOption("string", "", default="string")
|
||||
s2 = StrOption("string2", "", default="string2")
|
||||
s3 = StrOption("string3", "", default="string3", properties=('hidden',))
|
||||
descr = OptionDescription("options", "", [s, s2, s3])
|
||||
config = Config(descr)
|
||||
config.read_write()
|
||||
config.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
assert list(config.iter_all()) == [('string', 'string'), ('string2', 'string2')]
|
||||
assert list(config.iter_all(force_permissive=True)) == [('string', 'string'),
|
||||
('string2', 'string2'),
|
||||
('string3', 'string3')]
|
||||
|
||||
|
||||
def test_iter_all_prop():
|
||||
s = StrOption("string", "", default="string", properties=('disabled',))
|
||||
s2 = StrOption("string2", "", default="string2")
|
||||
|
|
|
@ -162,5 +162,5 @@ def test_force_store_value_hidden():
|
|||
conf.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
conf.read_write()
|
||||
assert conf.getowner(conf.unwrap_from_path('wantref2')) == 'default'
|
||||
conf._getattr('wantref2', force_permissive=True)
|
||||
conf.getattr('wantref2', force_permissive=True)
|
||||
assert conf.getowner(conf.unwrap_from_path('wantref2')) == 'user'
|
||||
|
|
|
@ -34,7 +34,9 @@ def make_description():
|
|||
mode_conteneur_actif, adresse_serveur_ntp,
|
||||
time_zone])
|
||||
general.impl_set_group_type(groups.family)
|
||||
creole = OptionDescription('creole', 'first tiramisu configuration', [general, interface1])
|
||||
new = OptionDescription('new', '', [], properties=('hidden',))
|
||||
new.impl_set_group_type(groups.family)
|
||||
creole = OptionDescription('creole', 'first tiramisu configuration', [general, interface1, new])
|
||||
descr = OptionDescription('baseconfig', 'baseconifgdescr', [creole])
|
||||
return descr
|
||||
|
||||
|
@ -99,6 +101,17 @@ def test_iter_on_groups():
|
|||
break
|
||||
|
||||
|
||||
def test_iter_on_groups_force_permissive():
|
||||
descr = make_description()
|
||||
config = Config(descr)
|
||||
config.read_write()
|
||||
config.cfgimpl_get_settings().setpermissive(('hidden',))
|
||||
result = list(config.creole.iter_groups(group_type=groups.family,
|
||||
force_permissive=True))
|
||||
group_names = [res[0] for res in result]
|
||||
assert group_names == ['general', 'interface1', 'new']
|
||||
|
||||
|
||||
def test_iter_on_groups_props():
|
||||
descr = make_description()
|
||||
config = Config(descr)
|
||||
|
|
|
@ -154,8 +154,8 @@ def carry_out_calculation(option, config, callback, callback_params,
|
|||
).impl_get_path_by_opt(opt)
|
||||
# get value
|
||||
try:
|
||||
value = config._getattr(path, force_permissive=True,
|
||||
validate=False)
|
||||
value = config.getattr(path, force_permissive=True,
|
||||
validate=False)
|
||||
# convert to list, not modifie this multi
|
||||
if value.__class__.__name__ == 'Multi':
|
||||
value = list(value)
|
||||
|
|
|
@ -67,9 +67,9 @@ class SubConfig(object):
|
|||
""":returns: tuple (config, name)"""
|
||||
path = path.split('.')
|
||||
for step in path[:-1]:
|
||||
self = self._getattr(step,
|
||||
force_permissive=force_permissive,
|
||||
force_properties=force_properties)
|
||||
self = self.getattr(step,
|
||||
force_permissive=force_permissive,
|
||||
force_properties=force_properties)
|
||||
return self, path[-1]
|
||||
|
||||
def __hash__(self):
|
||||
|
@ -101,18 +101,19 @@ class SubConfig(object):
|
|||
except PropertiesOptionError:
|
||||
pass # option with properties
|
||||
|
||||
def iter_all(self):
|
||||
def iter_all(self, force_permissive=False):
|
||||
"""A way of parsing options **and** groups.
|
||||
iteration on Options and OptionDescriptions."""
|
||||
for child in self.cfgimpl_get_description().impl_getchildren():
|
||||
try:
|
||||
yield child._name, getattr(self, child._name)
|
||||
yield child._name, self.getattr(child._name,
|
||||
force_permissive=force_permissive)
|
||||
except GeneratorExit:
|
||||
raise StopIteration
|
||||
except PropertiesOptionError:
|
||||
pass # option with properties
|
||||
|
||||
def iter_groups(self, group_type=None):
|
||||
def iter_groups(self, group_type=None, force_permissive=False):
|
||||
"""iteration on groups objects only.
|
||||
All groups are returned if `group_type` is `None`, otherwise the groups
|
||||
can be filtered by categories (families, or whatever).
|
||||
|
@ -130,7 +131,8 @@ class SubConfig(object):
|
|||
if group_type is None or (group_type is not None and
|
||||
child.impl_get_group_type()
|
||||
== group_type):
|
||||
yield child._name, getattr(self, child._name)
|
||||
yield child._name, self.getattr(child._name,
|
||||
force_permissive=force_permissive)
|
||||
except GeneratorExit:
|
||||
raise StopIteration
|
||||
except PropertiesOptionError:
|
||||
|
@ -210,10 +212,15 @@ class SubConfig(object):
|
|||
self.cfgimpl_get_values().__delitem__(child)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return self._getattr(name)
|
||||
return self.getattr(name)
|
||||
|
||||
def _getattr(self, name, force_permissive=False, force_properties=None,
|
||||
validate=True):
|
||||
def _getattr(self, name):
|
||||
"""use getattr instead of _getattr
|
||||
"""
|
||||
return self.getattr(name)
|
||||
|
||||
def getattr(self, name, force_permissive=False, force_properties=None,
|
||||
validate=True):
|
||||
"""
|
||||
attribute notation mechanism for accessing the value of an option
|
||||
:param name: attribute name
|
||||
|
@ -226,9 +233,9 @@ class SubConfig(object):
|
|||
homeconfig, name = self.cfgimpl_get_home_by_path(
|
||||
name, force_permissive=force_permissive,
|
||||
force_properties=force_properties)
|
||||
return homeconfig._getattr(name, force_permissive=force_permissive,
|
||||
force_properties=force_properties,
|
||||
validate=validate)
|
||||
return homeconfig.getattr(name, force_permissive=force_permissive,
|
||||
force_properties=force_properties,
|
||||
validate=validate)
|
||||
opt_or_descr = getattr(self.cfgimpl_get_description(), name)
|
||||
if self._impl_path is None:
|
||||
subpath = name
|
||||
|
@ -239,9 +246,9 @@ class SubConfig(object):
|
|||
context = self._cfgimpl_get_context()
|
||||
path = context.cfgimpl_get_description().impl_get_path_by_opt(
|
||||
opt_or_descr._opt)
|
||||
return context._getattr(path, validate=validate,
|
||||
force_properties=force_properties,
|
||||
force_permissive=force_permissive)
|
||||
return context.getattr(path, validate=validate,
|
||||
force_properties=force_properties,
|
||||
force_permissive=force_permissive)
|
||||
elif isinstance(opt_or_descr, OptionDescription):
|
||||
self.cfgimpl_get_settings().validate_properties(
|
||||
opt_or_descr, True, False, path=subpath,
|
||||
|
@ -256,7 +263,7 @@ class SubConfig(object):
|
|||
force_permissive=force_permissive)
|
||||
|
||||
def find(self, bytype=None, byname=None, byvalue=None, type_='option',
|
||||
check_properties=True):
|
||||
check_properties=True, force_permissive=False):
|
||||
"""
|
||||
finds a list of options recursively in the config
|
||||
|
||||
|
@ -269,10 +276,12 @@ class SubConfig(object):
|
|||
first=False,
|
||||
type_=type_,
|
||||
_subpath=self.cfgimpl_get_path(),
|
||||
check_properties=check_properties)
|
||||
check_properties=check_properties,
|
||||
force_permissive=force_permissive)
|
||||
|
||||
def find_first(self, bytype=None, byname=None, byvalue=None,
|
||||
type_='option', display_error=True, check_properties=True):
|
||||
type_='option', display_error=True, check_properties=True,
|
||||
force_permissive=False):
|
||||
"""
|
||||
finds an option recursively in the config
|
||||
|
||||
|
@ -284,10 +293,12 @@ class SubConfig(object):
|
|||
return self._cfgimpl_get_context()._find(
|
||||
bytype, byname, byvalue, first=True, type_=type_,
|
||||
_subpath=self.cfgimpl_get_path(), display_error=display_error,
|
||||
check_properties=check_properties)
|
||||
check_properties=check_properties,
|
||||
force_permissive=force_permissive)
|
||||
|
||||
def _find(self, bytype, byname, byvalue, first, type_='option',
|
||||
_subpath=None, check_properties=True, display_error=True):
|
||||
_subpath=None, check_properties=True, display_error=True,
|
||||
force_permissive=False):
|
||||
"""
|
||||
convenience method for finding an option that lives only in the subtree
|
||||
|
||||
|
@ -304,7 +315,7 @@ class SubConfig(object):
|
|||
if byvalue is None:
|
||||
return True
|
||||
try:
|
||||
value = getattr(self, path)
|
||||
value = self.getattr(path, force_permissive=force_permissive)
|
||||
if isinstance(value, Multi):
|
||||
return byvalue in value
|
||||
else:
|
||||
|
@ -341,7 +352,8 @@ class SubConfig(object):
|
|||
#remove option with propertyerror, ...
|
||||
if byvalue is None and check_properties:
|
||||
try:
|
||||
value = getattr(self, path)
|
||||
value = self.getattr(path,
|
||||
force_permissive=force_permissive)
|
||||
except PropertiesOptionError:
|
||||
# a property restricts the access of the value
|
||||
continue
|
||||
|
@ -369,7 +381,7 @@ class SubConfig(object):
|
|||
return find_results
|
||||
|
||||
def make_dict(self, flatten=False, _currpath=None, withoption=None,
|
||||
withvalue=None):
|
||||
withvalue=None, force_permissive=False):
|
||||
"""exports the whole config into a `dict`, for example:
|
||||
|
||||
>>> print cfg.make_dict()
|
||||
|
@ -419,7 +431,8 @@ class SubConfig(object):
|
|||
byvalue=withvalue,
|
||||
first=False,
|
||||
type_='path',
|
||||
_subpath=mypath):
|
||||
_subpath=mypath,
|
||||
force_permissive=force_permissive):
|
||||
path = '.'.join(path.split('.')[:-1])
|
||||
opt = self._cfgimpl_get_context().cfgimpl_get_description(
|
||||
).impl_get_opt_by_path(path)
|
||||
|
@ -435,25 +448,31 @@ class SubConfig(object):
|
|||
'should start with {1}'
|
||||
'').format(path, mypath))
|
||||
path = path[len(tmypath):]
|
||||
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
|
||||
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
|
||||
force_permissive=force_permissive)
|
||||
#withoption can be set to None below !
|
||||
if withoption is None:
|
||||
for opt in self.cfgimpl_get_description().impl_getchildren():
|
||||
path = opt._name
|
||||
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
|
||||
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
|
||||
force_permissive=force_permissive)
|
||||
if _currpath == []:
|
||||
options = dict(pathsvalues)
|
||||
return options
|
||||
return pathsvalues
|
||||
|
||||
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
|
||||
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten,
|
||||
force_permissive=False):
|
||||
try:
|
||||
if isinstance(opt, OptionDescription):
|
||||
pathsvalues += getattr(self, path).make_dict(flatten,
|
||||
_currpath +
|
||||
path.split('.'))
|
||||
pathsvalues += self.getattr(path,
|
||||
force_permissive=force_permissive).make_dict(
|
||||
flatten,
|
||||
_currpath + path.split('.'),
|
||||
force_permissive=force_permissive)
|
||||
else:
|
||||
value = self._getattr(opt._name)
|
||||
value = self.getattr(opt._name,
|
||||
force_permissive=force_permissive)
|
||||
if flatten:
|
||||
name = opt._name
|
||||
else:
|
||||
|
|
|
@ -364,7 +364,7 @@ class Option(BaseOption):
|
|||
else:
|
||||
#if context, calculate value, otherwise get default value
|
||||
if context is not None:
|
||||
opt_value = context._getattr(
|
||||
opt_value = context.getattr(
|
||||
descr.impl_get_path_by_opt(opt), validate=False,
|
||||
force_permissive=True)
|
||||
else:
|
||||
|
|
|
@ -603,7 +603,7 @@ class Settings(object):
|
|||
" '{0}' with requirement on: "
|
||||
"'{1}'").format(path, reqpath))
|
||||
try:
|
||||
value = context._getattr(reqpath, force_permissive=True)
|
||||
value = context.getattr(reqpath, force_permissive=True)
|
||||
except PropertiesOptionError as err:
|
||||
if not transitive:
|
||||
continue
|
||||
|
|
|
@ -203,7 +203,7 @@ class Values(object):
|
|||
if (opt.impl_is_multi() and
|
||||
opt.impl_get_multitype() == multitypes.slave):
|
||||
masterp = self._get_opt_path(opt.impl_get_master_slaves())
|
||||
mastervalue = context._getattr(masterp, validate=validate)
|
||||
mastervalue = context.getattr(masterp, validate=validate)
|
||||
lenmaster = len(mastervalue)
|
||||
if lenmaster == 0:
|
||||
value = []
|
||||
|
@ -407,8 +407,8 @@ class Values(object):
|
|||
for path in context.cfgimpl_get_description().impl_getpaths(
|
||||
include_groups=True):
|
||||
try:
|
||||
context._getattr(path,
|
||||
force_properties=frozenset(('mandatory',)))
|
||||
context.getattr(path,
|
||||
force_properties=frozenset(('mandatory',)))
|
||||
except PropertiesOptionError as err:
|
||||
if err.proptype == ['mandatory']:
|
||||
yield path
|
||||
|
@ -426,7 +426,7 @@ class Values(object):
|
|||
for path in context.cfgimpl_get_description().impl_getpaths(
|
||||
include_groups=True):
|
||||
try:
|
||||
context._getattr(path)
|
||||
context.getattr(path)
|
||||
except PropertiesOptionError:
|
||||
pass
|
||||
|
||||
|
@ -490,7 +490,7 @@ class Multi(list):
|
|||
values = context.cfgimpl_get_values()
|
||||
masterp = context.cfgimpl_get_description().impl_get_path_by_opt(
|
||||
self.opt.impl_get_master_slaves())
|
||||
mastervalue = context._getattr(masterp, validate=False)
|
||||
mastervalue = context.getattr(masterp, validate=False)
|
||||
masterlen = len(mastervalue)
|
||||
valuelen = len(value)
|
||||
if valuelen > masterlen or (valuelen < masterlen and setitem):
|
||||
|
|
Loading…
Reference in New Issue