tiramisu/tiramisu/value.py

256 lines
9.8 KiB
Python
Raw Normal View History

2013-02-07 16:20:21 +01:00
# -*- coding: utf-8 -*-
"takes care of the option's values and multi values"
2013-02-21 17:07:00 +01:00
# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
2013-02-07 16:20:21 +01:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# 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
2013-02-22 11:09:17 +01:00
from tiramisu.setting import owners, multitypes
2013-02-07 16:20:21 +01:00
2013-02-22 11:09:17 +01:00
class Values(object):
def __init__(self, context):
2013-02-21 17:07:00 +01:00
"""
Initializes the values's dict.
:param context: the context is the home config's values and properties
"""
2013-02-07 16:20:21 +01:00
self.owners = {}
"Config's root indeed is in charge of the `Option()`'s values"
self.values = {}
self.previous_values = {}
2013-02-22 11:09:17 +01:00
self.masters = {}
self.slaves = {}
self.context = context
2013-02-07 16:20:21 +01:00
2013-02-25 15:12:09 +01:00
def _get_multitype(self, opt):
2013-02-25 14:27:12 +01:00
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"
2013-02-22 11:09:17 +01:00
if opt not in self.values:
if opt.is_multi():
2013-02-25 15:12:09 +01:00
multitype = self._get_multitype(opt)
2013-02-22 11:09:17 +01:00
return Multi(opt.getdefault(), self.context, opt, multitype)
else:
return opt.getdefault()
return self.values[opt]
2013-02-07 16:20:21 +01:00
2013-02-21 17:07:00 +01:00
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]
else:
old_value = None
if type(old_value) == Multi:
self.previous_values[opt] = list(old_value)
else:
self.previous_values[opt] = old_value
def _is_empty(self, opt, value=None):
"convenience method to know if an option is empty"
2013-02-21 17:07:00 +01:00
if value is not None:
return False
2013-02-26 14:31:45 +01:00
if (not opt.is_multi() and value == None) or \
(opt.is_multi() and (value == [] or \
None in self._get_value(opt))):
return True
return False
2013-02-07 16:20:21 +01:00
2013-02-26 14:31:45 +01:00
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
2013-02-21 17:07:00 +01:00
def _test_mandatory(self, opt, value=None):
# mandatory options
mandatory = self.context._cfgimpl_settings.mandatory
if opt.is_mandatory() and mandatory:
2013-02-21 17:07:00 +01:00
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))
2013-02-21 17:07:00 +01:00
def fill_multi(self, opt, result):
"""fills a multi option with default and calculated values
"""
value = self._get_value(opt)
if not isinstance(result, list):
_result = [result]
else:
_result = result
2013-02-25 15:12:09 +01:00
multitype = self._get_multitype(opt)
2013-02-25 14:27:12 +01:00
return Multi(_result, self.context, opt, multitype)
def __getitem__(self, opt):
# options with callbacks
2013-02-21 17:07:00 +01:00
value = self._get_value(opt)
if opt.has_callback():
if (not opt.is_frozen() or \
not opt.is_forced_on_freeze()) and \
2013-02-25 14:27:12 +01:00
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():
2013-02-25 14:27:12 +01:00
value = self.fill_multi(opt, result)
else:
# this result **shall not** be a list
if isinstance(result, list):
2013-02-21 17:07:00 +01:00
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))
# frozen and force default
if not opt.has_callback() and opt.is_forced_on_freeze():
value = opt.getdefault()
if opt.is_multi():
2013-02-21 17:07:00 +01:00
value = self.fill_multi(opt, value)
self._test_mandatory(opt, value)
return value
def __setitem__(self, opt, value):
2013-02-22 11:09:17 +01:00
if opt in self.masters:
masterlen = len(value)
for slave in self.masters[opt]:
2013-02-25 15:52:10 +01:00
value_slave = self._get_value(slave)
if len(value_slave) > masterlen:
2013-02-22 11:09:17 +01:00
raise MultiTypeError("invalid len for the slave: {0}"
" which has {1} as master".format(slave._name,
opt._name))
2013-02-25 15:52:10 +01:00
elif len(value_slave) < masterlen:
for num in range(0, masterlen - len(value_slave)):
value_slave.append(None, force=True)
2013-02-22 11:09:17 +01:00
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))
2013-02-22 11:09:17 +01:00
self.setitem(opt, value)
def setitem(self, opt, value):
2013-02-21 17:07:00 +01:00
self.set_previous_value(opt)
self.values[opt] = value
2013-02-21 17:07:00 +01:00
self.setowner(opt, self.context._cfgimpl_settings.getowner())
def __contains__(self, opt):
return opt in self.values
#____________________________________________________________
def setowner(self, opt, owner):
2013-02-21 17:07:00 +01:00
if isinstance(owner, owners.Owner):
self.owners[opt] = owner
else:
raise OptionValueError("Bad owner: " + str(owner))
def getowner(self, opt):
return self.owners.get(opt, owners.default)
2013-02-21 17:07:00 +01:00
# ____________________________________________________________
# multi types
class Multi(list):
"""multi options values container
that support item notation for the values of multi options"""
2013-02-22 11:09:17 +01:00
def __init__(self, lst, context, opt, multitype):
"""
:param lst: the Multi wraps a list value
2013-02-21 17:07:00 +01:00
:param context: the home config that has the settings and 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
2013-02-21 17:07:00 +01:00
self.multitype = multitype
super(Multi, self).__init__(lst)
2013-02-22 11:09:17 +01:00
if multitype == multitypes.master:
self.slaves = context._cfgimpl_values.masters[opt]
else:
self.slaves = None
def __setitem__(self, key, value):
2013-02-22 11:09:17 +01:00
self._validate(value)
self.values[self.opt] = self
super(Multi, self).__setitem__(key, value)
2013-02-22 11:09:17 +01:00
def append(self, value, force=False):
"""the list value can be updated (appened)
only if the option is a master
"""
2013-02-22 11:09:17 +01:00
if not force:
if self.multitype == multitypes.slave:
raise MultiTypeError("cannot append a value on a multi option {0}"
2013-02-25 15:52:10 +01:00
" which is a slave".format(self.opt._name))
2013-02-22 11:09:17 +01:00
elif self.multitype == multitypes.master:
for slave in self.slaves:
self.values[slave].append(None, force=True)
self._validate(value)
self.values.setitem(self.opt, self)
super(Multi, self).append(value)
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))
2013-02-22 11:09:17 +01:00
def pop(self, key, 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
"""
2013-02-22 11:09:17 +01:00
if not force:
if self.multitype == multitypes.slave:
raise MultiTypeError("cannot append a value on a multi option {0}"
2013-02-25 15:52:10 +01:00
" which is a slave".format(self.opt._name))
2013-02-22 11:09:17 +01:00
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)