tiramisu/tiramisu/value.py

186 lines
7.3 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-07 16:20:21 +01:00
# Copyright (C) 2012 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 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
from tiramisu.setting import owners
2013-02-07 16:20:21 +01:00
class OptionValues(object):
def __init__(self, context):
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 = {}
self.context = context
2013-02-07 16:20:21 +01:00
def _get_value(self, opt):
"special case for the multis: they never return None"
if opt.is_multi():
if opt not in self.values:
# FIXME : default value for a multi, we shall work on groups
return Multi(opt.getdefault(), self.context, opt)
else:
if opt not in self.values:
return opt.getdefault()
return self.values[opt]
2013-02-07 16:20:21 +01:00
def _is_empty(self, opt):
"convenience method to know if an option is empty"
if (not opt.is_multi() and self._get_value(opt) == None) or \
(opt.is_multi() and (self._get_value(opt) == [] or \
None in self._get_value(opt))):
return True
return False
2013-02-07 16:20:21 +01:00
def _test_mandatory(self, opt):
# mandatory options
mandatory = self.context._cfgimpl_settings.mandatory
if opt.is_mandatory() and mandatory:
if self._is_empty(opt) 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, use_default_multi=False, default_multi=None):
"""fills a multi option with default and calculated values
"""
value = self._get_value(opt)
if not isinstance(result, list):
_result = [result]
else:
_result = result
return Multi(_result, self.context, opt)
def __getitem__(self, opt):
# options with callbacks
if opt.has_callback():
if (not opt.is_frozen() or \
not opt.is_forced_on_freeze()) and \
not opt.is_default_owner(self):
return self._get_value(opt)
try:
result = opt.getcallback_value(
self.context)
except NoValueReturned, err:
pass
else:
if opt.is_multi():
#FIXME revoir les multis
_result = 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))
_result = result
if _result != None and not opt.validate(_result,
self.context._cfgimpl_settings.validator):
raise ConfigError('invalid calculated value returned'
' for option {0}'.format(name))
self.values[opt] = _result
self.owners[opt] = owners.default
# frozen and force default
if not opt.has_callback() and opt.is_forced_on_freeze():
value = opt.getdefault()
if opt.is_multi():
#FIXME le fill_multi
value = self.fill_multi(opt, value,
use_default_multi=True,
default_multi=opt.getdefault_multi())
self.values[opt] = value
self.owners[opt] = owners.default
self._test_mandatory(opt)
value = self._get_value(opt)
return value
def __setitem__(self, opt, value):
if opt in self.values:
old_value = self.values[opt]
else:
old_value = None
if type(old_value) == Multi:
self.previous_values[opt] = list(value)
else:
self.previous_values[opt] = value
self.values[opt] = value
def __contains__(self, opt):
return opt in self.values
#____________________________________________________________
def setowner(self, opt, owner):
pass
def getowner(self, opt):
return self.owners.get(opt, owners.default)
# ____________________________________________________________
# 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):
"""
:param lst: the Multi wraps a list value
:param context: the context 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
super(Multi, self).__init__(lst)
def __setitem__(self, key, value):
self._setvalue(value, key, who=self.settings.getowner())
def append(self, value):
"""the list value can be updated (appened)
only if the option is a master
"""
self._setvalue(value, who=self.settings.getowner(self.opt))
def _setvalue(self, value, key=None, who=None):
if value != None:
if not self.opt._validate(value):
raise ConfigError("invalid value {0} "
"for option {1}".format(str(value), self.opt._name))
oldvalue = list(self)
if key is None:
super(Multi, self).append(value)
else:
super(Multi, self).__setitem__(key, value)
if who != None:
if not isinstance(who, owners.Owner):
raise TypeError("invalid owner {0} for the value {1}".format(
str(who), str(value)))
self.values.setowner(self.opt, getattr(owners, who))
self.values.previous_values[self.opt] = oldvalue
def pop(self, key):
"""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
"""
self.values.setowner(opt, self.settings.get_owner())
self.values.previous_values[self.opt] = list(self)
return super(Multi, self).pop(key)