improvemnt information
authorEmmanuel Garette <egarette@cadoles.com>
Mon, 30 Nov 2015 14:55:34 +0000 (15:55 +0100)
committerEmmanuel Garette <egarette@cadoles.com>
Mon, 30 Nov 2015 14:55:34 +0000 (15:55 +0100)
test/test_option.py
test/test_requires.py
tiramisu/setting.py
tiramisu/storage/dictionary/option.py
tiramisu/storage/dictionary/value.py

index 90113e0..56ce188 100644 (file)
@@ -7,6 +7,7 @@ do_autopath()
 from py.test import raises
 
 from tiramisu.option import IntOption, OptionDescription
+from tiramisu.config import Config
 
 
 def a_func():
@@ -65,6 +66,7 @@ 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')")
@@ -73,6 +75,38 @@ def test_option_get_information():
     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'
index 3def709..64ac4c9 100644 (file)
@@ -630,3 +630,27 @@ def test_master_slave_requires():
     cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.1.1')
     assert cfg.ip_admin_eth0.netmask_admin_eth0[0] is None
     raises(PropertiesOptionError, "cfg.ip_admin_eth0.netmask_admin_eth0[1]")
+
+
+def test_master_slave_requires_no_master():
+    activate = BoolOption('activate', "Activer l'accès au réseau", True)
+    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,
+                                   requires=[{'option': activate, 'expected': False, 'action': 'disabled'}])
+    interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
+    interface1.impl_set_group_type(groups.master)
+    maconfig = OptionDescription('toto', '', [activate, interface1])
+    cfg = Config(maconfig)
+    cfg.read_write()
+    assert cfg.ip_admin_eth0.netmask_admin_eth0 == []
+    assert cfg.ip_admin_eth0.ip_admin_eth0 == []
+    cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.1.2')
+    assert cfg.ip_admin_eth0.netmask_admin_eth0 == [None]
+    assert cfg.ip_admin_eth0.netmask_admin_eth0[0] is None
+    assert cfg.ip_admin_eth0.ip_admin_eth0 == ['192.168.1.2']
+    cfg.activate = False
+    cfg.ip_admin_eth0.ip_admin_eth0.append('192.168.1.1')
+    assert cfg.ip_admin_eth0.ip_admin_eth0 == ['192.168.1.2', '192.168.1.1']
+    raises(PropertiesOptionError, "cfg.ip_admin_eth0.netmask_admin_eth0")
+    raises(PropertiesOptionError, "cfg.ip_admin_eth0.netmask_admin_eth0[0]")
+    raises(PropertiesOptionError, "cfg.ip_admin_eth0.netmask_admin_eth0[1]")
index ebfab5a..c5e4abd 100644 (file)
@@ -633,9 +633,13 @@ class Settings(object):
                                              " '{0}' with requirement on: "
                                              "'{1}'").format(path, reqpath))
                 try:
+                    if option.impl_is_multi():
+                        idx = index
+                    else:
+                        idx = None
                     value = context.getattr(reqpath, force_permissive=True,
                                             _setting_properties=setting_properties,
-                                            index=index)
+                                            index=idx)
                 except PropertiesOptionError as err:
                     if not transitive:
                         continue
index 4863119..d64bfac 100644 (file)
@@ -107,6 +107,13 @@ class StorageBase(object):
         :param key: information's key (ex: "help", "doc"
         :param value: information's value (ex: "the help string")
         """
+        if self.impl_is_readonly():
+            raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
+                                   " read-only").format(
+                                       self.__class__.__name__,
+                                       self,
+                                       #self.impl_getname(),
+                                       key))
         self._informations[key] = value
 
     def impl_get_information(self, key, default=undefined):
@@ -119,15 +126,18 @@ class StorageBase(object):
         if dico is None or isinstance(dico, str) or isinstance(dico, unicode):
             if key == 'doc':
                 return dico
+            if default is not undefined:
+                return default
             error = True
         elif isinstance(dico, tuple):
             try:
                 return dico[1][dico[0].index(key)]
-            except AttributeError:
+            except ValueError:
                 if default is not undefined:
                     return default
                 error = True
         else:
+            # dict
             if default is not undefined:
                 return self._informations.get(key, default)
             try:
@@ -236,7 +246,7 @@ class StorageBase(object):
                     dico = dico['doc']
                 else:
                     dico = tuple([tuple(dico.keys()), tuple(dico.values())])
-            self._informations = dico
+                self._informations = dico
             try:
                 extra = self._extra
                 self._extra = tuple([tuple(extra.keys()), tuple(extra.values())])
index 6b50054..d7892c6 100644 (file)
@@ -162,9 +162,9 @@ class Values(Cache):
         else:
             if isinstance(self._values[1][idx], list):
                 if index is None:
-                    raise ValueError('list but no index')
+                    raise ValueError('index is mandatory')
             elif index is not None:
-                raise ValueError('index set but not a list')
+                raise ValueError('index is forbidden')
 
             if self._values[1][idx] is None:
                 if index is None: