key is now always path and change opt by path dictionary storage
[tiramisu.git] / tiramisu / value.py
index 2ac7add..f2cbdf3 100644 (file)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
-# The original `Config` design model is unproudly borrowed from
-# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
-# the whole pypy projet is under MIT licence
 # ____________________________________________________________
-from tiramisu.error import NoValueReturned, MandatoryError, MultiTypeError
-from tiramisu.setting import owners, multitypes
+from time import time
+from copy import copy
+from tiramisu.error import ConfigError, SlaveError
+from tiramisu.setting import owners, multitypes, expires_time, storage_type
+from tiramisu.autolib import carry_out_calculation
+from tiramisu.i18n import _
+from tiramisu.option import SymLinkOption
+
 
 class Values(object):
-    def __init__(self, context):
+    """The `Config`'s root is indeed  in charge of the `Option()`'s values,
+    but the values are physicaly located here, in `Values`, wich is also
+    responsible of a caching utility.
+    """
+    __slots__ = ('context', '_p_')
+
+    def __init__(self, context, storage):
         """
         Initializes the values's dict.
 
-        :param context: the context is the home config's values and properties
+        :param context: the context is the home config's values
+
         """
-        self.owners = {}
-        "Config's root indeed is in charge of the `Option()`'s values"
-        self.values = {}
-        self.previous_values = {}
-        self.masters = {}
-        self.slaves = {}
         self.context = 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 _get_multitype(self, opt):
-        if opt in self.slaves:
-            # slave
-            multitype = multitypes.slave
-        elif opt in self.masters:
-            # master
-            multitype = multitypes.master
-        # FIXME : default value for a multi, we shall work on groups
-        else:
-            multitype = multitypes.default
-        return multitype
-
-    def _get_value(self, opt):
-        "special case for the multis: they never return None"
-        if opt not in self.values:
-            if opt.is_multi():
-                multitype = self._get_multitype(opt)
-                return Multi(opt.getdefault(), self.context, opt, multitype)
-            else:
-                return opt.getdefault()
-        return self.values[opt]
-
-    def reset(self, opt):
-        if opt in self.values:
-            self.set_previous_value(opt)
-            del(self.values[opt])
-        self.setowner(opt, owners.default)
-
-    def set_previous_value(self, opt):
-        if opt in self.values:
-            old_value = self.values[opt]
-        elif opt.is_multi():
-            old_value = []
+    def _getdefault(self, opt):
+        """
+        actually retrieves the default value
+
+        :param opt: the `option.Option()` object
+        """
+        meta = self.context.cfgimpl_get_meta()
+        if meta is not None:
+            value = meta.cfgimpl_get_values()[opt]
         else:
-            old_value = None
-        if type(old_value) == Multi:
-           self.previous_values[opt] = list(old_value)
+            value = opt.impl_getdefault()
+        if opt.impl_is_multi():
+            return copy(value)
         else:
-           self.previous_values[opt] = old_value
+            return value
+
+    def _getvalue(self, opt, path, validate=True):
+        """actually retrieves the value
 
-    def get_previous_value(self, opt):
-        if opt in self.previous_values:
-            prec_value = self.previous_values[opt]
-        elif opt.is_multi():
-            prec_value = []
+        :param opt: the `option.Option()` object
+        :returns: the option's value (or the default value if not set)
+        """
+        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, path, validate)
         else:
-            prec_value = None
-        return prec_value
+            # 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, path, validate=False)
+        return value
 
-    def _is_empty(self, opt, value=None):
+    def get_modified_values(self):
+        return self._p_.get_modified_values()
+
+    def __contains__(self, opt):
+        """
+        implements the 'in' keyword syntax in order provide a pythonic way
+        to kow if an option have a value
+
+        :param opt: the `option.Option()` object
+        """
+        path = self._get_opt_path(opt)
+        self._contains(path)
+
+    def _contains(self, path):
+        return self._p_.hasvalue('value', path)
+
+    def __delitem__(self, opt):
+        """overrides the builtins `del()` instructions"""
+        self.reset(opt)
+
+    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()
+            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(path)
+
+    def _isempty(self, opt, value):
         "convenience method to know if an option is empty"
-        if value is not None:
-            return False
-        if (not opt.is_multi() and value == None) or \
-            (opt.is_multi() and (value == [] or \
-                None in self._get_value(opt))):
+        empty = opt._empty
+        if (not opt.impl_is_multi() and (value is None or value == empty)) or \
+           (opt.impl_is_multi() and (value == [] or
+                                     None in value or empty in value)):
             return True
         return False
 
-    def is_empty(self, opt):
-        if opt not in self.values:
-            return True
-        value = self.values[opt]
-        if not opt.is_multi():
-            if self._get_value(opt) == None:
-                return True
-            return False
-        else:
-            value = list(value)
-            for val in value:
-                if val != None:
-                    return False
-            return True
+    def _getcallback_value(self, opt, index=None):
+        """
+        retrieves a value for the options that have a callback
 
-    def _test_mandatory(self, opt, value=None):
-        # mandatory options
-        mandatory = self.context._cfgimpl_settings.mandatory
-        if opt.is_mandatory() and mandatory:
-            if self._is_empty(opt, value) and \
-                    opt.is_empty_by_default():
-                raise MandatoryError("option: {0} is mandatory "
-                                      "and shall have a value".format(opt._name))
-
-    def fill_multi(self, opt, result):
-        """fills a multi option with default and calculated values
+        :param opt: the `option.Option()` object
+        :param index: if an option is multi, only calculates the nth value
+        :type index: int
+        :returns: a calculated value
         """
-        value = self._get_value(opt)
-        if not isinstance(result, list):
-            _result = [result]
-        else:
-            _result = result
-        multitype = self._get_multitype(opt)
-        return Multi(_result, self.context, opt, multitype)
+        callback, callback_params = opt._callback
+        if callback_params is None:
+            callback_params = {}
+        return carry_out_calculation(opt._name, config=self.context,
+                                     callback=callback,
+                                     callback_params=callback_params,
+                                     index=index)
 
     def __getitem__(self, opt):
+        "enables us to use the pythonic dictionnary-like access to values"
+        return self.getitem(opt)
+
+    def getitem(self, opt, path=None, validate=True, force_permissive=False,
+                force_properties=None, validate_properties=True):
+        ntime = None
+        if path is None:
+            path = self._get_opt_path(opt)
+        if self._p_.hascache('value', path):
+            ntime = time()
+            is_cached, value = self._p_.getcache('value', 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, path, validate=False)
+                return value
+        val = self._getitem(opt, path, 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', path, val, ntime + expires_time)
+
+        return val
+
+    def _getitem(self, opt, path, validate, force_permissive, force_properties,
+                 validate_properties):
         # options with callbacks
-        value = self._get_value(opt)
-        if opt.has_callback():
-            if (not opt.is_frozen() or \
-                    not opt.is_forced_on_freeze()) and \
-                    not opt.is_default_owner(self.context):
-                return self._get_value(opt)
-            try:
-                result = opt.getcallback_value(
-                        self.context)
-            except NoValueReturned, err:
-                pass
-            else:
-                if opt.is_multi():
-                    value = self.fill_multi(opt, result)
-                else:
-                    # this result **shall not** be a list
-                    if isinstance(result, list):
-                        raise ConfigError('invalid calculated value returned '
-                            'for option {0} : shall not be a list'.format(name))
-                    value = result
-                if value != None and not opt.validate(value,
-                            self.context._cfgimpl_settings.validator):
-                    raise ConfigError('invalid calculated value returned'
-                        ' for option {0}'.format(name))
+        setting = self.context.cfgimpl_get_settings()
+        is_frozen = 'frozen' in setting[opt]
+        # 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(path) or
+                (is_frozen and 'force_default_on_freeze' in setting[opt])):
+            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)
+                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, path, validate)
+            # suppress value if already set
+            self.reset(opt, path)
         # frozen and force default
-        if not opt.has_callback() and opt.is_forced_on_freeze():
-            value = opt.getdefault()
-            if opt.is_multi():
-                value = self.fill_multi(opt, value)
-        self._test_mandatory(opt, value)
+        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, path, validate)
+        else:
+            value = self._getvalue(opt, path, validate)
+        if validate:
+            opt.impl_validate(value, self.context, 'validator' in setting)
+        if self._is_default_owner(path) and \
+                'force_store_value' in setting[opt]:
+            self.setitem(opt, value, path, is_write=False)
+        if validate_properties:
+            setting.validate_properties(opt, False, False, value=value, path=path,
+                                        force_permissive=force_permissive,
+                                        force_properties=force_properties)
         return value
 
     def __setitem__(self, opt, value):
-        if opt in self.masters:
-            masterlen = len(value)
-            for slave in self.masters[opt]:
-                value_slave = self._get_value(slave)
-                if len(value_slave) > masterlen:
-                    raise MultiTypeError("invalid len for the slave: {0}"
-                    " which has {1} as master".format(slave._name,
-                                                      opt._name))
-                elif len(value_slave) < masterlen:
-                    for num in range(0, masterlen - len(value_slave)):
-                        value_slave.append(None, force=True)
-
-        elif opt in self.slaves:
-            if len(self._get_value(self.slaves[opt])) != len(value):
-                   raise MultiTypeError("invalid len for the slave: {0}"
-                    " which has {1} as master".format(opt._name,
-                                                      self.slaves[opt]._name))
-        elif opt.is_multi():
-            if not isinstance(value, Multi):
-                value = Multi(value, self.context, opt, multitypes.default)
-        self.setitem(opt, value)
-
-    def setitem(self, opt, value):
-        self.set_previous_value(opt)
-        if type(value) == list:
-            raise MultiTypeError("the type of the value {0} which is multi shall "
-                                 "be Multi and not list".format(str(value)))
-        self.values[opt] = value
-        self.setowner(opt, self.context._cfgimpl_settings.getowner())
+        path = self._get_opt_path(opt)
+        self.setitem(opt, value, path)
+
+    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())
+        if opt.impl_is_multi() and not isinstance(value, Multi):
+            value = Multi(value, self.context, opt, path)
+        self._setvalue(opt, path, value, force_permissive=force_permissive,
+                       is_write=is_write)
+
+    def _setvalue(self, opt, path, value, force_permissive=False,
+                  force_properties=None,
+                  is_write=True, validate_properties=True):
+        self.context.cfgimpl_reset_cache()
+        if validate_properties:
+            setting = self.context.cfgimpl_get_settings()
+            setting.validate_properties(opt, False, is_write,
+                                        value=value, path=path,
+                                        force_permissive=force_permissive,
+                                        force_properties=force_properties)
+        owner = self.context.cfgimpl_get_settings().getowner()
+        self._p_.setvalue(path, value, owner)
+
+    def getowner(self, opt):
+        """
+        retrieves the option's owner
+
+        :param opt: the `option.Option` object
+        :returns: a `setting.owners.Owner` object
+        """
+        if isinstance(opt, SymLinkOption):
+            opt = opt._opt
+        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(path)
+        return owner
 
-    def __contains__(self, opt):
-        return opt in self.values
-    #____________________________________________________________
     def setowner(self, opt, owner):
-        if isinstance(owner, owners.Owner):
-            self.owners[opt] = owner
+        """
+        sets a owner to an option
+
+        :param opt: the `option.Option` object
+        :param owner: a valid owner, that is a `setting.owners.Owner` object
+        """
+        if not isinstance(owner, owners.Owner):
+            raise TypeError(_("invalid generic owner {0}").format(str(owner)))
+
+        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(path, owner))
+        self._p_.setowner(path, owner)
+
+    def is_default_owner(self, opt):
+        """
+        :param config: *must* be only the **parent** config
+                       (not the toplevel config)
+        :return: boolean
+        """
+        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())
         else:
-            raise OptionValueError("Bad owner: " + str(owner))
+            self._p_.reset_all_cache('value')
 
-    def getowner(self, opt):
-        return self.owners.get(opt, owners.default)
+    def _get_opt_path(self, opt):
+        """
+        retrieve the option's path in the config
+
+        :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)
 
 # ____________________________________________________________
 # multi types
+
+
 class Multi(list):
     """multi options values container
     that support item notation for the values of multi options"""
-    def __init__(self, lst, context, opt, multitype):
+    __slots__ = ('opt', 'path', 'context')
+
+    def __init__(self, value, context, opt, path, validate=True):
         """
-        :param lst: the Multi wraps a list value
-        :param context: the home config that has the settings and the values
+        :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
         """
-        self.settings = context._cfgimpl_settings
         self.opt = opt
-        self.values = context._cfgimpl_values
-        self.multitype = multitype
-        super(Multi, self).__init__(lst)
-        if multitype == multitypes.master:
-            self.slaves = context._cfgimpl_values.masters[opt]
-        else:
-            self.slaves = None
-    def __setitem__(self, key, value):
+        self.path = path
+        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:
+            self._valid_master(value)
+        super(Multi, self).__init__(value)
+
+    def _valid_slave(self, value):
+        #if slave, had values until master's one
+        masterp = self.context.cfgimpl_get_description().impl_get_path_by_opt(
+            self.opt.impl_get_master_slaves())
+        mastervalue = getattr(self.context, masterp)
+        masterlen = len(mastervalue)
+        valuelen = len(value)
+        if valuelen > masterlen or (valuelen < masterlen and
+                                    not self.context.cfgimpl_get_values(
+                                    )._is_default_owner(self.path)):
+            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())
+        #else: same len so do nothing
+        return value
+
+    def _valid_master(self, value):
+        masterlen = len(value)
+        values = self.context.cfgimpl_get_values()
+        for slave in self.opt._master_slaves:
+            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"
+                                       " greater len").format(
+                                           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)
+
+    def __setitem__(self, path, value):
         self._validate(value)
-        self.values[self.opt] = self
-        super(Multi, self).__setitem__(key, value)
+        #assume not checking mandatory property
+        super(Multi, self).__setitem__(path, value)
+        self.context.cfgimpl_get_values()._setvalue(self.opt, path, self)
 
     def append(self, value, force=False):
         """the list value can be updated (appened)
         only if the option is a master
         """
         if not force:
-            if self.multitype == multitypes.slave:
-                raise MultiTypeError("cannot append a value on a multi option {0}"
-                        " which is a slave".format(self.opt._name))
-            elif self.multitype == multitypes.master:
-                for slave in self.slaves:
-                    self.values[slave].append(None, force=True)
+            if self.opt.impl_get_multitype() == multitypes.slave:
+                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()
+                if value is None and self.opt.impl_has_callback():
+                    value = values._getcallback_value(self.opt)
+                    #Force None il return a list
+                    if isinstance(value, list):
+                        value = None
         self._validate(value)
-        self.values.setitem(self.opt, self)
         super(Multi, self).append(value)
+        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():
+                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, path,
+                                               validate_properties=False)
+                    if len(old_value) < self.__len__():
+                        values.getitem(slave, path,
+                                       validate_properties=False).append(
+                                           dvalue, force=True)
+                    else:
+                        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.path, self)
+
+    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))
+        super(Multi, self).reverse()
+        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,
+                                             multitypes.master]:
+            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.path, self)
+
+    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))
+        super(Multi, self).extend(iterable)
+        self.context.cfgimpl_get_values()._setvalue(self.opt, self.path, self)
 
     def _validate(self, value):
-        if value != None and not self.opt._validate(value):
-            raise ConfigError("invalid value {0} "
-                    "for option {1}".format(str(value), self.opt._name))
+        if value is not None:
+            try:
+                self.opt._validate(value)
+            except ValueError, err:
+                raise ValueError(_("invalid value {0} "
+                                   "for option {1}: {2}"
+                                   "").format(str(value),
+                                              self.opt._name, err))
 
     def pop(self, key, force=False):
         """the list value can be updated (poped)
@@ -262,11 +470,18 @@ class Multi(list):
         :return: the requested element
         """
         if not force:
-            if self.multitype == multitypes.slave:
-                raise MultiTypeError("cannot append a value on a multi option {0}"
-                        " which is a slave".format(self.opt._name))
-            elif self.multitype == multitypes.master:
-                for slave in self.slaves:
-                    self.values[slave].pop(key, force=True)
-        self.values.setitem(self.opt, self)
-        return super(Multi, self).pop(key)
+            if self.opt.impl_get_multitype() == multitypes.slave:
+                raise SlaveError(_("cannot pop a value on a multi option {0}"
+                                   " 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()
+                    if not values.is_default_owner(slave):
+                        #get multi without valid properties
+                        values.getitem(slave,
+                                       validate_properties=False
+                                       ).pop(key, force=True)
+        #set value without valid properties
+        ret = super(Multi, self).pop(key)
+        self.context.cfgimpl_get_values()._setvalue(self.opt, self.path, self, validate_properties=not force)
+        return ret