tiramisu/tiramisu/value.py

613 lines
25 KiB
Python

# -*- coding: utf-8 -*-
"takes care of the option's values and multi values"
# Copyright (C) 2013 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 Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________
from time import time
import sys
import weakref
from tiramisu.error import ConfigError, SlaveError, PropertiesOptionError
from tiramisu.setting import owners, expires_time, undefined
from tiramisu.autolib import carry_out_calculation
from tiramisu.i18n import _
from tiramisu.option import SymLinkOption, OptionDescription
class Values(object):
"""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_', '__weakref__')
def __init__(self, context, storage):
"""
Initializes the values's dict.
:param context: the context is the home config's values
"""
self.context = weakref.ref(context)
# the storage type is dictionary or sqlite3
self._p_ = storage
def _getcontext(self):
"""context could be None, we need to test it
context is None only if all reference to `Config` object is deleted
(for example we delete a `Config` and we manipulate a reference to
old `SubConfig`, `Values`, `Multi` or `Settings`)
"""
context = self.context()
if context is None:
raise ConfigError(_('the context does not exist anymore'))
return context
def _getvalue(self, opt, path, is_default, index=undefined):
"""actually retrieves the value
:param opt: the `option.Option()` object
:returns: the option's value (or the default value if not set)
"""
setting = self._getcontext().cfgimpl_get_settings()
force_default = 'frozen' in setting[opt] and \
'force_default_on_freeze' in setting[opt]
if not is_default and not force_default:
value = self._p_.getvalue(path)
if index is not undefined:
try:
return value[index]
except IndexError:
#value is smaller than expected
#so return default value
pass
else:
return value
#so default value
# if value has callback and is not set
if opt.impl_has_callback():
callback, callback_params = opt.impl_get_callback()
if callback_params is None:
callback_params = {}
value = carry_out_calculation(opt, config=self._getcontext(),
callback=callback,
callback_params=callback_params,
index=index)
try:
if isinstance(value, list) and index is not undefined:
return value[index]
return value
except IndexError:
pass
meta = self._getcontext().cfgimpl_get_meta()
if meta is not None:
#FIXME : problème de longueur si meta + slave
#doit passer de meta à pas meta
#en plus il faut gérer la longueur avec les meta !
#FIXME SymlinkOption
value = meta.cfgimpl_get_values()[opt]
if isinstance(value, Multi):
if index is not undefined:
value = value[index]
else:
value = list(value)
return value
# now try to get default value
value = opt.impl_getdefault()
if opt.impl_is_multi() and index is not undefined:
if value is None:
value = opt.impl_getdefault_multi()
else:
try:
value = value[index]
except IndexError:
value = opt.impl_getdefault_multi()
return value
def get_modified_values(self):
context = self._getcontext()
if context._impl_descr is not None:
for opt, path in context.cfgimpl_get_settings(
).get_with_property('force_store_value'):
self._getowner(opt, path, force_permissive=True)
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)
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, path=None):
if path is None:
path = self._get_opt_path(opt)
if self._p_.hasvalue(path):
context = self._getcontext()
setting = context.cfgimpl_get_settings()
opt.impl_validate(opt.impl_getdefault(),
context, 'validator' in setting)
context.cfgimpl_reset_cache()
if opt.impl_is_master_slaves('master'):
opt.impl_get_master_slaves().reset(self)
self._p_.resetvalue(path)
def _isempty(self, opt, value):
"convenience method to know if an option is empty"
empty = opt._empty
if value is not undefined:
empty_not_multi = not opt.impl_is_multi() and (value is None or
value == empty)
empty_multi = opt.impl_is_multi() and (value == [] or
None in value or
empty in value)
else:
empty_multi = empty_not_multi = False
return empty_not_multi or empty_multi
def __getitem__(self, opt):
"enables us to use the pythonic dictionary-like access to values"
return self.getitem(opt)
def getitem(self, opt, validate=True, force_permissive=False):
"""
"""
return self._get_cached_item(opt, validate=validate,
force_permissive=force_permissive)
def _get_cached_item(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
setting = self._getcontext().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, path)
return value
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, path, validate, force_permissive, force_properties,
validate_properties):
if opt.impl_is_master_slaves():
return opt.impl_get_master_slaves().getitem(self, opt, path,
validate,
force_permissive,
force_properties,
validate_properties)
else:
return self._get_validated_value(opt, path, validate,
force_permissive,
force_properties,
validate_properties)
def _get_validated_value(self, opt, path, validate, force_permissive,
force_properties, validate_properties,
index=undefined):
"""same has getitem but don't touch the cache"""
#FIXME expliquer la différence entre index == undefined et index == None
context = self._getcontext()
setting = context.cfgimpl_get_settings()
is_default = self._is_default_owner(opt, path,
validate_properties=False,
validate_meta=False)
try:
if index is None:
gv_index = undefined
else:
gv_index = index
value = self._getvalue(opt, path, is_default, index=gv_index)
config_error = None
except ConfigError as err:
# For calculating properties, we need value (ie for mandatory
# value).
# If value is calculating with a PropertiesOptionError's option
# _getvalue 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.
# cannot assign config_err directly in python 3.3
config_error = err
# value is not set, for 'undefined' (cannot set None because of
# mandatory property)
value = undefined
if config_error is None:
if index is undefined:
force_index = None
else:
force_index = index
if opt.impl_is_multi():
if index is None and not isinstance(value, list):
value = []
if force_index is None:
value = Multi(value, self.context, opt, path)
if validate:
opt.impl_validate(value, context, 'validator' in setting,
force_index=force_index)
#FIXME pas de test avec les metas ...
#FIXME et les symlinkoption ...
if is_default and 'force_store_value' in setting[opt]:
self.setitem(opt, value, path, is_write=False,
force_permissive=force_permissive)
if validate_properties:
setting.validate_properties(opt, False, False, value=value,
path=path,
force_permissive=force_permissive,
force_properties=force_properties)
if config_error is not None:
raise config_error
return value
def __setitem__(self, opt, value):
raise ValueError(_('you should only set value with config'))
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
context = self._getcontext()
opt.impl_validate(value, context,
'validator' in context.cfgimpl_get_settings())
if opt.impl_is_multi():
value = Multi(value, self.context, opt, path)
if opt.impl_is_master_slaves():
opt.impl_get_master_slaves().setitem(self, opt, value, path)
self._setvalue(opt, path, value, force_permissive=force_permissive,
is_write=is_write)
def _setvalue(self, opt, path, value, force_permissive=False,
is_write=True, validate_properties=True):
context = self._getcontext()
context.cfgimpl_reset_cache()
if validate_properties:
setting = context.cfgimpl_get_settings()
setting.validate_properties(opt, False, is_write,
value=value, path=path,
force_permissive=force_permissive)
owner = context.cfgimpl_get_settings().getowner()
if isinstance(value, Multi):
value = list(value)
self._p_.setvalue(path, value, owner)
def getowner(self, opt, force_permissive=False):
"""
retrieves the option's owner
:param opt: the `option.Option` object
:param force_permissive: behaves as if the permissive property
was present
:returns: a `setting.owners.Owner` object
"""
if isinstance(opt, SymLinkOption):
opt = opt._opt
path = self._get_opt_path(opt)
return self._getowner(opt, path, force_permissive=force_permissive)
def _getowner(self, opt, path, validate_properties=True,
force_permissive=False, validate_meta=True):
if validate_properties:
self._getitem(opt, path, True, force_permissive, None, True)
owner = self._p_.getowner(path, owners.default)
if validate_meta:
meta = self._getcontext().cfgimpl_get_meta()
if owner is owners.default and meta is not None:
owner = meta.cfgimpl_get_values()._getowner(opt, path)
return owner
def setowner(self, 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(opt, path, owner)
def _setowner(self, opt, path, owner):
if self._getowner(opt, 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, validate_properties=True,
validate_meta=True):
"""
: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(opt, path,
validate_properties=validate_properties,
validate_meta=validate_meta)
def _is_default_owner(self, opt, path, validate_properties=True,
validate_meta=True):
return self._getowner(opt, path, validate_properties,
validate_meta=validate_meta) == owners.default
def reset_cache(self, only_expired):
"""
clears the cache if necessary
"""
if only_expired:
self._p_.reset_expired_cache(int(time()))
else:
self._p_.reset_all_cache()
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._getcontext().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=undefined):
"""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 undefined:
return default
else:
raise ValueError(_("information's item"
" not found: {0}").format(key))
def mandatory_warnings(self):
"""convenience function to trace Options that are mandatory and
where no value has been set
:returns: generator of mandatory Option's path
"""
def _mandatory_warnings(description):
#if value in cache, properties are not calculated
for opt in description.impl_getchildren():
if isinstance(opt, OptionDescription):
_mandatory_warnings(opt)
elif isinstance(opt, SymLinkOption):
pass
else:
path = self._get_opt_path(opt)
try:
self._get_cached_item(opt, path=path,
force_properties=frozenset(('mandatory',)))
except PropertiesOptionError as err:
if err.proptype == ['mandatory']:
yield path
self.reset_cache(False)
descr = self._getcontext().cfgimpl_get_description()
ret = list(_mandatory_warnings(descr))
self.reset_cache(False)
return ret
def force_cache(self):
"""parse all option to force data in cache
"""
context = self.context()
if not 'cache' in context.cfgimpl_get_settings():
raise ConfigError(_('can force cache only if cache '
'is actived in config'))
#remove all cached properties and value to update "expired" time
context.cfgimpl_reset_cache()
for path in context.cfgimpl_get_description().impl_getpaths(
include_groups=True):
try:
context.getattr(path)
except PropertiesOptionError:
pass
def __getstate__(self):
return {'_p_': self._p_}
def _impl_setstate(self, storage):
self._p_._storage = storage
def __setstate__(self, states):
self._p_ = states['_p_']
# ____________________________________________________________
# multi types
class Multi(list):
"""multi options values container
that support item notation for the values of multi options"""
__slots__ = ('opt', 'path', 'context')
def __init__(self, value, context, opt, path):
"""
: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
"""
if isinstance(value, Multi):
raise ValueError(_('{0} is already a Multi ').format(opt.impl_getname()))
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]
super(Multi, self).__init__(value)
def _getcontext(self):
"""context could be None, we need to test it
context is None only if all reference to `Config` object is deleted
(for example we delete a `Config` and we manipulate a reference to
old `SubConfig`, `Values`, `Multi` or `Settings`)
"""
context = self.context()
if context is None:
raise ConfigError(_('the context does not exist anymore'))
return context
def __setitem__(self, index, value):
self._validate(value, index)
#assume not checking mandatory property
super(Multi, self).__setitem__(index, value)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
self)
def __repr__(self, *args, **kwargs):
print args, kwargs
return super(Multi, self).__repr__(*args, **kwargs)
def __getitem__(self, y):
return super(Multi, self).__getitem__(y)
def append(self, value=undefined, force=False, setitem=True):
"""the list value can be updated (appened)
only if the option is a master
"""
values = self._getcontext().cfgimpl_get_values()
if not force:
if self.opt.impl_is_master_slaves('slave'):
raise SlaveError(_("cannot append a value on a multi option {0}"
" which is a slave").format(self.opt.impl_getname()))
index = self.__len__()
if value is undefined:
try:
value = values._get_validated_value(self.opt, self.path,
True, False, None, True,
index=index)
except IndexError:
value = None
self._validate(value, index)
super(Multi, self).append(value)
if setitem:
values._setvalue(self.opt, self.path, self,
validate_properties=not force)
def sort(self, cmp=None, key=None, reverse=False):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot sort multi option {0} if master or slave"
"").format(self.opt.impl_getname()))
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._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
self)
def reverse(self):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot reverse multi option {0} if master or "
"slave").format(self.opt.impl_getname()))
super(Multi, self).reverse()
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
self)
def insert(self, index, obj):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot insert multi option {0} if master or "
"slave").format(self.opt.impl_getname()))
super(Multi, self).insert(index, obj)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
self)
def extend(self, iterable):
if self.opt.impl_is_master_slaves():
raise SlaveError(_("cannot extend multi option {0} if master or "
"slave").format(self.opt.impl_getname()))
super(Multi, self).extend(iterable)
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
self)
def _validate(self, value, force_index):
if value is not None:
try:
self.opt.impl_validate(value, context=self._getcontext(),
force_index=force_index)
except ValueError as err:
raise ValueError(_("invalid value {0} "
"for option {1}: {2}"
"").format(str(value),
self.opt.impl_getname(), err))
def pop(self, index, force=False):
"""the list value can be updated (poped)
only if the option is a master
: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
"""
context = self._getcontext()
if not force:
if self.opt.impl_is_master_slaves('slave'):
raise SlaveError(_("cannot pop a value on a multi option {0}"
" which is a slave").format(self.opt.impl_getname()))
if self.opt.impl_is_master_slaves('master'):
self.opt.impl_get_master_slaves().pop(
context.cfgimpl_get_values(), index)
#set value without valid properties
ret = super(Multi, self).pop(index)
context.cfgimpl_get_values()._setvalue(self.opt, self.path, self,
validate_properties=not force)
return ret