Merge branch 'force_permissive'

Conflicts:
	test/test_freeze.py
This commit is contained in:
Emmanuel Garette 2014-03-31 22:38:56 +02:00
commit a3c5e6034f
9 changed files with 109 additions and 46 deletions

View file

@ -9,7 +9,7 @@ from py.test import raises
from tiramisu.config import Config, SubConfig from tiramisu.config import Config, SubConfig
from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \ from tiramisu.option import IntOption, FloatOption, StrOption, ChoiceOption, \
BoolOption, UnicodeOption, OptionDescription BoolOption, UnicodeOption, OptionDescription
from tiramisu.error import ConflictError, ConfigError from tiramisu.error import ConflictError, ConfigError, PropertiesOptionError
import weakref import weakref
@ -22,7 +22,7 @@ def make_description():
intoption = IntOption('int', 'Test int option', default=0) intoption = IntOption('int', 'Test int option', default=0)
floatoption = FloatOption('float', 'Test float option', default=2.3) floatoption = FloatOption('float', 'Test float option', default=2.3)
stroption = StrOption('str', 'Test string option', default="abc", properties=('mandatory', )) 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) wantref_option = BoolOption('wantref', 'Test requires', default=False)
wantframework_option = BoolOption('wantframework', 'Test requires', wantframework_option = BoolOption('wantframework', 'Test requires',
default=False) default=False)
@ -90,6 +90,15 @@ def test_base_config_and_groups():
#assert nm._name == 'name' #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(): def test_base_config_in_a_tree():
"how options are organized into a tree, see :ref:`tree`" "how options are organized into a tree, see :ref:`tree`"
descr = make_description() descr = make_description()

View file

@ -101,9 +101,12 @@ def test_make_dict():
"serialization of the whole config to a dict" "serialization of the whole config to a dict"
descr = OptionDescription("opt", "", [ descr = OptionDescription("opt", "", [
OptionDescription("s1", "", [ OptionDescription("s1", "", [
BoolOption("a", "", default=False)]), BoolOption("a", "", default=False),
BoolOption("b", "", default=False, properties=('hidden',))]),
IntOption("int", "", default=42)]) IntOption("int", "", default=42)])
config = Config(descr) config = Config(descr)
config.read_write()
config.cfgimpl_get_settings().setpermissive(('hidden',))
d = config.make_dict() d = config.make_dict()
assert d == {"s1.a": False, "int": 42} assert d == {"s1.a": False, "int": 42}
config.int = 43 config.int = 43
@ -113,6 +116,8 @@ def test_make_dict():
d2 = config.make_dict(flatten=True) d2 = config.make_dict(flatten=True)
assert d2 == {'a': True, 'int': 43} assert d2 == {'a': True, 'int': 43}
raises(ValueError, 'd2 = config.make_dict(withvalue="3")') 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(): def test_make_dict_with_disabled():
@ -132,6 +137,7 @@ def test_find_in_config():
descr = make_description() descr = make_description()
conf = Config(descr) conf = Config(descr)
conf.read_only() conf.read_only()
conf.cfgimpl_get_settings().setpermissive(('hidden',))
assert conf.find(byname='dummy') == [conf.unwrap_from_path('gc.dummy')] 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(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') 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() conf.read_write()
raises(AttributeError, "assert conf.find(byname='prop')") 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', 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') #assert conf.find_first(byname='prop') == conf.unwrap_from_path('gc.prop')
# combinaison of filters # combinaison of filters
assert conf.find(bytype=BoolOption, byname='dummy') == [conf.unwrap_from_path('gc.dummy')] assert conf.find(bytype=BoolOption, byname='dummy') == [conf.unwrap_from_path('gc.dummy')]
@ -217,6 +225,20 @@ def test_iter_all():
break 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(): def test_iter_all_prop():
s = StrOption("string", "", default="string", properties=('disabled',)) s = StrOption("string", "", default="string", properties=('disabled',))
s2 = StrOption("string2", "", default="string2") s2 = StrOption("string2", "", default="string2")

View file

@ -171,7 +171,7 @@ def test_force_store_value_hidden():
conf.cfgimpl_get_settings().setpermissive(('hidden',)) conf.cfgimpl_get_settings().setpermissive(('hidden',))
conf.read_write() conf.read_write()
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {} assert conf.cfgimpl_get_values()._p_.get_modified_values() == {}
conf._getattr('wantref2', force_permissive=True) conf.getattr('wantref2', force_permissive=True)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'wantref2': ('user', False)} assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'wantref2': ('user', False)}

View file

@ -34,7 +34,9 @@ def make_description():
mode_conteneur_actif, adresse_serveur_ntp, mode_conteneur_actif, adresse_serveur_ntp,
time_zone]) time_zone])
general.impl_set_group_type(groups.family) 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]) descr = OptionDescription('baseconfig', 'baseconifgdescr', [creole])
return descr return descr
@ -99,6 +101,17 @@ def test_iter_on_groups():
break 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(): def test_iter_on_groups_props():
descr = make_description() descr = make_description()
config = Config(descr) config = Config(descr)

View file

@ -154,8 +154,8 @@ def carry_out_calculation(option, config, callback, callback_params,
).impl_get_path_by_opt(opt) ).impl_get_path_by_opt(opt)
# get value # get value
try: try:
value = config._getattr(path, force_permissive=True, value = config.getattr(path, force_permissive=True,
validate=False) validate=False)
# convert to list, not modifie this multi # convert to list, not modifie this multi
if value.__class__.__name__ == 'Multi': if value.__class__.__name__ == 'Multi':
value = list(value) value = list(value)

View file

@ -67,9 +67,9 @@ class SubConfig(object):
""":returns: tuple (config, name)""" """:returns: tuple (config, name)"""
path = path.split('.') path = path.split('.')
for step in path[:-1]: for step in path[:-1]:
self = self._getattr(step, self = self.getattr(step,
force_permissive=force_permissive, force_permissive=force_permissive,
force_properties=force_properties) force_properties=force_properties)
return self, path[-1] return self, path[-1]
def __hash__(self): def __hash__(self):
@ -101,18 +101,19 @@ class SubConfig(object):
except PropertiesOptionError: except PropertiesOptionError:
pass # option with properties pass # option with properties
def iter_all(self): def iter_all(self, force_permissive=False):
"""A way of parsing options **and** groups. """A way of parsing options **and** groups.
iteration on Options and OptionDescriptions.""" iteration on Options and OptionDescriptions."""
for child in self.cfgimpl_get_description().impl_getchildren(): for child in self.cfgimpl_get_description().impl_getchildren():
try: try:
yield child._name, getattr(self, child._name) yield child._name, self.getattr(child._name,
force_permissive=force_permissive)
except GeneratorExit: except GeneratorExit:
raise StopIteration raise StopIteration
except PropertiesOptionError: except PropertiesOptionError:
pass # option with properties 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. """iteration on groups objects only.
All groups are returned if `group_type` is `None`, otherwise the groups All groups are returned if `group_type` is `None`, otherwise the groups
can be filtered by categories (families, or whatever). 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 if group_type is None or (group_type is not None and
child.impl_get_group_type() child.impl_get_group_type()
== group_type): == group_type):
yield child._name, getattr(self, child._name) yield child._name, self.getattr(child._name,
force_permissive=force_permissive)
except GeneratorExit: except GeneratorExit:
raise StopIteration raise StopIteration
except PropertiesOptionError: except PropertiesOptionError:
@ -210,10 +212,15 @@ class SubConfig(object):
self.cfgimpl_get_values().__delitem__(child) self.cfgimpl_get_values().__delitem__(child)
def __getattr__(self, name): def __getattr__(self, name):
return self._getattr(name) return self.getattr(name)
def _getattr(self, name, force_permissive=False, force_properties=None, def _getattr(self, name):
validate=True): """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 attribute notation mechanism for accessing the value of an option
:param name: attribute name :param name: attribute name
@ -226,9 +233,9 @@ class SubConfig(object):
homeconfig, name = self.cfgimpl_get_home_by_path( homeconfig, name = self.cfgimpl_get_home_by_path(
name, force_permissive=force_permissive, name, force_permissive=force_permissive,
force_properties=force_properties) force_properties=force_properties)
return homeconfig._getattr(name, force_permissive=force_permissive, return homeconfig.getattr(name, force_permissive=force_permissive,
force_properties=force_properties, force_properties=force_properties,
validate=validate) validate=validate)
opt_or_descr = getattr(self.cfgimpl_get_description(), name) opt_or_descr = getattr(self.cfgimpl_get_description(), name)
if self._impl_path is None: if self._impl_path is None:
subpath = name subpath = name
@ -239,9 +246,9 @@ class SubConfig(object):
context = self._cfgimpl_get_context() context = self._cfgimpl_get_context()
path = context.cfgimpl_get_description().impl_get_path_by_opt( path = context.cfgimpl_get_description().impl_get_path_by_opt(
opt_or_descr._opt) opt_or_descr._opt)
return context._getattr(path, validate=validate, return context.getattr(path, validate=validate,
force_properties=force_properties, force_properties=force_properties,
force_permissive=force_permissive) force_permissive=force_permissive)
elif isinstance(opt_or_descr, OptionDescription): elif isinstance(opt_or_descr, OptionDescription):
self.cfgimpl_get_settings().validate_properties( self.cfgimpl_get_settings().validate_properties(
opt_or_descr, True, False, path=subpath, opt_or_descr, True, False, path=subpath,
@ -256,7 +263,7 @@ class SubConfig(object):
force_permissive=force_permissive) force_permissive=force_permissive)
def find(self, bytype=None, byname=None, byvalue=None, type_='option', 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 finds a list of options recursively in the config
@ -269,10 +276,12 @@ class SubConfig(object):
first=False, first=False,
type_=type_, type_=type_,
_subpath=self.cfgimpl_get_path(), _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, 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 finds an option recursively in the config
@ -284,10 +293,12 @@ class SubConfig(object):
return self._cfgimpl_get_context()._find( return self._cfgimpl_get_context()._find(
bytype, byname, byvalue, first=True, type_=type_, bytype, byname, byvalue, first=True, type_=type_,
_subpath=self.cfgimpl_get_path(), display_error=display_error, _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', 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 convenience method for finding an option that lives only in the subtree
@ -304,7 +315,7 @@ class SubConfig(object):
if byvalue is None: if byvalue is None:
return True return True
try: try:
value = getattr(self, path) value = self.getattr(path, force_permissive=force_permissive)
if isinstance(value, Multi): if isinstance(value, Multi):
return byvalue in value return byvalue in value
else: else:
@ -341,7 +352,8 @@ class SubConfig(object):
#remove option with propertyerror, ... #remove option with propertyerror, ...
if byvalue is None and check_properties: if byvalue is None and check_properties:
try: try:
value = getattr(self, path) value = self.getattr(path,
force_permissive=force_permissive)
except PropertiesOptionError: except PropertiesOptionError:
# a property restricts the access of the value # a property restricts the access of the value
continue continue
@ -369,7 +381,7 @@ class SubConfig(object):
return find_results return find_results
def make_dict(self, flatten=False, _currpath=None, withoption=None, 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: """exports the whole config into a `dict`, for example:
>>> print cfg.make_dict() >>> print cfg.make_dict()
@ -419,7 +431,8 @@ class SubConfig(object):
byvalue=withvalue, byvalue=withvalue,
first=False, first=False,
type_='path', type_='path',
_subpath=mypath): _subpath=mypath,
force_permissive=force_permissive):
path = '.'.join(path.split('.')[:-1]) path = '.'.join(path.split('.')[:-1])
opt = self._cfgimpl_get_context().cfgimpl_get_description( opt = self._cfgimpl_get_context().cfgimpl_get_description(
).impl_get_opt_by_path(path) ).impl_get_opt_by_path(path)
@ -435,25 +448,31 @@ class SubConfig(object):
'should start with {1}' 'should start with {1}'
'').format(path, mypath)) '').format(path, mypath))
path = path[len(tmypath):] 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 ! #withoption can be set to None below !
if withoption is None: if withoption is None:
for opt in self.cfgimpl_get_description().impl_getchildren(): for opt in self.cfgimpl_get_description().impl_getchildren():
path = opt._name 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 == []: if _currpath == []:
options = dict(pathsvalues) options = dict(pathsvalues)
return options return options
return pathsvalues 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: try:
if isinstance(opt, OptionDescription): if isinstance(opt, OptionDescription):
pathsvalues += getattr(self, path).make_dict(flatten, pathsvalues += self.getattr(path,
_currpath + force_permissive=force_permissive).make_dict(
path.split('.')) flatten,
_currpath + path.split('.'),
force_permissive=force_permissive)
else: else:
value = self._getattr(opt._name) value = self.getattr(opt._name,
force_permissive=force_permissive)
if flatten: if flatten:
name = opt._name name = opt._name
else: else:

View file

@ -364,7 +364,7 @@ class Option(BaseOption):
else: else:
#if context, calculate value, otherwise get default value #if context, calculate value, otherwise get default value
if context is not None: if context is not None:
opt_value = context._getattr( opt_value = context.getattr(
descr.impl_get_path_by_opt(opt), validate=False, descr.impl_get_path_by_opt(opt), validate=False,
force_permissive=True) force_permissive=True)
else: else:

View file

@ -607,7 +607,7 @@ class Settings(object):
" '{0}' with requirement on: " " '{0}' with requirement on: "
"'{1}'").format(path, reqpath)) "'{1}'").format(path, reqpath))
try: try:
value = context._getattr(reqpath, force_permissive=True) value = context.getattr(reqpath, force_permissive=True)
except PropertiesOptionError as err: except PropertiesOptionError as err:
if not transitive: if not transitive:
continue continue

View file

@ -208,7 +208,7 @@ class Values(object):
if (opt.impl_is_multi() and if (opt.impl_is_multi() and
opt.impl_get_multitype() == multitypes.slave): opt.impl_get_multitype() == multitypes.slave):
masterp = self._get_opt_path(opt.impl_get_master_slaves()) 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) lenmaster = len(mastervalue)
if lenmaster == 0: if lenmaster == 0:
value = [] value = []
@ -417,8 +417,8 @@ class Values(object):
for path in context.cfgimpl_get_description().impl_getpaths( for path in context.cfgimpl_get_description().impl_getpaths(
include_groups=True): include_groups=True):
try: try:
context._getattr(path, context.getattr(path,
force_properties=frozenset(('mandatory',))) force_properties=frozenset(('mandatory',)))
except PropertiesOptionError as err: except PropertiesOptionError as err:
if err.proptype == ['mandatory']: if err.proptype == ['mandatory']:
yield path yield path
@ -436,7 +436,7 @@ class Values(object):
for path in context.cfgimpl_get_description().impl_getpaths( for path in context.cfgimpl_get_description().impl_getpaths(
include_groups=True): include_groups=True):
try: try:
context._getattr(path) context.getattr(path)
except PropertiesOptionError: except PropertiesOptionError:
pass pass
@ -500,7 +500,7 @@ class Multi(list):
values = context.cfgimpl_get_values() values = context.cfgimpl_get_values()
masterp = context.cfgimpl_get_description().impl_get_path_by_opt( masterp = context.cfgimpl_get_description().impl_get_path_by_opt(
self.opt.impl_get_master_slaves()) self.opt.impl_get_master_slaves())
mastervalue = context._getattr(masterp, validate=False) mastervalue = context.getattr(masterp, validate=False)
masterlen = len(mastervalue) masterlen = len(mastervalue)
valuelen = len(value) valuelen = len(value)
if valuelen > masterlen or (valuelen < masterlen and setitem): if valuelen > masterlen or (valuelen < masterlen and setitem):