add warning ability
[tiramisu.git] / tiramisu / value.py
index ee0bb76..247d273 100644 (file)
 # ____________________________________________________________
 from time import time
 from copy import copy
+import sys
+import weakref
 from tiramisu.error import ConfigError, SlaveError
-from tiramisu.setting import owners, multitypes, expires_time, storage_type
+from tiramisu.setting import owners, multitypes, expires_time
 from tiramisu.autolib import carry_out_calculation
 from tiramisu.i18n import _
 from tiramisu.option import SymLinkOption
@@ -31,7 +33,7 @@ class Values(object):
     but the values are physicaly located here, in `Values`, wich is also
     responsible of a caching utility.
     """
-    __slots__ = ('context', '_p_')
+    __slots__ = ('context', '_warning', '_p_', '__weakref__')
 
     def __init__(self, context, storage):
         """
@@ -40,20 +42,9 @@ class Values(object):
         :param context: the context is the home config's values
 
         """
-        self.context = context
+        self.context = weakref.ref(context)
         # the storage type is dictionary or sqlite3
-        import_lib = 'tiramisu.storage.{0}.value'.format(storage_type)
-        self._p_ = __import__(import_lib, globals(), locals(), ['Values'],
-                              -1).Values(storage)
-
-    def _getkey(self, opt):
-        """depends on the storage utility.
-        typically, the option's path in the parent `Config` or `SubConfig`
-        """
-        if self._p_.key_is_path:
-            return self._get_opt_path(opt)
-        else:
-            return opt
+        self._p_ = storage
 
     def _getdefault(self, opt):
         """
@@ -61,7 +52,7 @@ class Values(object):
 
         :param opt: the `option.Option()` object
         """
-        meta = self.context.cfgimpl_get_meta()
+        meta = self.context().cfgimpl_get_meta()
         if meta is not None:
             value = meta.cfgimpl_get_values()[opt]
         else:
@@ -71,24 +62,23 @@ class Values(object):
         else:
             return value
 
-    def _getvalue(self, opt, validate=True):
+    def _getvalue(self, opt, path, validate=True):
         """actually retrieves the value
 
         :param opt: the `option.Option()` object
         :returns: the option's value (or the default value if not set)
         """
-        key = self._getkey(opt)
-        if not self._p_.hasvalue(key):
-            # if no value
+        if not self._p_.hasvalue(path):
+            # if there is no value
             value = self._getdefault(opt)
             if opt.impl_is_multi():
-                value = Multi(value, self.context, opt, validate)
+                value = Multi(value, self.context, opt, path, validate)
         else:
-            #if value
-            value = self._p_.getvalue(key)
+            # if there is a value
+            value = self._p_.getvalue(path)
             if opt.impl_is_multi() and not isinstance(value, Multi):
                 # load value so don't need to validate if is not a Multi
-                value = Multi(value, self.context, opt, validate=False)
+                value = Multi(value, self.context, opt, path, validate=False)
         return value
 
     def get_modified_values(self):
@@ -101,24 +91,30 @@ class Values(object):
 
         :param opt: the `option.Option()` object
         """
-        return self._p_.hasvalue('value', self._getkey(opt))
+        path = self._get_opt_path(opt)
+        return self._contains(path)
+
+    def _contains(self, path):
+        return self._p_.hasvalue(path)
 
     def __delitem__(self, opt):
         """overrides the builtins `del()` instructions"""
         self.reset(opt)
 
-    def reset(self, opt):
-        key = self._getkey(opt)
-        if self._p_.hasvalue(key):
-            setting = self.context.cfgimpl_get_settings()
-            opt.impl_validate(opt.impl_getdefault(), self.context,
-                              'validator' in setting)
-            self.context.cfgimpl_reset_cache()
+    def reset(self, opt, path=None):
+        if path is None:
+            path = self._get_opt_path(opt)
+        if self._p_.hasvalue(path):
+            setting = self.context().cfgimpl_get_settings()
+            self._warning = opt.impl_validate(opt.impl_getdefault(),
+                                              self.context(),
+                                              'validator' in setting)
+            self.context().cfgimpl_reset_cache()
             if (opt.impl_is_multi() and
                     opt.impl_get_multitype() == multitypes.master):
                 for slave in opt.impl_get_master_slaves():
                     self.reset(slave)
-            self._p_.resetvalue(key)
+            self._p_.resetvalue(path)
 
     def _isempty(self, opt, value):
         "convenience method to know if an option is empty"
@@ -129,7 +125,7 @@ class Values(object):
             return True
         return False
 
-    def _getcallback_value(self, opt, index=None):
+    def _getcallback_value(self, opt, index=None, max_len=None):
         """
         retrieves a value for the options that have a callback
 
@@ -141,112 +137,141 @@ class Values(object):
         callback, callback_params = opt._callback
         if callback_params is None:
             callback_params = {}
-        return carry_out_calculation(opt._name, config=self.context,
+        return carry_out_calculation(opt._name, config=self.context(),
                                      callback=callback,
                                      callback_params=callback_params,
-                                     index=index)
+                                     index=index, max_len=max_len)
 
     def __getitem__(self, opt):
-        "enables us to use the pythonic dictionnary-like access to values"
+        "enables us to use the pythonic dictionary-like access to values"
         return self.getitem(opt)
 
-    def getitem(self, opt, validate=True, force_permissive=False,
+    def getitem(self, opt, path=None, validate=True, force_permissive=False,
                 force_properties=None, validate_properties=True):
+        if path is None:
+            path = self._get_opt_path(opt)
         ntime = None
-        key = self._getkey(opt)
-        if self._p_.hascache('value', self._getkey(opt)):
-            ntime = time()
-            is_cached, value = self._p_.getcache('value', key, ntime)
+        setting = self.context().cfgimpl_get_settings()
+        if 'cache' in setting and self._p_.hascache(path):
+            if 'expire' in setting:
+                ntime = int(time())
+            is_cached, value = self._p_.getcache(path, ntime)
             if is_cached:
                 if opt.impl_is_multi() and not isinstance(value, Multi):
                     #load value so don't need to validate if is not a Multi
-                    value = Multi(value, self.context, opt, validate=False)
+                    value = Multi(value, self.context, opt, path, validate=False)
                 return value
-        val = self._getitem(opt, validate, force_permissive, force_properties,
-                            validate_properties)
-        if 'expire' in self.context.cfgimpl_get_settings() and validate and \
-                validate_properties and force_permissive is False and \
-                force_properties is None:
-            if ntime is None:
-                ntime = time()
-            self._p_.setcache('value', key, val, ntime + expires_time)
+        val = self._getitem(opt, path, validate, force_permissive,
+                            force_properties, validate_properties)
+        if 'cache' in setting and validate and validate_properties and \
+                force_permissive is False and force_properties is None:
+            if 'expire' in setting:
+                if ntime is None:
+                    ntime = int(time())
+                ntime = ntime + expires_time
+            self._p_.setcache(path, val, ntime)
 
         return val
 
-    def _getitem(self, opt, validate, force_permissive, force_properties,
+    def _getitem(self, opt, path, validate, force_permissive, force_properties,
                  validate_properties):
         # options with callbacks
-        setting = self.context.cfgimpl_get_settings()
+        setting = self.context().cfgimpl_get_settings()
         is_frozen = 'frozen' in setting[opt]
+        # For calculating properties, we need value (ie for mandatory value).
+        # If value is calculating with a PropertiesOptionError's option
+        # _getcallback_value raise a ConfigError.
+        # We can not raise ConfigError if this option should raise
+        # PropertiesOptionError too. So we get config_error and raise
+        # ConfigError if properties did not raise.
+        config_error = None
+        force_permissives = None
         # if value is callback and is not set
         # or frozen with force_default_on_freeze
         if opt.impl_has_callback() and (
-                self.is_default_owner(opt) or
+                self._is_default_owner(path) or
                 (is_frozen and 'force_default_on_freeze' in setting[opt])):
+            lenmaster = None
             no_value_slave = False
             if (opt.impl_is_multi() and
                     opt.impl_get_multitype() == multitypes.slave):
                 masterp = self._get_opt_path(opt.impl_get_master_slaves())
-                mastervalue = getattr(self.context, masterp)
+                mastervalue = getattr(self.context(), masterp)
                 lenmaster = len(mastervalue)
                 if lenmaster == 0:
                     value = []
                     no_value_slave = True
 
             if not no_value_slave:
-                value = self._getcallback_value(opt)
-                if (opt.impl_is_multi() and
-                        opt.impl_get_multitype() == multitypes.slave):
-                    if not isinstance(value, list):
-                        value = [value for i in range(lenmaster)]
-            if opt.impl_is_multi():
-                value = Multi(value, self.context, opt, validate)
-            # suppress value if already set
-            self.reset(opt)
+                try:
+                    value = self._getcallback_value(opt, max_len=lenmaster)
+                except ConfigError as err:
+                    # cannot assign config_err directly in python 3.3
+                    config_error = err
+                    value = None
+                    # should not raise PropertiesOptionError if option is
+                    # mandatory
+                    force_permissives = set(['mandatory'])
+                else:
+                    if (opt.impl_is_multi() and
+                            opt.impl_get_multitype() == multitypes.slave):
+                        if not isinstance(value, list):
+                            value = [value for i in range(lenmaster)]
+            if config_error is None:
+                if opt.impl_is_multi():
+                    value = Multi(value, self.context, opt, path, validate)
+                # suppress value if already set
+                self.reset(opt, path)
         # frozen and force default
         elif is_frozen and 'force_default_on_freeze' in setting[opt]:
             value = self._getdefault(opt)
             if opt.impl_is_multi():
-                value = Multi(value, self.context, opt, validate)
+                value = Multi(value, self.context, opt, path, validate)
         else:
-            value = self._getvalue(opt, validate)
-        if validate:
-            opt.impl_validate(value, self.context, 'validator' in setting)
-        if self.is_default_owner(opt) and \
+            value = self._getvalue(opt, path, validate)
+        if config_error is None and validate:
+            self._warning = opt.impl_validate(value, self.context(),
+                                              'validator' in setting)
+        if config_error is None and self._is_default_owner(path) and \
                 'force_store_value' in setting[opt]:
-            self.setitem(opt, value, is_write=False)
+            self.setitem(opt, value, path, is_write=False)
         if validate_properties:
-            setting.validate_properties(opt, False, False, value=value,
+            setting.validate_properties(opt, False, False, value=value, path=path,
                                         force_permissive=force_permissive,
-                                        force_properties=force_properties)
+                                        force_properties=force_properties,
+                                        force_permissives=force_permissives)
+        if config_error is not None:
+            raise ConfigError(config_error)
         return value
 
     def __setitem__(self, opt, value):
-        self.setitem(opt, value)
+        raise ValueError('you should only set value with config')
 
-    def setitem(self, opt, value, force_permissive=False, is_write=True):
+    def setitem(self, opt, value, path, force_permissive=False,
+                is_write=True):
         # is_write is, for example, used with "force_store_value"
         # user didn't change value, so not write
         # valid opt
-        opt.impl_validate(value, self.context,
-                          'validator' in self.context.cfgimpl_get_settings())
+        self._warning = opt.impl_validate(value, self.context(),
+                                          'validator' in self.context(
+                                          ).cfgimpl_get_settings())
         if opt.impl_is_multi() and not isinstance(value, Multi):
-            value = Multi(value, self.context, opt)
-        self._setvalue(opt, value, force_permissive=force_permissive,
+            value = Multi(value, self.context, opt, path, setitem=True)
+        self._setvalue(opt, path, value, force_permissive=force_permissive,
                        is_write=is_write)
 
-    def _setvalue(self, opt, value, force_permissive=False,
+    def _setvalue(self, opt, path, value, force_permissive=False,
                   force_properties=None,
                   is_write=True, validate_properties=True):
-        self.context.cfgimpl_reset_cache()
+        self.context().cfgimpl_reset_cache()
         if validate_properties:
-            setting = self.context.cfgimpl_get_settings()
+            setting = self.context().cfgimpl_get_settings()
             setting.validate_properties(opt, False, is_write,
-                                        value=value,
+                                        value=value, path=path,
                                         force_permissive=force_permissive,
                                         force_properties=force_properties)
-        owner = self.context.cfgimpl_get_settings().getowner()
-        self._p_.setvalue(self._getkey(opt), value, owner)
+        owner = self.context().cfgimpl_get_settings().getowner()
+        self._p_.setvalue(path, value, owner)
 
     def getowner(self, opt):
         """
@@ -257,10 +282,14 @@ class Values(object):
         """
         if isinstance(opt, SymLinkOption):
             opt = opt._opt
-        owner = self._p_.getowner(self._getkey(opt), owners.default)
-        meta = self.context.cfgimpl_get_meta()
+        path = self._get_opt_path(opt)
+        return self._getowner(path)
+
+    def _getowner(self, path):
+        owner = self._p_.getowner(path, owners.default)
+        meta = self.context().cfgimpl_get_meta()
         if owner is owners.default and meta is not None:
-            owner = meta.cfgimpl_get_values().getowner(opt)
+            owner = meta.cfgimpl_get_values()._getowner(path)
         return owner
 
     def setowner(self, opt, owner):
@@ -272,10 +301,15 @@ class Values(object):
         """
         if not isinstance(owner, owners.Owner):
             raise TypeError(_("invalid generic owner {0}").format(str(owner)))
-        if self.getowner(opt) == owners.default:
+
+        path = self._get_opt_path(opt)
+        self._setowner(path, owner)
+
+    def _setowner(self, path, owner):
+        if self._getowner(path) == owners.default:
             raise ConfigError(_('no value for {0} cannot change owner to {1}'
-                                '').format(opt._name, owner))
-        self._p_.setowner(self._getkey(opt), owner)
+                                '').format(path, owner))
+        self._p_.setowner(path, owner)
 
     def is_default_owner(self, opt):
         """
@@ -283,16 +317,20 @@ class Values(object):
                        (not the toplevel config)
         :return: boolean
         """
-        return self.getowner(opt) == owners.default
+        path = self._get_opt_path(opt)
+        return self._is_default_owner(path)
+
+    def _is_default_owner(self, path):
+        return self._getowner(path) == owners.default
 
     def reset_cache(self, only_expired):
         """
         clears the cache if necessary
         """
         if only_expired:
-            self._p_.reset_expired_cache('value', time())
+            self._p_.reset_expired_cache(int(time()))
         else:
-            self._p_.reset_all_cache('value')
+            self._p_.reset_all_cache()
 
     def _get_opt_path(self, opt):
         """
@@ -301,7 +339,55 @@ class Values(object):
         :param opt: the `option.Option` object
         :returns: a string with points like "gc.dummy.my_option"
         """
-        return self.context.cfgimpl_get_description().impl_get_path_by_opt(opt)
+        return self.context().cfgimpl_get_description().impl_get_path_by_opt(opt)
+
+    # information
+    def set_information(self, key, value):
+        """updates the information's attribute
+
+        :param key: information's key (ex: "help", "doc"
+        :param value: information's value (ex: "the help string")
+        """
+        self._p_.set_information(key, value)
+
+    def get_information(self, key, default=None):
+        """retrieves one information's item
+
+        :param key: the item string (ex: "help")
+        """
+        try:
+            return self._p_.get_information(key)
+        except ValueError:
+            if default is not None:
+                return default
+            else:
+                raise ValueError(_("information's item"
+                                   " not found: {0}").format(key))
+
+    def __getstate__(self):
+        return {'_p_': self._p_}
+
+    def _impl_setstate(self, storage):
+        self._p_._storage = storage
+
+    def __setstate__(self, states):
+        self._p_ = states['_p_']
+
+    def has_warning(self):
+        """If option is "only_warning", validation error is store in
+        self._warning.
+        has_warning just indicate that a warning message is store
+        """
+        return self._warning is not None
+
+    def get_last_warning(self):
+        """Get last warning message in self._warning.
+        We can get only one time this message.
+        """
+        ret = self._warning
+        self._warning = None
+        return ret
+
 
 # ____________________________________________________________
 # multi types
@@ -310,49 +396,63 @@ class Values(object):
 class Multi(list):
     """multi options values container
     that support item notation for the values of multi options"""
-    __slots__ = ('opt', 'context')
+    __slots__ = ('opt', 'path', 'context')
 
-    def __init__(self, value, context, opt, validate=True):
+    def __init__(self, value, context, opt, path, validate=True,
+                 setitem=False):
         """
         :param value: the Multi wraps a list value
         :param context: the home config that has the values
         :param opt: the option object that have this Multi value
+        :param setitem: only if set a value
         """
         self.opt = opt
+        self.path = path
+        if not isinstance(context, weakref.ReferenceType):
+            raise ValueError('context must be a Weakref')
         self.context = context
         if not isinstance(value, list):
             value = [value]
         if validate and self.opt.impl_get_multitype() == multitypes.slave:
-            value = self._valid_slave(value)
-        elif self.opt.impl_get_multitype() == multitypes.master:
+            value = self._valid_slave(value, setitem)
+        elif validate and self.opt.impl_get_multitype() == multitypes.master:
             self._valid_master(value)
         super(Multi, self).__init__(value)
 
-    def _valid_slave(self, value):
+    def _valid_slave(self, value, setitem):
         #if slave, had values until master's one
-        masterp = self.context.cfgimpl_get_description().impl_get_path_by_opt(
+        values = self.context().cfgimpl_get_values()
+        masterp = self.context().cfgimpl_get_description().impl_get_path_by_opt(
             self.opt.impl_get_master_slaves())
-        mastervalue = getattr(self.context, masterp)
+        mastervalue = getattr(self.context(), masterp)
         masterlen = len(mastervalue)
         valuelen = len(value)
+        is_default_owner = not values._is_default_owner(self.path) or setitem
         if valuelen > masterlen or (valuelen < masterlen and
-                                    not self.context.cfgimpl_get_values(
-                                    ).is_default_owner(self.opt)):
+                                    is_default_owner):
             raise SlaveError(_("invalid len for the slave: {0}"
                                " which has {1} as master").format(
                                    self.opt._name, masterp))
         elif valuelen < masterlen:
             for num in range(0, masterlen - valuelen):
-                value.append(self.opt.impl_getdefault_multi())
+                if self.opt.impl_has_callback():
+                    # if callback add a value, but this value will not change
+                    # anymore automaticly (because this value has owner)
+                    index = value.__len__()
+                    value.append(values._getcallback_value(self.opt,
+                                                           index=index))
+                else:
+                    value.append(self.opt.impl_getdefault_multi())
         #else: same len so do nothing
         return value
 
     def _valid_master(self, value):
         masterlen = len(value)
-        values = self.context.cfgimpl_get_values()
+        values = self.context().cfgimpl_get_values()
         for slave in self.opt._master_slaves:
-            if not values.is_default_owner(slave):
-                value_slave = values._getvalue(slave)
+            path = values._get_opt_path(slave)
+            if not values._is_default_owner(path):
+                value_slave = values._getvalue(slave, path)
                 if len(value_slave) > masterlen:
                     raise SlaveError(_("invalid len for the master: {0}"
                                        " which has {1} as slave with"
@@ -360,14 +460,23 @@ class Multi(list):
                                            self.opt._name, slave._name))
                 elif len(value_slave) < masterlen:
                     for num in range(0, masterlen - len(value_slave)):
-                        value_slave.append(slave.impl_getdefault_multi(),
-                                           force=True)
+                        if slave.impl_has_callback():
+                            # if callback add a value, but this value will not
+                            # change anymore automaticly (because this value
+                            # has owner)
+                            index = value_slave.__len__()
+                            value_slave.append(
+                                values._getcallback_value(slave, index=index),
+                                force=True)
+                        else:
+                            value_slave.append(slave.impl_getdefault_multi(),
+                                               force=True)
 
     def __setitem__(self, key, value):
         self._validate(value)
         #assume not checking mandatory property
         super(Multi, self).__setitem__(key, value)
-        self.context.cfgimpl_get_values()._setvalue(self.opt, self)
+        self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
 
     def append(self, value, force=False):
         """the list value can be updated (appened)
@@ -378,7 +487,7 @@ class Multi(list):
                 raise SlaveError(_("cannot append a value on a multi option {0}"
                                    " which is a slave").format(self.opt._name))
             elif self.opt.impl_get_multitype() == multitypes.master:
-                values = self.context.cfgimpl_get_values()
+                values = self.context().cfgimpl_get_values()
                 if value is None and self.opt.impl_has_callback():
                     value = values._getcallback_value(self.opt)
                     #Force None il return a list
@@ -386,29 +495,41 @@ class Multi(list):
                         value = None
         self._validate(value)
         super(Multi, self).append(value)
-        self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force)
+        self.context().cfgimpl_get_values()._setvalue(self.opt, self.path,
+                                                      self,
+                                                      validate_properties=not force)
         if not force and self.opt.impl_get_multitype() == multitypes.master:
             for slave in self.opt.impl_get_master_slaves():
-                if not values.is_default_owner(slave):
+                path = values._get_opt_path(slave)
+                if not values._is_default_owner(path):
                     if slave.impl_has_callback():
                         index = self.__len__() - 1
                         dvalue = values._getcallback_value(slave, index=index)
                     else:
                         dvalue = slave.impl_getdefault_multi()
-                    old_value = values.getitem(slave, validate_properties=False)
+                    old_value = values.getitem(slave, path,
+                                               validate_properties=False)
                     if len(old_value) < self.__len__():
-                        values.getitem(slave, validate_properties=False).append(
-                            dvalue, force=True)
+                        values.getitem(slave, path,
+                                       validate_properties=False).append(
+                                           dvalue, force=True)
                     else:
-                        values.getitem(slave, validate_properties=False)[index] = dvalue
+                        values.getitem(slave, path,
+                                       validate_properties=False)[
+                                           index] = dvalue
 
     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))
-        super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
-        self.context.cfgimpl_get_values()._setvalue(self.opt, self)
+        if sys.version_info[0] >= 3:
+            if cmp is not None:
+                raise ValueError(_('cmp is not permitted in python v3 or greater'))
+            super(Multi, self).sort(key=key, reverse=reverse)
+        else:
+            super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
+        self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
 
     def reverse(self):
         if self.opt.impl_get_multitype() in [multitypes.slave,
@@ -416,7 +537,7 @@ class Multi(list):
             raise SlaveError(_("cannot reverse multi option {0} if master or "
                                "slave").format(self.opt._name))
         super(Multi, self).reverse()
-        self.context.cfgimpl_get_values()._setvalue(self.opt, self)
+        self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
 
     def insert(self, index, obj):
         if self.opt.impl_get_multitype() in [multitypes.slave,
@@ -424,7 +545,7 @@ class Multi(list):
             raise SlaveError(_("cannot insert multi option {0} if master or "
                                "slave").format(self.opt._name))
         super(Multi, self).insert(index, obj)
-        self.context.cfgimpl_get_values()._setvalue(self.opt, self)
+        self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
 
     def extend(self, iterable):
         if self.opt.impl_get_multitype() in [multitypes.slave,
@@ -432,24 +553,29 @@ class Multi(list):
             raise SlaveError(_("cannot extend multi option {0} if master or "
                                "slave").format(self.opt._name))
         super(Multi, self).extend(iterable)
-        self.context.cfgimpl_get_values()._setvalue(self.opt, self)
+        self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
 
     def _validate(self, value):
         if value is not None:
             try:
-                self.opt._validate(value)
-            except ValueError, err:
+                self.context().cfgimpl_get_values()._warning = \
+                    self.opt.impl_validate(value, context=self.context(),
+                                           force_no_multi=True)
+            except ValueError as err:
                 raise ValueError(_("invalid value {0} "
                                    "for option {1}: {2}"
                                    "").format(str(value),
                                               self.opt._name, err))
 
-    def pop(self, key, force=False):
+    def pop(self, index, force=False):
         """the list value can be updated (poped)
         only if the option is a master
 
-        :param key: index of the element to pop
-        :return: the requested element
+        :param index: remove item a index
+        :type index: int
+        :param force: force pop item (withoud check master/slave)
+        :type force: boolean
+        :returns: item at index
         """
         if not force:
             if self.opt.impl_get_multitype() == multitypes.slave:
@@ -457,13 +583,13 @@ class Multi(list):
                                    " which is a slave").format(self.opt._name))
             elif self.opt.impl_get_multitype() == multitypes.master:
                 for slave in self.opt.impl_get_master_slaves():
-                    values = self.context.cfgimpl_get_values()
+                    values = self.context().cfgimpl_get_values()
                     if not values.is_default_owner(slave):
                         #get multi without valid properties
                         values.getitem(slave,
                                        validate_properties=False
-                                       ).pop(key, force=True)
+                                       ).pop(index, force=True)
         #set value without valid properties
-        ret = super(Multi, self).pop(key)
-        self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force)
+        ret = super(Multi, self).pop(index)
+        self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self, validate_properties=not force)
         return ret