This commit is contained in:
Emmanuel Garette 2017-02-03 23:39:24 +01:00
parent fe379abb42
commit 722779ebf7
11 changed files with 119 additions and 140 deletions

View File

@ -165,6 +165,9 @@ def test_information_config():
assert config.impl_get_information('info') == string
raises(ValueError, "config.impl_get_information('noinfo')")
assert config.impl_get_information('noinfo', 'default') == 'default'
config.impl_del_information('info')
raises(ValueError, "config.impl_get_information('info')")
raises(ValueError, "config.impl_del_information('noinfo')")
def test_config_impl_get_path_by_opt():

View File

@ -162,6 +162,10 @@ def test_find_in_config():
ret = conf.find(byname='dummy')
assert len(ret) == 1
_is_same_opt(ret[0], conf.unwrap_from_path('gc.dummy'))
#
ret = conf.find_first(byname='dummy')
_is_same_opt(ret, conf.unwrap_from_path('gc.dummy'))
#
ret = conf.find(byname='float')
assert len(ret) == 2
_is_same_opt(ret[0], conf.unwrap_from_path('gc.float'))

View File

@ -61,8 +61,8 @@ def test_duplicate_force_store_value():
descr = make_description()
conf = Config(descr)
conf2 = Config(descr)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'creole.general.wantref': ('forced', False)}
assert conf2.cfgimpl_get_values()._p_.get_modified_values() == {'creole.general.wantref': ('forced', False)}
assert conf.cfgimpl_get_values().get_modified_values() == {'creole.general.wantref': ('forced', False)}
assert conf2.cfgimpl_get_values().get_modified_values() == {'creole.general.wantref': ('forced', False)}
conf.creole.general.wantref = True
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'creole.general.wantref': ('user', True)}
assert conf2.cfgimpl_get_values()._p_.get_modified_values() == {'creole.general.wantref': ('forced', False)}
assert conf.cfgimpl_get_values().get_modified_values() == {'creole.general.wantref': ('user', True)}
assert conf2.cfgimpl_get_values().get_modified_values() == {'creole.general.wantref': ('forced', False)}

View File

@ -646,6 +646,7 @@ def test_find_dyndescription_context():
assert isinstance(cfg.find_first(byname='stval1', type_='option'), DynSymLinkOption)
assert cfg.find(bytype=StrOption, type_='path') == ['od.dodval1.stval1', 'od.dodval2.stval2', 'od.val1']
opts = cfg.find(byvalue='yes')
raises(AttributeError, "cfg.find(byname='strnotexists')")
assert len(opts) == 1
assert isinstance(opts[0], DynSymLinkOption)
assert opts[0].impl_getname() == 'stval1'

View File

@ -170,15 +170,15 @@ def test_freeze_get_multi():
def test_force_store_value():
descr = make_description_freeze()
conf = Config(descr)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'wantref': ('forced', False),
assert conf.cfgimpl_get_values().get_modified_values() == {'wantref': ('forced', False),
'wantref2': ('forced', False),
'wantref3': ('forced', (False,))}
conf.wantref = True
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'wantref': ('user', True),
assert conf.cfgimpl_get_values().get_modified_values() == {'wantref': ('user', True),
'wantref2': ('forced', False),
'wantref3': ('forced', (False,))}
del(conf.wantref)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'wantref': ('forced', False),
assert conf.cfgimpl_get_values().get_modified_values() == {'wantref': ('forced', False),
'wantref2': ('forced', False),
'wantref3': ('forced', (False,))}
@ -206,28 +206,28 @@ def test_force_store_value_masterslaves():
descr = OptionDescription("int", "", [b, c])
descr.impl_set_group_type(groups.master)
conf = Config(descr)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'int': ('forced', ())}
assert conf.cfgimpl_get_values().get_modified_values() == {'int': ('forced', ())}
def test_force_store_value_callback():
b = IntOption('int', 'Test int option', properties=('force_store_value',), callback=return_val)
descr = OptionDescription("int", "", [b])
conf = Config(descr)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'int': ('forced', 1)}
assert conf.cfgimpl_get_values().get_modified_values() == {'int': ('forced', 1)}
def test_force_store_value_callback_params():
b = IntOption('int', 'Test int option', properties=('force_store_value',), callback=return_val2, callback_params={'value': (2,)})
descr = OptionDescription("int", "", [b])
conf = Config(descr)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'int': ('forced', 2)}
assert conf.cfgimpl_get_values().get_modified_values() == {'int': ('forced', 2)}
def test_force_store_value_callback_params_2():
b = IntOption('int', 'Test int option', properties=('force_store_value',), callback=return_val3, callback_params={'': ((None,),), 'value': (2,)})
descr = OptionDescription("int", "", [b])
conf = Config(descr)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'int': ('forced', 2)}
assert conf.cfgimpl_get_values().get_modified_values() == {'int': ('forced', 2)}
def test_force_store_value_callback_params_with_opt():
@ -235,4 +235,4 @@ def test_force_store_value_callback_params_with_opt():
b = IntOption('int', 'Test int option', properties=('force_store_value',), callback=return_val2, callback_params={'value': ((a, False),)})
descr = OptionDescription("int", "", [a, b])
conf = Config(descr)
assert conf.cfgimpl_get_values()._p_.get_modified_values() == {'int': ('forced', 2)}
assert conf.cfgimpl_get_values().get_modified_values() == {'int': ('forced', 2)}

View File

@ -14,38 +14,6 @@ def a_func():
return None
#def test_option_comparison():
# "compare :class:`tiramisu.option.BoolOption`"
# dummy1 = BoolOption('dummy1', 'doc dummy')
# dummy2 = BoolOption('dummy2', 'doc dummy')
# dummy3 = BoolOption('dummy1', 'doc dummy')
# assert dummy1 != dummy2
# assert dummy1 == dummy3
#def test_option_comparison_obj():
# "compare :class:`tiramisu.option.IntOption`"
# dummy1 = BoolOption('dummy1', 'doc dummy')
# dummy2 = IntOption('dummy1', 'doc dummy')
# assert dummy1 != dummy2
#def test_option_comparison_advanced():
# dummy1 = BoolOption('dummy1', 'doc dummy')
# dummy2 = BoolOption('dummy1', 'doc dummy')
# dummy3 = BoolOption('dummy1', 'doc dummy', None)
# dummy4 = BoolOption('dummy1', 'doc dummy', True)
# dummy5 = BoolOption('dummy1', 'doc dummy', multi=True)
# dummy6 = BoolOption('dummy1', 'doc dummy', properties=tuple())
# dummy7 = BoolOption('dummy1', 'doc dummy', properties=tuple('new',))
# assert dummy1 == dummy2
# assert dummy1 == dummy3
# assert dummy1 != dummy4
# assert dummy1 != dummy5
# assert dummy1 == dummy6
# assert dummy1 != dummy7
def test_option_valid_name():
IntOption('test', '')
raises(ValueError, 'IntOption(1, "")')
@ -62,61 +30,61 @@ def test_option_with_callback():
raises(ValueError, "IntOption('test', '', default=1, callback=a_func)")
#def test_option_get_information():
# description = "it's ok"
# string = 'some informations'
# i = IntOption('test', description)
# raises(ValueError, "i.impl_get_information('noinfo')")
# i.impl_set_information('info', string)
def test_option_get_information():
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
raises(ValueError, "i.impl_get_information('noinfo')")
i.impl_set_information('info', string)
assert i.impl_get_information('info') == string
raises(ValueError, "i.impl_get_information('noinfo')")
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
assert i.impl_getdoc() == description
def test_option_get_information_config():
description = "it's ok"
string = 'some informations'
string
i = IntOption('test', description)
od = OptionDescription('od', '', [i])
Config(od)
raises(ValueError, "i.impl_get_information('noinfo')")
raises(AttributeError, "i.impl_set_information('info', string)")
# assert i.impl_get_information('info') == string
# raises(ValueError, "i.impl_get_information('noinfo')")
# assert i.impl_get_information('noinfo', 'default') == 'default'
# assert i.impl_get_information('doc') == description
# assert i.impl_getdoc() == description
#
#
#def test_option_get_information_config():
# description = "it's ok"
# string = 'some informations'
# string
# i = IntOption('test', description)
# od = OptionDescription('od', '', [i])
# Config(od)
# raises(ValueError, "i.impl_get_information('noinfo')")
# raises(AttributeError, "i.impl_set_information('info', string)")
## assert i.impl_get_information('info') == string
# raises(ValueError, "i.impl_get_information('noinfo')")
# assert i.impl_get_information('noinfo', 'default') == 'default'
# assert i.impl_get_information('doc') == description
# assert i.impl_getdoc() == description
#
#
#def test_option_get_information_config2():
# description = "it's ok"
# string = 'some informations'
# i = IntOption('test', description)
# i.impl_set_information('info', string)
# od = OptionDescription('od', '', [i])
# Config(od)
# raises(ValueError, "i.impl_get_information('noinfo')")
# raises(AttributeError, "i.impl_set_information('info', 'hello')")
# assert i.impl_get_information('info') == string
# raises(ValueError, "i.impl_get_information('noinfo')")
# assert i.impl_get_information('noinfo', 'default') == 'default'
# assert i.impl_get_information('doc') == description
# assert i.impl_getdoc() == description
#
#
#def test_optiondescription_get_information():
# description = "it's ok"
# string = 'some informations'
# o = OptionDescription('test', description, [])
# o.impl_set_information('info', string)
# assert o.impl_get_information('info') == string
# raises(ValueError, "o.impl_get_information('noinfo')")
# assert o.impl_get_information('noinfo', 'default') == 'default'
# assert o.impl_get_information('doc') == description
# assert o.impl_getdoc() == description
raises(ValueError, "i.impl_get_information('noinfo')")
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
assert i.impl_getdoc() == description
def test_option_get_information_config2():
description = "it's ok"
string = 'some informations'
i = IntOption('test', description)
i.impl_set_information('info', string)
od = OptionDescription('od', '', [i])
Config(od)
raises(ValueError, "i.impl_get_information('noinfo')")
raises(AttributeError, "i.impl_set_information('info', 'hello')")
assert i.impl_get_information('info') == string
raises(ValueError, "i.impl_get_information('noinfo')")
assert i.impl_get_information('noinfo', 'default') == 'default'
assert i.impl_get_information('doc') == description
assert i.impl_getdoc() == description
def test_optiondescription_get_information():
description = "it's ok"
string = 'some informations'
o = OptionDescription('test', description, [])
o.impl_set_information('info', string)
assert o.impl_get_information('info') == string
raises(ValueError, "o.impl_get_information('noinfo')")
assert o.impl_get_information('noinfo', 'default') == 'default'
assert o.impl_get_information('doc') == description
assert o.impl_getdoc() == description
def test_option_multi():

View File

@ -647,3 +647,19 @@ def test_multi_master_default_slave():
cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.1.1')
cfg.cfgimpl_reset_cache()
assert cfg.ip_admin_eth0.ip_admin_eth0 == ['192.168.1.1']
def test_groups_with_master_get_modified_value():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=True)
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
interface1.impl_set_group_type(groups.master)
maconfig = OptionDescription('toto', '', [interface1])
cfg = Config(maconfig)
cfg.read_write()
assert cfg.cfgimpl_get_values().get_modified_values() == {}
cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.1.1')
cfg.cfgimpl_reset_cache()
assert cfg.cfgimpl_get_values().get_modified_values() == {'ip_admin_eth0.ip_admin_eth0': ('user', ('192.168.1.1',))}
cfg.ip_admin_eth0.netmask_admin_eth0 = ['255.255.255.255']
assert cfg.cfgimpl_get_values().get_modified_values() == {'ip_admin_eth0.ip_admin_eth0': ('user', ('192.168.1.1',)), 'ip_admin_eth0.netmask_admin_eth0': ({'0': 'user'}, {'0': '255.255.255.255'})}

View File

@ -29,6 +29,8 @@ def display_list(lst, separator='and'):
ret = lst[0]
if isinstance(ret, unicode):
ret = ret.encode('utf8')
if not isinstance(ret, str):
ret = str(ret)
return ret
else:
lst_ = []
@ -41,6 +43,8 @@ def display_list(lst, separator='and'):
last = lst[-1]
if isinstance(last, unicode):
last = last.encode('utf8')
if not isinstance(last, str):
last = str(last)
return ', '.join(lst_) + _(' {} ').format(separator) + last

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
""
# Copyright (C) 2014 Team tiramisu (see AUTHORS for all contributors)
# Copyright (C) 2014-2017 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -23,7 +23,7 @@ from ...setting import undefined
from ...error import ConfigError
static_tuple = tuple()
static_set = frozenset()
if sys.version_info[0] >= 3: # pragma: optional cover
if sys.version_info[0] >= 3: # pragma: no cover
xrange = range
@ -286,7 +286,7 @@ class StorageBase(object):
self._opt = opt
def _is_string(self, infos):
if sys.version_info[0] >= 3: # pragma: optional cover
if sys.version_info[0] >= 3: # pragma: no cover
return isinstance(infos, str)
else:
return isinstance(infos, str) or isinstance(infos, unicode)
@ -424,7 +424,7 @@ class StorageOptionDescription(StorageBase):
raise AttributeError(_('no option {0} found').format(opt))
return self._cache_paths[1][self._cache_paths[0].index(opt)]
def impl_get_group_type(self): # pragma: optional cover
def impl_get_group_type(self):
return self._group_type
def impl_build_cache_option(self, _currpath=None, cache_path=None,
@ -467,7 +467,7 @@ class StorageOptionDescription(StorageBase):
if dynopt == subopt:
found = True
break
if not found:
if not found: # pragma: no cover
raise ConfigError(_('cannot find dynpath'))
subpath = subpath + suffix
for slength in xrange(length, len(spath)):
@ -523,7 +523,7 @@ class StorageOptionDescription(StorageBase):
return _filter_by_name(path, option)
opts, paths = self._cache_paths
for index in range(0, len(paths)):
for index in xrange(0, len(paths)):
option = opts[index]
if option.impl_is_optiondescription():
continue
@ -556,7 +556,7 @@ class StorageOptionDescription(StorageBase):
def _getattr(self, name, suffix=undefined, context=undefined, dyn=True):
error = False
if suffix is not undefined:
if undefined in [suffix, context]: # pragma: optional cover
if undefined in [suffix, context]: # pragma: no cover
raise ConfigError(_("suffix and context needed if "
"it's a dyn option"))
if name.endswith(suffix):

View File

@ -35,9 +35,6 @@ class Values(Cache):
def getsession(self):
pass
def delsession(self, session):
pass
def _setvalue_info(self, nb, idx, value, values, index, vidx):
lst = list(self._values[nb])
if idx is None:
@ -82,8 +79,6 @@ class Values(Cache):
if isinstance(value, list):
value = tuple(value)
vidx = self._setvalue_info(2, idx, value, values, index, vidx)
if isinstance(value, list):
value = tuple(value)
self._setvalue_info(3, idx, owner, values, index, vidx)
self._values = tuple(values)
@ -183,32 +178,20 @@ class Values(Cache):
_values == ((path1, path2), ((idx1_1, idx1_2), None), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2))
"""
if path in self._values[0]:
idx = self._values[0].index(path)
if isinstance(self._values[1][idx], tuple):
if index is None:
raise ValueError('index is mandatory')
elif index is not None:
raise ValueError('index is forbidden')
if self._values[1][idx] is None:
if index is None:
value = self._values[nb][idx]
else:
value = self._values[nb][idx][index]
path_idx = self._values[0].index(path)
indexes = self._values[1][path_idx]
if indexes is None:
if index is not None: # pragma: no cover
raise ValueError('index is forbidden')
value = self._values[nb][path_idx]
else:
if index is not None:
if index in self._values[1][idx]:
subidx = self._values[1][idx].index(index)
value = self._values[nb][idx][subidx]
else:
value = undefined
if index is None: # pragma: no cover
raise ValueError('index is mandatory')
if index in indexes:
subidx = indexes.index(index)
value = self._values[nb][path_idx][subidx]
else:
value = []
for i in xrange(0, max(self._values[1][idx])):
if i in self._values[1][idx]:
value.append(self._values[nb][idx][self._values[1][idx].index(i)])
else:
value.append(undefined)
value = undefined
else:
value = undefined
if isinstance(value, tuple):

View File

@ -19,7 +19,7 @@ from ..setting import owners
class SerializeObject(object):
def __getstate__(self):
def __getstate__(self): # pragma: no cover
ret = {}
for key in dir(self):
if not key.startswith('__'):
@ -53,7 +53,7 @@ class Cache(object):
str_owner = []
_value.append(value[2])
for owner in value[3]:
if isinstance(owner, list):
if isinstance(owner, list): # pragma: no cover
str_owners = []
for subowner in owner:
str_owners.append(str(subowner))
@ -64,7 +64,7 @@ class Cache(object):
states[slot] = _value
else:
states[slot] = value
except AttributeError:
except AttributeError: # pragma: no cover
pass
return states
@ -72,7 +72,7 @@ class Cache(object):
def convert_owner(owner):
try:
owner = getattr(owners, owner)
except AttributeError:
except AttributeError: # pragma: no cover
owners.addowner(owner)
owner = getattr(owners, owner)
return owner
@ -86,7 +86,7 @@ class Cache(object):
_value.append(value[2])
obj_owner = []
for owner in value[3]:
if isinstance(owner, list):
if isinstance(owner, list): # pragma: no cover
obj_owners = []
for subowner in owner:
obj_owners.append(convert_owner(subowner))
@ -104,7 +104,7 @@ class Cache(object):
value, created = self._cache[path][index]
if created is None or exp <= created:
return True, value
return False, None
return False, None # pragma: no cover
def hascache(self, path, index):
""" path is in the cache