add sort, reverse, insert and extend in Multi

This commit is contained in:
Emmanuel Garette 2013-06-12 22:48:22 +02:00
parent 9c47eb32d5
commit fd02a954ca
2 changed files with 128 additions and 0 deletions

View File

@ -288,3 +288,99 @@ def test_values_with_master_disabled():
cfg.ip_admin_eth0.netmask_admin_eth0 = ["192.168.230.145"] cfg.ip_admin_eth0.netmask_admin_eth0 = ["192.168.230.145"]
cfg.cfgimpl_get_settings()[netmask_admin_eth0].append('disabled') cfg.cfgimpl_get_settings()[netmask_admin_eth0].append('disabled')
cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.230.43') cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.230.43')
def test_multi_insert():
var = StrOption('var', '', ['ok'], multi=True)
od = OptionDescription('od', '', [var])
c = Config(od)
c.read_write()
assert c.var == ['ok']
assert c.getowner("var") == owners.default
c.var.insert(0, 'nok')
assert c.var == ['nok', 'ok']
assert c.getowner("var") != owners.default
def test_multi_insert_master():
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()
raises(SlaveError, "cfg.ip_admin_eth0.ip_admin_eth0.insert(0, 'nok')")
raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0.insert(0, 'nok')")
def test_multi_sort():
var = StrOption('var', '', ['ok', 'nok'], multi=True)
od = OptionDescription('od', '', [var])
c = Config(od)
c.read_write()
assert c.var == ['ok', 'nok']
assert c.getowner("var") == owners.default
c.var.sort()
assert c.var == ['nok', 'ok']
assert c.getowner("var") != owners.default
def test_multi_sort_master():
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()
raises(SlaveError, "cfg.ip_admin_eth0.ip_admin_eth0.sort()")
raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0.sort()")
def test_multi_reverse():
var = StrOption('var', '', ['ok', 'nok'], multi=True)
od = OptionDescription('od', '', [var])
c = Config(od)
c.read_write()
assert c.var == ['ok', 'nok']
assert c.getowner("var") == owners.default
c.var.reverse()
assert c.var == ['nok', 'ok']
assert c.getowner("var") != owners.default
def test_multi_reverse_master():
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()
raises(SlaveError, "cfg.ip_admin_eth0.ip_admin_eth0.reverse()")
raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0.reverse()")
def test_multi_extend():
var = StrOption('var', '', ['ok', 'nok'], multi=True)
od = OptionDescription('od', '', [var])
c = Config(od)
c.read_write()
assert c.var == ['ok', 'nok']
assert c.getowner("var") == owners.default
c.var.extend(['pok'])
assert c.var == ['ok', 'nok', 'pok']
assert c.getowner("var") != owners.default
def test_multi_extend_master():
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()
raises(SlaveError, "cfg.ip_admin_eth0.ip_admin_eth0.extend(['ok'])")
raises(SlaveError, "cfg.ip_admin_eth0.netmask_admin_eth0.extend(['ok'])")

View File

@ -325,6 +325,38 @@ class Multi(list):
values.getitem(slave, validate_properties=False).append( values.getitem(slave, validate_properties=False).append(
dvalue, force=True) dvalue, force=True)
def sort(self, cmp=None, key=None, reverse=False):
if self.opt.impl_get_multitype() in [multitypes.slave,
multitypes.master]:
raise SlaveError(_("cannot sort multi option {0} if master or slave"
"").format(self.opt._name))
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
def reverse(self):
if self.opt.impl_get_multitype() in [multitypes.slave,
multitypes.master]:
raise SlaveError(_("cannot reverse multi option {0} if master or "
"slave").format(self.opt._name))
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
super(Multi, self).reverse()
def insert(self, index, obj):
if self.opt.impl_get_multitype() in [multitypes.slave,
multitypes.master]:
raise SlaveError(_("cannot insert multi option {0} if master or "
"slave").format(self.opt._name))
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
super(Multi, self).insert(index, obj)
def extend(self, iterable):
if self.opt.impl_get_multitype() in [multitypes.slave,
multitypes.master]:
raise SlaveError(_("cannot extend multi option {0} if master or "
"slave").format(self.opt._name))
self.context.cfgimpl_get_values()._setvalue(self.opt, self)
super(Multi, self).extend(iterable)
def _validate(self, value): def _validate(self, value):
if value is not None and not self.opt._validate(value): if value is not None and not self.opt._validate(value):
raise ValueError(_("invalid value {0} " raise ValueError(_("invalid value {0} "