2012-07-13 09:37:35 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"option types and option description for the configuration management"
|
2012-07-25 09:04:22 +02:00
|
|
|
|
# Copyright (C) 2012 Team tiramisu (see AUTHORS for all contributors)
|
2012-07-13 09:37:35 +02: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
|
|
|
|
|
#
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# The original `Config` design model is unproudly borrowed from
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
|
|
|
|
|
# the whole pypy projet is under MIT licence
|
|
|
|
|
# ____________________________________________________________
|
2012-11-19 09:51:40 +01:00
|
|
|
|
from types import FunctionType
|
2012-08-13 09:32:33 +02:00
|
|
|
|
from tiramisu.basetype import HiddenBaseType, DisabledBaseType
|
2012-10-05 16:00:07 +02:00
|
|
|
|
from tiramisu.error import (ConfigError, ConflictConfigError, NotFoundError,
|
2012-10-16 15:09:52 +02:00
|
|
|
|
RequiresError, RequirementRecursionError, MandatoryError,
|
|
|
|
|
PropertiesOptionError)
|
2012-10-15 15:06:41 +02:00
|
|
|
|
from tiramisu.autolib import carry_out_calculation
|
2012-12-10 14:10:05 +01:00
|
|
|
|
from tiramisu.setting import settings, groups, owners
|
2012-10-15 15:06:41 +02:00
|
|
|
|
|
2012-09-20 10:51:35 +02:00
|
|
|
|
requires_actions = [('hide', 'show'), ('enable', 'disable'), ('freeze', 'unfreeze')]
|
|
|
|
|
|
|
|
|
|
available_actions = []
|
|
|
|
|
reverse_actions = {}
|
|
|
|
|
for act1, act2 in requires_actions:
|
|
|
|
|
available_actions.extend([act1, act2])
|
|
|
|
|
reverse_actions[act1] = act2
|
|
|
|
|
reverse_actions[act2] = act1
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# ____________________________________________________________
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# multi types
|
2012-11-12 12:06:58 +01:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
class Multi(list):
|
2012-12-04 15:18:13 +01:00
|
|
|
|
"""multi options values container
|
|
|
|
|
that support item notation for the values of multi options"""
|
2013-01-10 12:03:59 +01:00
|
|
|
|
def __init__(self, lst, config, opt, force_append=True):
|
2012-12-04 15:18:13 +01:00
|
|
|
|
"""
|
2013-01-10 12:03:59 +01:00
|
|
|
|
:param lst: the Multi wraps a list value
|
2012-12-04 15:18:13 +01:00
|
|
|
|
:param config: the parent config
|
|
|
|
|
:param opt: the option object that have this Multi value
|
2013-01-10 12:03:59 +01:00
|
|
|
|
:param force_append: - True to append child value with master's one
|
|
|
|
|
- False to force lst value
|
2012-12-04 15:18:13 +01:00
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self.config = config
|
2012-12-04 15:18:13 +01:00
|
|
|
|
self.opt = opt
|
2013-01-10 12:03:59 +01:00
|
|
|
|
if force_append and self.opt.is_master(config):
|
2012-12-05 11:12:04 +01:00
|
|
|
|
# we pass the list at the list type's init
|
|
|
|
|
# because a normal init cannot return anything
|
2012-12-05 10:54:32 +01:00
|
|
|
|
super(Multi, self).__init__(lst)
|
2012-12-05 11:12:04 +01:00
|
|
|
|
# we add the slaves without modifying the master
|
2012-12-05 10:54:32 +01:00
|
|
|
|
for l in lst:
|
2013-01-30 14:51:29 +01:00
|
|
|
|
self.append(l, add_master=False)
|
2012-12-05 10:54:32 +01:00
|
|
|
|
else:
|
2013-01-10 12:03:59 +01:00
|
|
|
|
if force_append:
|
|
|
|
|
self.config._valid_len(self.opt._name, lst)
|
2012-12-05 10:54:32 +01:00
|
|
|
|
super(Multi, self).__init__(lst)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __setitem__(self, key, value):
|
2013-01-30 09:19:48 +01:00
|
|
|
|
self._setvalue(value, key, who=settings.get_owner())
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
2012-12-05 10:54:32 +01:00
|
|
|
|
def append(self, value, add_master=True):
|
2012-12-04 15:18:13 +01:00
|
|
|
|
"""the list value can be updated (appened)
|
2012-12-05 11:12:04 +01:00
|
|
|
|
only if the option is a master
|
|
|
|
|
:param add_master: adds slaves without modifiying the master option
|
|
|
|
|
if True, adds slaves **and** the master option
|
|
|
|
|
"""
|
2012-12-04 15:18:13 +01:00
|
|
|
|
try:
|
|
|
|
|
master = self.config._cfgimpl_descr.get_master_name()
|
|
|
|
|
if master != self.opt._name:
|
|
|
|
|
raise IndexError("in a group with a master, you mustn't add "
|
|
|
|
|
"a value in a slave's Multi value")
|
|
|
|
|
except TypeError:
|
2013-01-31 16:57:15 +01:00
|
|
|
|
# Not a master/slaves
|
2013-01-30 09:19:48 +01:00
|
|
|
|
self._setvalue(value, who=settings.get_owner())
|
2013-01-31 16:57:15 +01:00
|
|
|
|
return
|
|
|
|
|
|
2012-12-04 16:22:39 +01:00
|
|
|
|
multis = []
|
2013-01-30 14:51:29 +01:00
|
|
|
|
for opt in self.config._cfgimpl_descr._children:
|
2013-01-30 18:03:15 +01:00
|
|
|
|
if isinstance(opt, OptionDescription):
|
|
|
|
|
continue
|
2013-01-30 14:51:29 +01:00
|
|
|
|
multi = self.config._cfgimpl_values[opt._name]
|
2012-12-04 16:22:39 +01:00
|
|
|
|
if master == multi.opt._name:
|
2012-12-05 10:54:32 +01:00
|
|
|
|
if add_master:
|
2013-01-30 09:19:48 +01:00
|
|
|
|
multi._setvalue(value, who=settings.get_owner())
|
2013-01-31 17:01:10 +01:00
|
|
|
|
elif len(multi) == 0 or len(multi) < len(self):
|
2013-01-10 12:03:59 +01:00
|
|
|
|
multi._append_default()
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2013-01-10 12:03:59 +01:00
|
|
|
|
def _append_default(self):
|
|
|
|
|
default_value = self.opt.getdefault_multi()
|
|
|
|
|
self._setvalue(default_value)
|
|
|
|
|
|
2012-12-04 15:18:13 +01:00
|
|
|
|
def _setvalue(self, value, key=None, who=None):
|
2012-11-15 10:55:14 +01:00
|
|
|
|
if value != None:
|
2012-12-04 15:18:13 +01:00
|
|
|
|
if not self.opt._validate(value):
|
2012-11-15 10:55:14 +01:00
|
|
|
|
raise ConfigError("invalid value {0} "
|
2012-12-04 15:18:13 +01:00
|
|
|
|
"for option {1}".format(str(value), self.opt._name))
|
2012-07-23 14:30:06 +02:00
|
|
|
|
oldvalue = list(self)
|
|
|
|
|
if key is None:
|
2013-01-30 09:19:48 +01:00
|
|
|
|
super(Multi, self).append(value)
|
2012-07-23 14:30:06 +02:00
|
|
|
|
else:
|
2013-01-30 09:19:48 +01:00
|
|
|
|
super(Multi, self).__setitem__(key, value)
|
2012-12-04 15:18:13 +01:00
|
|
|
|
if who != None:
|
2012-12-11 16:22:02 +01:00
|
|
|
|
if not isinstance(who, owners.Owner):
|
|
|
|
|
raise TypeError("invalid owner {0} for the value {1}".format(
|
|
|
|
|
str(who), str(value)))
|
2012-12-11 11:18:53 +01:00
|
|
|
|
self.opt.setowner(self.config, getattr(owners, who))
|
2012-12-04 15:18:13 +01:00
|
|
|
|
self.config._cfgimpl_previous_values[self.opt._name] = oldvalue
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-23 14:30:06 +02:00
|
|
|
|
def pop(self, key):
|
2012-12-04 15:18:13 +01:00
|
|
|
|
"""the list value can be updated (poped)
|
|
|
|
|
only if the option is a master
|
2013-01-30 09:19:48 +01:00
|
|
|
|
|
|
|
|
|
:param key: index of the element to pop
|
|
|
|
|
:return: the requested element
|
|
|
|
|
|
2012-12-04 15:18:13 +01:00
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
master = self.config._cfgimpl_descr.get_master_name()
|
|
|
|
|
if master != self.opt._name:
|
|
|
|
|
raise IndexError("in a group with a master, you mustn't remove "
|
|
|
|
|
"a value in a slave's Multi value")
|
|
|
|
|
except TypeError:
|
2012-12-04 16:22:39 +01:00
|
|
|
|
return self._pop(key)
|
|
|
|
|
|
|
|
|
|
multis = []
|
|
|
|
|
for name, multi in self.config:
|
|
|
|
|
multis.append(multi)
|
|
|
|
|
for multi in multis:
|
|
|
|
|
if master == multi.opt._name:
|
|
|
|
|
ret = multi._pop(key)
|
|
|
|
|
else:
|
|
|
|
|
change_who = False
|
|
|
|
|
# the value owner has to be updated because
|
|
|
|
|
# the default value doesn't have the same length
|
|
|
|
|
# of the new value
|
|
|
|
|
if len(multi.opt.getdefault()) >= len(multi):
|
|
|
|
|
change_who = True
|
|
|
|
|
multi._pop(key, change_who=change_who)
|
2013-01-30 09:19:48 +01:00
|
|
|
|
if ret not in locals():
|
|
|
|
|
raise ConfigError('Unexpected multi pop error: ret must be defined')
|
2012-12-04 16:22:39 +01:00
|
|
|
|
return ret
|
2012-12-04 15:18:13 +01:00
|
|
|
|
|
|
|
|
|
def _pop(self, key, change_who=True):
|
|
|
|
|
if change_who:
|
2012-12-11 11:18:53 +01:00
|
|
|
|
self.opt.setowner(self.config, settings.get_owner())
|
2013-01-30 09:19:48 +01:00
|
|
|
|
self.config._cfgimpl_previous_values[self.opt._name] = list(self)
|
2012-11-22 10:19:13 +01:00
|
|
|
|
return super(Multi, self).pop(key)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# ____________________________________________________________
|
|
|
|
|
#
|
2012-08-13 09:32:33 +02:00
|
|
|
|
class Option(HiddenBaseType, DisabledBaseType):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
2012-11-20 17:14:58 +01:00
|
|
|
|
Abstract base class for configuration option's.
|
|
|
|
|
|
|
|
|
|
Reminder: an Option object is **not** a container for the value
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
2012-11-20 17:14:58 +01:00
|
|
|
|
#freeze means: cannot modify the value of an Option once set
|
2012-07-13 09:37:35 +02:00
|
|
|
|
_frozen = False
|
2012-11-20 17:14:58 +01:00
|
|
|
|
#if an Option has been frozen, shall return the default value
|
2012-08-13 16:06:02 +02:00
|
|
|
|
_force_default_on_freeze = False
|
2012-10-05 16:00:07 +02:00
|
|
|
|
def __init__(self, name, doc, default=None, default_multi=None,
|
|
|
|
|
requires=None, mandatory=False, multi=False, callback=None,
|
2012-11-19 09:51:40 +01:00
|
|
|
|
callback_params=None, validator=None, validator_args={}):
|
2012-11-12 12:06:58 +01:00
|
|
|
|
"""
|
2012-11-20 17:14:58 +01:00
|
|
|
|
:param name: the option's name
|
|
|
|
|
:param doc: the option's description
|
|
|
|
|
:param default: specifies the default value of the option,
|
|
|
|
|
for a multi : ['bla', 'bla', 'bla']
|
2012-11-12 12:06:58 +01:00
|
|
|
|
:param default_multi: 'bla' (used in case of a reset to default only at
|
2012-11-20 17:14:58 +01:00
|
|
|
|
a given index)
|
|
|
|
|
:param requires: is a list of names of options located anywhere
|
|
|
|
|
in the configuration.
|
|
|
|
|
:param multi: if true, the option's value is a list
|
|
|
|
|
:param callback: the name of a function. If set, the function's output
|
|
|
|
|
is responsible of the option's value
|
|
|
|
|
:param callback_params: the callback's parameter
|
|
|
|
|
:param validator: the name of a function wich stands for a custom
|
|
|
|
|
validation of the value
|
|
|
|
|
:param validator_args: the validator's parameters
|
2012-11-12 12:06:58 +01:00
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._name = name
|
|
|
|
|
self.doc = doc
|
|
|
|
|
self._requires = requires
|
|
|
|
|
self._mandatory = mandatory
|
|
|
|
|
self.multi = multi
|
2012-11-19 09:51:40 +01:00
|
|
|
|
self._validator = None
|
|
|
|
|
self._validator_args = None
|
|
|
|
|
if validator is not None:
|
|
|
|
|
if type(validator) != FunctionType:
|
|
|
|
|
raise TypeError("validator must be a function")
|
|
|
|
|
self._validator = validator
|
|
|
|
|
if validator_args is not None:
|
|
|
|
|
self._validator_args = validator_args
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if not self.multi and default_multi is not None:
|
|
|
|
|
raise ConfigError("a default_multi is set whereas multi is False"
|
2012-10-05 16:00:07 +02:00
|
|
|
|
" in option: {0}".format(name))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if default_multi is not None and not self._validate(default_multi):
|
|
|
|
|
raise ConfigError("invalid default_multi value {0} "
|
|
|
|
|
"for option {1}".format(str(default_multi), name))
|
|
|
|
|
self.default_multi = default_multi
|
|
|
|
|
#if self.multi and default_multi is None:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# _cfgimpl_warnings[name] = DefaultMultiWarning
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if callback is not None and (default is not None or default_multi is not None):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
raise ConfigError("defaut values not allowed if option: {0} "
|
2012-07-13 09:37:35 +02:00
|
|
|
|
"is calculated".format(name))
|
|
|
|
|
self.callback = callback
|
|
|
|
|
if self.callback is None and callback_params is not None:
|
|
|
|
|
raise ConfigError("params defined for a callback function but"
|
2012-10-05 16:00:07 +02:00
|
|
|
|
" no callback defined yet for option {0}".format(name))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self.callback_params = callback_params
|
|
|
|
|
if self.multi == True:
|
|
|
|
|
if default == None:
|
|
|
|
|
default = []
|
2012-11-19 09:51:40 +01:00
|
|
|
|
if not isinstance(default, list):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
raise ConfigError("invalid default value {0} "
|
|
|
|
|
"for option {1} : not list type".format(str(default), name))
|
2012-11-19 09:51:40 +01:00
|
|
|
|
if not self.validate(default, False):
|
|
|
|
|
raise ConfigError("invalid default value {0} "
|
|
|
|
|
"for option {1}".format(str(default), name))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
else:
|
2012-11-19 09:51:40 +01:00
|
|
|
|
if default != None and not self.validate(default, False):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
raise ConfigError("invalid default value {0} "
|
2012-07-13 09:37:35 +02:00
|
|
|
|
"for option {1}".format(str(default), name))
|
|
|
|
|
self.default = default
|
2012-08-13 11:48:25 +02:00
|
|
|
|
self.properties = [] # 'hidden', 'disabled'...
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-11-19 09:51:40 +01:00
|
|
|
|
def validate(self, value, validate=True):
|
|
|
|
|
"""
|
|
|
|
|
:param value: the option's value
|
|
|
|
|
:param validate: if true enables ``self._validator`` validation
|
|
|
|
|
"""
|
|
|
|
|
# generic calculation
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if self.multi == False:
|
|
|
|
|
# None allows the reset of the value
|
|
|
|
|
if value != None:
|
2012-11-22 11:53:51 +01:00
|
|
|
|
# customizing the validator
|
|
|
|
|
if validate and self._validator is not None and \
|
|
|
|
|
not self._validator(value, **self._validator_args):
|
|
|
|
|
return False
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self._validate(value)
|
|
|
|
|
else:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
if not isinstance(value, list):
|
|
|
|
|
raise ConfigError("invalid value {0} "
|
2012-07-13 09:37:35 +02:00
|
|
|
|
"for option {1} which must be a list".format(value,
|
|
|
|
|
self._name))
|
|
|
|
|
for val in value:
|
2012-11-22 11:53:51 +01:00
|
|
|
|
# None allows the reset of the value
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if val != None:
|
2012-11-22 11:53:51 +01:00
|
|
|
|
# customizing the validator
|
|
|
|
|
if validate and self._validator is not None and \
|
|
|
|
|
not self._validator(val, **self._validator_args):
|
|
|
|
|
return False
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if not self._validate(val):
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2012-11-29 11:40:52 +01:00
|
|
|
|
def getdefault(self, default_multi=False):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"accessing the default value"
|
2012-11-29 11:40:52 +01:00
|
|
|
|
if default_multi == False or not self.is_multi():
|
|
|
|
|
return self.default
|
|
|
|
|
else:
|
|
|
|
|
return self.getdefault_multi()
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
2012-11-15 14:59:36 +01:00
|
|
|
|
def getdefault_multi(self):
|
|
|
|
|
"accessing the default value for a multi"
|
|
|
|
|
return self.default_multi
|
|
|
|
|
|
2012-09-11 15:18:38 +02:00
|
|
|
|
def is_empty_by_default(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"no default value has been set yet"
|
2012-09-11 15:18:38 +02:00
|
|
|
|
if ((not self.is_multi() and self.default == None) or
|
2012-11-22 10:19:13 +01:00
|
|
|
|
(self.is_multi() and (self.default == [] or None in self.default))):
|
2012-09-11 15:18:38 +02:00
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2012-08-13 16:06:02 +02:00
|
|
|
|
def force_default(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"if an Option has been frozen, shall return the default value"
|
2012-08-13 16:06:02 +02:00
|
|
|
|
self._force_default_on_freeze = True
|
|
|
|
|
|
2012-08-14 10:55:08 +02:00
|
|
|
|
def hascallback_and_isfrozen():
|
|
|
|
|
return self._frozen and self.has_callback()
|
|
|
|
|
|
2012-08-13 16:06:02 +02:00
|
|
|
|
def is_forced_on_freeze(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"if an Option has been frozen, shall return the default value"
|
2012-08-14 10:55:08 +02:00
|
|
|
|
return self._frozen and self._force_default_on_freeze
|
2012-08-13 16:06:02 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def getdoc(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"accesses the Option's doc"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self.doc
|
|
|
|
|
|
|
|
|
|
def getcallback(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"a callback is only a link, the name of an external hook"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self.callback
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-27 11:46:27 +02:00
|
|
|
|
def has_callback(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"to know if a callback has been defined or not"
|
2012-07-27 11:46:27 +02:00
|
|
|
|
if self.callback == None:
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
2012-10-15 15:06:41 +02:00
|
|
|
|
def getcallback_value(self, config):
|
|
|
|
|
return carry_out_calculation(self._name,
|
2012-10-16 15:09:52 +02:00
|
|
|
|
option=self, config=config)
|
2012-10-15 15:06:41 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def getcallback_params(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"if a callback has been defined, returns his arity"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self.callback_params
|
|
|
|
|
|
|
|
|
|
def setowner(self, config, owner):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
|
|
|
|
:param config: *must* be only the **parent** config
|
2012-11-20 17:14:58 +01:00
|
|
|
|
(not the toplevel config)
|
2012-12-10 14:10:05 +01:00
|
|
|
|
:param owner: is a **real** owner, that is an object
|
|
|
|
|
that lives in setting.owners
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
name = self._name
|
2012-12-10 14:10:05 +01:00
|
|
|
|
if not isinstance(owner, owners.Owner):
|
|
|
|
|
raise ConfigError("invalid type owner for option: {0}".format(
|
|
|
|
|
str(name)))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
config._cfgimpl_value_owners[name] = owner
|
|
|
|
|
|
|
|
|
|
def getowner(self, config):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"config *must* be only the **parent** config (not the toplevel config)"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return config._cfgimpl_value_owners[self._name]
|
|
|
|
|
|
2012-11-16 10:04:25 +01:00
|
|
|
|
def reset(self, config):
|
2012-11-12 12:06:58 +01:00
|
|
|
|
"""resets the default value and owner
|
|
|
|
|
"""
|
2012-12-10 14:10:05 +01:00
|
|
|
|
config.setoption(self._name, self.getdefault(), owners.default)
|
2012-11-12 12:06:58 +01:00
|
|
|
|
|
2012-11-15 10:55:14 +01:00
|
|
|
|
def is_default_owner(self, config):
|
2012-11-08 09:03:28 +01:00
|
|
|
|
"""
|
|
|
|
|
:param config: *must* be only the **parent** config
|
2012-11-20 17:14:58 +01:00
|
|
|
|
(not the toplevel config)
|
2012-11-08 09:03:28 +01:00
|
|
|
|
:return: boolean
|
|
|
|
|
"""
|
2012-12-11 11:18:53 +01:00
|
|
|
|
return self.getowner(config) == owners.default
|
2012-10-15 15:06:41 +02:00
|
|
|
|
|
2012-12-10 14:10:05 +01:00
|
|
|
|
def setoption(self, config, value):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""changes the option's value with the value_owner's who
|
|
|
|
|
:param config: the parent config is necessary here to store the value
|
2012-12-10 14:10:05 +01:00
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
name = self._name
|
2012-11-19 09:51:40 +01:00
|
|
|
|
rootconfig = config._cfgimpl_get_toplevel()
|
2012-11-19 10:45:03 +01:00
|
|
|
|
if not self.validate(value, settings.validator):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
raise ConfigError('invalid value %s for option %s' % (value, name))
|
2012-09-11 13:28:37 +02:00
|
|
|
|
if self.is_mandatory():
|
|
|
|
|
# value shall not be '' for a mandatory option
|
|
|
|
|
# so '' is considered as being None
|
2012-09-11 15:18:38 +02:00
|
|
|
|
if not self.is_multi() and value == '':
|
2012-09-11 13:28:37 +02:00
|
|
|
|
value = None
|
2012-09-11 15:18:38 +02:00
|
|
|
|
if self.is_multi() and '' in value:
|
|
|
|
|
value = Multi([{'': None}.get(i, i) for i in value], config, self)
|
2012-11-19 10:45:03 +01:00
|
|
|
|
if settings.is_mandatory() and ((self.is_multi() and value == []) or \
|
2012-09-11 13:28:37 +02:00
|
|
|
|
(not self.is_multi() and value is None)):
|
2012-09-19 09:31:02 +02:00
|
|
|
|
raise MandatoryError('cannot change the value to %s for '
|
2012-09-11 13:28:37 +02:00
|
|
|
|
'option %s' % (value, name))
|
|
|
|
|
if name not in config._cfgimpl_values:
|
|
|
|
|
raise AttributeError('unknown option %s' % (name))
|
2012-09-19 09:31:02 +02:00
|
|
|
|
|
2012-11-19 10:45:03 +01:00
|
|
|
|
if settings.is_frozen() and self.is_frozen():
|
2012-09-19 09:31:02 +02:00
|
|
|
|
raise TypeError('cannot change the value to %s for '
|
2012-11-19 09:51:40 +01:00
|
|
|
|
'option %s this option is frozen' % (str(value), name))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
apply_requires(self, config)
|
2012-07-23 14:30:06 +02:00
|
|
|
|
if type(config._cfgimpl_values[name]) == Multi:
|
|
|
|
|
config._cfgimpl_previous_values[name] = list(config._cfgimpl_values[name])
|
|
|
|
|
else:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
config._cfgimpl_previous_values[name] = config._cfgimpl_values[name]
|
2012-07-13 09:37:35 +02:00
|
|
|
|
config._cfgimpl_values[name] = value
|
|
|
|
|
|
|
|
|
|
def getkey(self, value):
|
|
|
|
|
return value
|
2012-12-05 10:54:32 +01:00
|
|
|
|
|
|
|
|
|
def is_master(self, config):
|
|
|
|
|
try:
|
|
|
|
|
self.master = config._cfgimpl_descr.get_master_name()
|
|
|
|
|
except TypeError:
|
|
|
|
|
return False
|
|
|
|
|
return self.master is not None and self.master == self._name
|
2012-08-14 10:55:08 +02:00
|
|
|
|
# ____________________________________________________________
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"freeze utility"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def freeze(self):
|
|
|
|
|
self._frozen = True
|
|
|
|
|
return True
|
|
|
|
|
def unfreeze(self):
|
|
|
|
|
self._frozen = False
|
2012-09-11 13:28:37 +02:00
|
|
|
|
def is_frozen(self):
|
2012-08-14 10:55:08 +02:00
|
|
|
|
return self._frozen
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# ____________________________________________________________
|
|
|
|
|
def is_multi(self):
|
|
|
|
|
return self.multi
|
|
|
|
|
def is_mandatory(self):
|
|
|
|
|
return self._mandatory
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
class ChoiceOption(Option):
|
|
|
|
|
opt_type = 'string'
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-11-16 10:04:25 +01:00
|
|
|
|
def __init__(self, name, doc, values, default=None, default_multi=None,
|
|
|
|
|
requires=None, mandatory=False, multi=False, callback=None,
|
2012-11-19 09:51:40 +01:00
|
|
|
|
callback_params=None, open_values=False, validator=None,
|
|
|
|
|
validator_args={}):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self.values = values
|
2012-07-23 14:30:06 +02:00
|
|
|
|
if open_values not in [True, False]:
|
|
|
|
|
raise ConfigError('Open_values must be a boolean for '
|
|
|
|
|
'{0}'.format(name))
|
|
|
|
|
self.open_values = open_values
|
2012-07-13 09:37:35 +02:00
|
|
|
|
super(ChoiceOption, self).__init__(name, doc, default=default,
|
2012-11-16 10:04:25 +01:00
|
|
|
|
default_multi=default_multi, callback=callback,
|
|
|
|
|
callback_params=callback_params, requires=requires,
|
2012-11-19 09:51:40 +01:00
|
|
|
|
multi=multi, mandatory=mandatory, validator=validator,
|
|
|
|
|
validator_args=validator_args)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
|
|
def _validate(self, value):
|
2012-07-23 14:30:06 +02:00
|
|
|
|
if not self.open_values:
|
|
|
|
|
return value is None or value in self.values
|
|
|
|
|
else:
|
|
|
|
|
return True
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
|
|
class BoolOption(Option):
|
|
|
|
|
opt_type = 'bool'
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def _validate(self, value):
|
|
|
|
|
return isinstance(value, bool)
|
|
|
|
|
|
|
|
|
|
class IntOption(Option):
|
|
|
|
|
opt_type = 'int'
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def _validate(self, value):
|
2012-09-19 09:31:02 +02:00
|
|
|
|
return isinstance(value, int)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
|
|
class FloatOption(Option):
|
|
|
|
|
opt_type = 'float'
|
|
|
|
|
|
|
|
|
|
def _validate(self, value):
|
2012-09-19 09:31:02 +02:00
|
|
|
|
return isinstance(value, float)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
|
|
class StrOption(Option):
|
|
|
|
|
opt_type = 'string'
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def _validate(self, value):
|
|
|
|
|
return isinstance(value, str)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-08-13 10:51:52 +02:00
|
|
|
|
class SymLinkOption(object):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
opt_type = 'symlink'
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-11-30 16:23:40 +01:00
|
|
|
|
def __init__(self, name, path, opt):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._name = name
|
2012-10-05 16:00:07 +02:00
|
|
|
|
self.path = path
|
2012-11-30 16:23:40 +01:00
|
|
|
|
self.opt = opt
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-12-10 14:10:05 +01:00
|
|
|
|
def setoption(self, config, value):
|
2012-11-30 16:23:40 +01:00
|
|
|
|
setattr(config, self.path, value)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
2012-11-30 15:08:34 +01:00
|
|
|
|
def __getattr__(self, name):
|
2012-11-30 16:23:40 +01:00
|
|
|
|
if name in ('_name', 'path', 'opt', 'setoption'):
|
|
|
|
|
return self.__dict__[name]
|
|
|
|
|
else:
|
|
|
|
|
return getattr(self.opt, name)
|
2012-11-30 15:08:34 +01:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
class IPOption(Option):
|
|
|
|
|
opt_type = 'ip'
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def _validate(self, value):
|
|
|
|
|
# by now the validation is nothing but a string, use IPy instead
|
|
|
|
|
return isinstance(value, str)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
class NetmaskOption(Option):
|
|
|
|
|
opt_type = 'netmask'
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def _validate(self, value):
|
|
|
|
|
# by now the validation is nothing but a string, use IPy instead
|
|
|
|
|
return isinstance(value, str)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-08-13 09:32:33 +02:00
|
|
|
|
class OptionDescription(HiddenBaseType, DisabledBaseType):
|
2012-12-06 18:14:57 +01:00
|
|
|
|
"""Config's schema (organisation, group) and container of Options"""
|
|
|
|
|
# the group_type is useful for filtering OptionDescriptions in a config
|
|
|
|
|
group_type = groups.default
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __init__(self, name, doc, children, requires=None):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
|
|
|
|
:param children: is a list of option descriptions (including
|
|
|
|
|
``OptionDescription`` instances for nested namespaces).
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._name = name
|
|
|
|
|
self.doc = doc
|
|
|
|
|
self._children = children
|
|
|
|
|
self._requires = requires
|
|
|
|
|
self._build()
|
2012-08-13 11:48:25 +02:00
|
|
|
|
self.properties = [] # 'hidden', 'disabled'...
|
2012-12-04 12:06:26 +01:00
|
|
|
|
# if this group is a master group, master is set
|
2012-12-06 18:14:57 +01:00
|
|
|
|
# to the master option name. it's just a ref to a name
|
2012-12-04 12:06:26 +01:00
|
|
|
|
self.master = None
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def getdoc(self):
|
|
|
|
|
return self.doc
|
|
|
|
|
|
|
|
|
|
def _build(self):
|
|
|
|
|
for child in self._children:
|
|
|
|
|
setattr(self, child._name, child)
|
|
|
|
|
|
|
|
|
|
def add_child(self, child):
|
|
|
|
|
"dynamically adds a configuration option"
|
|
|
|
|
#Nothing is static. Even the Mona Lisa is falling apart.
|
|
|
|
|
for ch in self._children:
|
|
|
|
|
if isinstance(ch, Option):
|
|
|
|
|
if child._name == ch._name:
|
|
|
|
|
raise ConflictConfigError("existing option : {0}".format(
|
|
|
|
|
child._name))
|
|
|
|
|
self._children.append(child)
|
|
|
|
|
setattr(self, child._name, child)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def update_child(self, child):
|
|
|
|
|
"modification of an existing option"
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# XXX : corresponds to the `redefine`, is it usefull
|
2012-07-13 09:37:35 +02:00
|
|
|
|
pass
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def getkey(self, config):
|
|
|
|
|
return tuple([child.getkey(getattr(config, child._name))
|
|
|
|
|
for child in self._children])
|
|
|
|
|
|
|
|
|
|
def getpaths(self, include_groups=False, currpath=None):
|
|
|
|
|
"""returns a list of all paths in self, recursively
|
|
|
|
|
currpath should not be provided (helps with recursion)
|
|
|
|
|
"""
|
|
|
|
|
if currpath is None:
|
|
|
|
|
currpath = []
|
|
|
|
|
paths = []
|
|
|
|
|
for option in self._children:
|
|
|
|
|
attr = option._name
|
|
|
|
|
if attr.startswith('_cfgimpl'):
|
|
|
|
|
continue
|
2012-09-24 15:58:37 +02:00
|
|
|
|
if isinstance(option, OptionDescription):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if include_groups:
|
|
|
|
|
paths.append('.'.join(currpath + [attr]))
|
|
|
|
|
currpath.append(attr)
|
2012-09-24 15:58:37 +02:00
|
|
|
|
paths += option.getpaths(include_groups=include_groups,
|
2012-07-13 09:37:35 +02:00
|
|
|
|
currpath=currpath)
|
|
|
|
|
currpath.pop()
|
|
|
|
|
else:
|
|
|
|
|
paths.append('.'.join(currpath + [attr]))
|
|
|
|
|
return paths
|
|
|
|
|
# ____________________________________________________________
|
2012-12-04 12:06:26 +01:00
|
|
|
|
def set_group_type(self, group_type, master=None):
|
2012-12-06 18:14:57 +01:00
|
|
|
|
"""sets a given group object to an OptionDescription
|
|
|
|
|
|
2012-12-10 14:38:25 +01:00
|
|
|
|
:param group_type: an instance of `GroupType` or `MasterGroupType`
|
2012-12-06 18:14:57 +01:00
|
|
|
|
that lives in `setting.groups`
|
|
|
|
|
"""
|
2012-12-10 14:38:25 +01:00
|
|
|
|
if isinstance(group_type, groups.GroupType):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self.group_type = group_type
|
2012-12-10 14:38:25 +01:00
|
|
|
|
if isinstance(group_type, groups.MasterGroupType):
|
2012-12-04 12:06:26 +01:00
|
|
|
|
if master is None:
|
|
|
|
|
raise ConfigError('this group type ({0}) needs a master '
|
|
|
|
|
'for OptionDescription {1}'.format(group_type,
|
|
|
|
|
self._name))
|
|
|
|
|
else:
|
|
|
|
|
if master is not None:
|
|
|
|
|
raise ConfigError("this group type ({0}) doesn't need a "
|
|
|
|
|
"master for OptionDescription {1}".format(
|
|
|
|
|
group_type, self._name))
|
|
|
|
|
self.master = master
|
2012-07-13 09:37:35 +02:00
|
|
|
|
else:
|
2012-12-06 18:14:57 +01:00
|
|
|
|
raise ConfigError('not allowed group_type : {0}'.format(group_type))
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def get_group_type(self):
|
|
|
|
|
return self.group_type
|
2012-12-04 12:06:26 +01:00
|
|
|
|
|
|
|
|
|
def get_master_name(self):
|
|
|
|
|
if self.master is None:
|
2012-12-04 16:22:39 +01:00
|
|
|
|
raise TypeError('get_master_name() shall not be called in case of '
|
2012-12-04 12:06:26 +01:00
|
|
|
|
'non-master OptionDescription')
|
|
|
|
|
return self.master
|
2012-12-05 10:54:32 +01:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# ____________________________________________________________
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"actions API"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def hide(self):
|
|
|
|
|
super(OptionDescription, self).hide()
|
|
|
|
|
for child in self._children:
|
|
|
|
|
if isinstance(child, OptionDescription):
|
|
|
|
|
child.hide()
|
|
|
|
|
def show(self):
|
|
|
|
|
super(OptionDescription, self).show()
|
|
|
|
|
for child in self._children:
|
|
|
|
|
if isinstance(child, OptionDescription):
|
|
|
|
|
child.show()
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def disable(self):
|
|
|
|
|
super(OptionDescription, self).disable()
|
|
|
|
|
for child in self._children:
|
|
|
|
|
if isinstance(child, OptionDescription):
|
|
|
|
|
child.disable()
|
|
|
|
|
def enable(self):
|
|
|
|
|
super(OptionDescription, self).enable()
|
|
|
|
|
for child in self._children:
|
|
|
|
|
if isinstance(child, OptionDescription):
|
|
|
|
|
child.enable()
|
|
|
|
|
# ____________________________________________________________
|
2012-09-20 10:51:35 +02:00
|
|
|
|
|
|
|
|
|
def validate_requires_arg(requires, name):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"malformed requirements"
|
2012-09-20 10:51:35 +02:00
|
|
|
|
config_action = []
|
|
|
|
|
for req in requires:
|
|
|
|
|
if not type(req) == tuple and len(req) != 3:
|
|
|
|
|
raise RequiresError("malformed requirements for option:"
|
|
|
|
|
" {0}".format(name))
|
|
|
|
|
action = req[2]
|
|
|
|
|
if action not in available_actions:
|
|
|
|
|
raise RequiresError("malformed requirements for option: {0}"
|
2013-01-31 17:01:10 +01:00
|
|
|
|
" unknown action: {1}".format(name, action))
|
2012-09-20 10:51:35 +02:00
|
|
|
|
if reverse_actions[action] in config_action:
|
|
|
|
|
raise RequiresError("inconsistency in action types for option: {0}"
|
2013-01-31 17:01:10 +01:00
|
|
|
|
" action: {1} in contradiction with {2}\n"
|
2012-09-20 10:51:35 +02:00
|
|
|
|
" ({3})".format(name, action,
|
|
|
|
|
reverse_actions[action], requires))
|
|
|
|
|
config_action.append(action)
|
|
|
|
|
|
|
|
|
|
def build_actions(requires):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"action are hide, show, enable, disable..."
|
2012-09-20 10:51:35 +02:00
|
|
|
|
trigger_actions = {}
|
|
|
|
|
for require in requires:
|
|
|
|
|
action = require[2]
|
|
|
|
|
trigger_actions.setdefault(action, []).append(require)
|
|
|
|
|
return trigger_actions
|
|
|
|
|
|
2012-11-30 10:47:35 +01:00
|
|
|
|
def apply_requires(opt, config, permissive=False):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"carries out the jit (just in time requirements between options"
|
2012-09-20 10:51:35 +02:00
|
|
|
|
if hasattr(opt, '_requires') and opt._requires is not None:
|
|
|
|
|
rootconfig = config._cfgimpl_get_toplevel()
|
|
|
|
|
validate_requires_arg(opt._requires, opt._name)
|
|
|
|
|
# filters the callbacks
|
|
|
|
|
trigger_actions = build_actions(opt._requires)
|
|
|
|
|
for requires in trigger_actions.values():
|
2012-07-13 09:37:35 +02:00
|
|
|
|
matches = False
|
2012-09-20 10:51:35 +02:00
|
|
|
|
for require in requires:
|
|
|
|
|
name, expected, action = require
|
2012-07-26 10:54:57 +02:00
|
|
|
|
path = config._cfgimpl_get_path() + '.' + opt._name
|
|
|
|
|
if name.startswith(path):
|
2012-09-20 10:51:35 +02:00
|
|
|
|
raise RequirementRecursionError("malformed requirements "
|
|
|
|
|
"imbrication detected for option: '{0}' "
|
|
|
|
|
"with requirement on: '{1}'".format(path, name))
|
|
|
|
|
homeconfig, shortname = rootconfig._cfgimpl_get_home_by_path(name)
|
2012-10-16 15:09:52 +02:00
|
|
|
|
try:
|
|
|
|
|
value = homeconfig._getattr(shortname, permissive=True)
|
|
|
|
|
except PropertiesOptionError, err:
|
2013-01-28 09:33:08 +01:00
|
|
|
|
properties = err.proptype
|
2012-11-30 10:47:35 +01:00
|
|
|
|
if permissive:
|
|
|
|
|
for perm in settings.permissive:
|
|
|
|
|
if perm in properties:
|
|
|
|
|
properties.remove(perm)
|
|
|
|
|
if properties != []:
|
|
|
|
|
raise NotFoundError("option '{0}' has requirement's property error: "
|
|
|
|
|
"{1} {2}".format(opt._name, name, properties))
|
2012-10-16 15:09:52 +02:00
|
|
|
|
except Exception, err:
|
2012-07-13 09:37:35 +02:00
|
|
|
|
raise NotFoundError("required option not found: "
|
|
|
|
|
"{0}".format(name))
|
2012-10-16 15:09:52 +02:00
|
|
|
|
if value == expected:
|
|
|
|
|
getattr(opt, action)() #.hide() or show() or...
|
|
|
|
|
# FIXME generic programming opt.property_launch(action, False)
|
|
|
|
|
matches = True
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# no callback has been triggered, then just reverse the action
|
|
|
|
|
if not matches:
|
|
|
|
|
getattr(opt, reverse_actions[action])()
|