2012-07-13 09:37:35 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"pretty small and local configuration management tool"
|
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-07-23 14:39:16 +02:00
|
|
|
|
from copy import copy
|
2012-10-05 16:00:07 +02:00
|
|
|
|
from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError,
|
|
|
|
|
AmbigousOptionError, ConflictConfigError, NoMatchingOptionFound,
|
2012-08-14 10:55:08 +02:00
|
|
|
|
MandatoryError, MethodCallError)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
from tiramisu.option import (OptionDescription, Option, SymLinkOption,
|
2012-08-13 11:48:25 +02:00
|
|
|
|
group_types, Multi, apply_requires)
|
2012-08-14 10:55:08 +02:00
|
|
|
|
from tiramisu.autolib import carry_out_calculation
|
2012-09-14 11:55:32 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# ______________________________________________________________________
|
|
|
|
|
# generic owner. 'default' is the general config owner after init time
|
|
|
|
|
default_owner = 'user'
|
|
|
|
|
# ____________________________________________________________
|
|
|
|
|
class Config(object):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"properties attribute: the name of a property enables this property"
|
2012-08-13 12:49:58 +02:00
|
|
|
|
_cfgimpl_properties = ['hidden', 'disabled']
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"mandatory means: a mandatory option has to have a value that is not None"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
_cfgimpl_mandatory = True
|
2012-09-19 09:31:02 +02:00
|
|
|
|
_cfgimpl_frozen = True
|
2012-07-13 09:37:35 +02:00
|
|
|
|
_cfgimpl_owner = default_owner
|
|
|
|
|
_cfgimpl_toplevel = None
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __init__(self, descr, parent=None, **overrides):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
""" Configuration option management master class
|
|
|
|
|
:param descr: describes the configuration schema
|
|
|
|
|
:type descr: an instance of ``option.OptionDescription``
|
|
|
|
|
:param overrides: can be used to set different default values
|
|
|
|
|
(see method ``override``)
|
|
|
|
|
:param parent: is None if the ``Config`` is root parent Config otherwise
|
|
|
|
|
:type parent: ``Config``
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._cfgimpl_descr = descr
|
|
|
|
|
self._cfgimpl_value_owners = {}
|
|
|
|
|
self._cfgimpl_parent = parent
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"`Config()` indeed is in charge of the `Option()`'s values"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._cfgimpl_values = {}
|
|
|
|
|
self._cfgimpl_previous_values = {}
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"warnings are a great idea, let's make up a better use of it"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._cfgimpl_warnings = []
|
|
|
|
|
self._cfgimpl_toplevel = self._cfgimpl_get_toplevel()
|
2012-10-05 16:00:07 +02:00
|
|
|
|
'`freeze()` allows us to carry out this calculation again if necessary'
|
|
|
|
|
self._cfgimpl_frozen = self._cfgimpl_toplevel._cfgimpl_frozen
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._cfgimpl_build(overrides)
|
|
|
|
|
|
|
|
|
|
def _validate_duplicates(self, children):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""duplicates Option names in the schema
|
|
|
|
|
:type children: list of `Option` or `OptionDescription`
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
duplicates = []
|
|
|
|
|
for dup in children:
|
|
|
|
|
if dup._name not in duplicates:
|
|
|
|
|
duplicates.append(dup._name)
|
|
|
|
|
else:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
raise ConflictConfigError('duplicate option name: '
|
2012-07-13 09:37:35 +02:00
|
|
|
|
'{0}'.format(dup._name))
|
2012-09-20 10:51:35 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def _cfgimpl_build(self, overrides):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
|
|
|
|
- builds the config object from the schema
|
|
|
|
|
- settles various default values for options
|
|
|
|
|
:param overrides: dict of options name:default values
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._validate_duplicates(self._cfgimpl_descr._children)
|
|
|
|
|
for child in self._cfgimpl_descr._children:
|
|
|
|
|
if isinstance(child, Option):
|
|
|
|
|
if child.is_multi():
|
2012-10-05 16:00:07 +02:00
|
|
|
|
childdef = Multi(copy(child.getdefault()), config=self,
|
2012-07-13 09:37:35 +02:00
|
|
|
|
child=child)
|
|
|
|
|
self._cfgimpl_values[child._name] = childdef
|
2012-07-23 14:30:06 +02:00
|
|
|
|
self._cfgimpl_previous_values[child._name] = list(childdef)
|
2012-08-14 10:55:08 +02:00
|
|
|
|
self._cfgimpl_value_owners[child._name] = ['default' \
|
|
|
|
|
for i in range(len(child.getdefault() ))]
|
2012-07-13 09:37:35 +02:00
|
|
|
|
else:
|
|
|
|
|
childdef = child.getdefault()
|
|
|
|
|
self._cfgimpl_values[child._name] = childdef
|
2012-10-05 16:00:07 +02:00
|
|
|
|
self._cfgimpl_previous_values[child._name] = childdef
|
2012-08-14 10:55:08 +02:00
|
|
|
|
self._cfgimpl_value_owners[child._name] = 'default'
|
2012-07-13 09:37:35 +02:00
|
|
|
|
elif isinstance(child, OptionDescription):
|
|
|
|
|
self._validate_duplicates(child._children)
|
|
|
|
|
self._cfgimpl_values[child._name] = Config(child, parent=self)
|
|
|
|
|
self.override(overrides)
|
|
|
|
|
|
|
|
|
|
def cfgimpl_update(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""dynamically adds `Option()` or `OptionDescription()`
|
|
|
|
|
"""
|
|
|
|
|
# FIXME this is an update for new options in the schema only
|
|
|
|
|
# see the update_child() method of the descr object
|
2012-07-13 09:37:35 +02:00
|
|
|
|
for child in self._cfgimpl_descr._children:
|
|
|
|
|
if isinstance(child, Option):
|
|
|
|
|
if child._name not in self._cfgimpl_values:
|
2012-07-23 14:30:06 +02:00
|
|
|
|
if child.is_multi():
|
|
|
|
|
self._cfgimpl_values[child._name] = Multi(
|
|
|
|
|
copy(child.getdefault()), config=self, child=child)
|
|
|
|
|
self._cfgimpl_value_owners[child._name] = ['default' \
|
|
|
|
|
for i in range(len(child.getdefault() ))]
|
|
|
|
|
else:
|
|
|
|
|
self._cfgimpl_values[child._name] = copy(child.getdefault())
|
|
|
|
|
self._cfgimpl_value_owners[child._name] = 'default'
|
2012-07-13 09:37:35 +02:00
|
|
|
|
elif isinstance(child, OptionDescription):
|
|
|
|
|
if child._name not in self._cfgimpl_values:
|
|
|
|
|
self._cfgimpl_values[child._name] = Config(child, parent=self)
|
|
|
|
|
|
|
|
|
|
def override(self, overrides):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
|
|
|
|
overrides default values. This marks the overridden values as defaults.
|
|
|
|
|
:param overrides: is a dictionary of path strings to values.
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
for name, value in overrides.iteritems():
|
|
|
|
|
homeconfig, name = self._cfgimpl_get_home_by_path(name)
|
|
|
|
|
homeconfig.setoption(name, value, 'default')
|
|
|
|
|
|
|
|
|
|
def cfgimpl_set_owner(self, owner):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
":param owner: sets the default value for owner at the Config level"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._cfgimpl_owner = owner
|
|
|
|
|
for child in self._cfgimpl_descr._children:
|
|
|
|
|
if isinstance(child, OptionDescription):
|
|
|
|
|
self._cfgimpl_values[child._name].cfgimpl_set_owner(owner)
|
|
|
|
|
# ____________________________________________________________
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# properties methods
|
2012-08-13 12:49:58 +02:00
|
|
|
|
def _cfgimpl_has_properties(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"has properties means the Config's properties attribute is not empty"
|
2012-08-13 12:49:58 +02:00
|
|
|
|
return bool(len(self._cfgimpl_properties))
|
|
|
|
|
|
2012-09-14 10:29:25 +02:00
|
|
|
|
def _cfgimpl_has_property(self, propname):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""has property propname in the Config's properties attribute
|
|
|
|
|
:param property: string wich is the name of the property"""
|
2012-09-14 10:29:25 +02:00
|
|
|
|
return propname in self._cfgimpl_properties
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
2012-09-14 10:29:25 +02:00
|
|
|
|
def cfgimpl_enable_property(self, propname):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"puts property propname in the Config's properties attribute"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if self._cfgimpl_parent != None:
|
|
|
|
|
raise MethodCallError("this method root_hide() shall not be"
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"used with non-root Config() object")
|
2012-09-14 11:55:32 +02:00
|
|
|
|
if propname not in self._cfgimpl_properties:
|
2012-09-14 10:29:25 +02:00
|
|
|
|
self._cfgimpl_properties.append(propname)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-09-14 10:29:25 +02:00
|
|
|
|
def cfgimpl_disable_property(self, propname):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"deletes property propname in the Config's properties attribute"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if self._cfgimpl_parent != None:
|
|
|
|
|
raise MethodCallError("this method root_hide() shall not be"
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"used with non-root Config() object")
|
2012-09-14 10:29:25 +02:00
|
|
|
|
if self._cfgimpl_has_property(propname):
|
2012-09-14 11:55:32 +02:00
|
|
|
|
self._cfgimpl_properties.remove(propname)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# ____________________________________________________________
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# attribute methods
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __setattr__(self, name, value):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"attribute notation mechanism for the setting of the value of an option"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if name.startswith('_cfgimpl_'):
|
|
|
|
|
self.__dict__[name] = value
|
|
|
|
|
return
|
2012-09-19 09:31:02 +02:00
|
|
|
|
if '.' in name:
|
|
|
|
|
homeconfig, name = self._cfgimpl_get_home_by_path(name)
|
|
|
|
|
return setattr(homeconfig, name, value)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
|
|
|
|
|
self._validate(name, getattr(self._cfgimpl_descr, name))
|
|
|
|
|
self.setoption(name, value, self._cfgimpl_owner)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def _validate(self, name, opt_or_descr):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"validation for the setattr and the getattr"
|
|
|
|
|
apply_requires(opt_or_descr, self)
|
2012-09-14 11:55:32 +02:00
|
|
|
|
if not isinstance(opt_or_descr, Option) and \
|
|
|
|
|
not isinstance(opt_or_descr, OptionDescription):
|
|
|
|
|
raise TypeError('Unexpected object: {0}'.format(repr(opt_or_descr)))
|
2012-09-24 15:58:37 +02:00
|
|
|
|
properties = copy(opt_or_descr.properties)
|
2012-09-14 11:55:32 +02:00
|
|
|
|
for proper in properties:
|
|
|
|
|
if not self._cfgimpl_toplevel._cfgimpl_has_property(proper):
|
|
|
|
|
properties.remove(proper)
|
|
|
|
|
if properties != []:
|
|
|
|
|
raise PropertiesOptionError("trying to access"
|
|
|
|
|
" to an option named: {0} with properties"
|
2012-10-05 16:00:07 +02:00
|
|
|
|
" {1}".format(name, str(properties)),
|
2012-09-14 11:55:32 +02:00
|
|
|
|
properties)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
2012-09-11 15:18:38 +02:00
|
|
|
|
def _is_empty(self, opt):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"convenience method to know if an option is empty"
|
2012-09-11 15:18:38 +02:00
|
|
|
|
if (not opt.is_multi() and self._cfgimpl_values[opt._name] == None) or \
|
|
|
|
|
(opt.is_multi() and (self._cfgimpl_values[opt._name] == [] or \
|
2012-09-11 15:25:35 +02:00
|
|
|
|
None in self._cfgimpl_values[opt._name])):
|
2012-09-11 15:18:38 +02:00
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __getattr__(self, name):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"attribute notation mechanism for accessing the value of an option"
|
|
|
|
|
# attribute access by passing a path,
|
|
|
|
|
# for instance getattr(self, "creole.general.family.adresse_ip_eth0")
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if '.' in name:
|
|
|
|
|
homeconfig, name = self._cfgimpl_get_home_by_path(name)
|
|
|
|
|
return getattr(homeconfig, name)
|
|
|
|
|
opt_or_descr = getattr(self._cfgimpl_descr, name)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# symlink options
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if type(opt_or_descr) == SymLinkOption:
|
|
|
|
|
return getattr(self, opt_or_descr.path)
|
2012-09-14 11:55:32 +02:00
|
|
|
|
if name not in self._cfgimpl_values:
|
|
|
|
|
raise AttributeError("%s object has no attribute %s" %
|
|
|
|
|
(self.__class__, name))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._validate(name, opt_or_descr)
|
|
|
|
|
# special attributes
|
|
|
|
|
if name.startswith('_cfgimpl_'):
|
|
|
|
|
# if it were in __dict__ it would have been found already
|
|
|
|
|
return self.__dict__[name]
|
|
|
|
|
raise AttributeError("%s object has no attribute %s" %
|
|
|
|
|
(self.__class__, name))
|
2012-08-14 10:55:08 +02:00
|
|
|
|
if not isinstance(opt_or_descr, OptionDescription):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# options with callbacks (fill or auto)
|
2012-08-14 10:55:08 +02:00
|
|
|
|
if opt_or_descr.has_callback():
|
2012-07-13 09:37:35 +02:00
|
|
|
|
value = self._cfgimpl_values[name]
|
2012-09-07 15:47:06 +02:00
|
|
|
|
if (not opt_or_descr.is_frozen() or \
|
|
|
|
|
not opt_or_descr.is_forced_on_freeze()) and value != None:
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if opt_or_descr.is_multi():
|
2012-08-14 10:55:08 +02:00
|
|
|
|
if None not in value:
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return value
|
|
|
|
|
else:
|
2012-08-14 10:55:08 +02:00
|
|
|
|
return value
|
2012-10-05 16:00:07 +02:00
|
|
|
|
result = carry_out_calculation(name,
|
2012-07-13 09:37:35 +02:00
|
|
|
|
callback=opt_or_descr.getcallback(),
|
|
|
|
|
callback_params=opt_or_descr.getcallback_params(),
|
|
|
|
|
config=self._cfgimpl_get_toplevel())
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# this result **shall not** be a list
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# for example, [1, 2, 3, None] -> [1, 2, 3, result]
|
2012-07-23 14:30:06 +02:00
|
|
|
|
if isinstance(result, list):
|
2012-07-13 09:37:35 +02:00
|
|
|
|
raise ConfigError('invalid calculated value returned'
|
|
|
|
|
' for option {0} : shall not be a list'.format(name))
|
|
|
|
|
if result != None and not opt_or_descr._validate(result):
|
|
|
|
|
raise ConfigError('invalid calculated value returned'
|
|
|
|
|
' for option {0}'.format(name))
|
|
|
|
|
if opt_or_descr.is_multi():
|
|
|
|
|
if value == []:
|
2012-07-23 14:30:06 +02:00
|
|
|
|
_result = Multi([result], value.config, value.child)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
else:
|
2012-07-23 14:30:06 +02:00
|
|
|
|
_result = Multi([], value.config, value.child)
|
2012-07-13 09:37:35 +02:00
|
|
|
|
for val in value:
|
|
|
|
|
if val == None:
|
|
|
|
|
val = result
|
|
|
|
|
_result.append(val)
|
|
|
|
|
else:
|
|
|
|
|
_result = result
|
|
|
|
|
return _result
|
2012-08-14 10:55:08 +02:00
|
|
|
|
|
2012-08-13 16:06:02 +02:00
|
|
|
|
# mandatory options
|
2012-07-13 09:37:35 +02:00
|
|
|
|
homeconfig = self._cfgimpl_get_toplevel()
|
|
|
|
|
mandatory = homeconfig._cfgimpl_mandatory
|
|
|
|
|
if opt_or_descr.is_mandatory() and mandatory:
|
2012-09-11 15:18:38 +02:00
|
|
|
|
if self._is_empty(opt_or_descr) and \
|
|
|
|
|
opt_or_descr.is_empty_by_default():
|
2012-10-05 16:00:07 +02:00
|
|
|
|
raise MandatoryError("option: {0} is mandatory "
|
2012-07-13 09:37:35 +02:00
|
|
|
|
"and shall have a value".format(name))
|
2012-08-13 16:06:02 +02:00
|
|
|
|
# frozen and force default
|
|
|
|
|
if opt_or_descr.is_forced_on_freeze():
|
|
|
|
|
return opt_or_descr.getdefault()
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self._cfgimpl_values[name]
|
|
|
|
|
|
|
|
|
|
def unwrap_from_name(self, name):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""convenience method to extract and Option() object from the Config()
|
|
|
|
|
**and it is slow**: it recursively searches into the namespaces
|
|
|
|
|
|
|
|
|
|
:returns: Option()
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
paths = self.getpaths(allpaths=True)
|
|
|
|
|
opts = dict([(path, self.unwrap_from_path(path)) for path in paths])
|
|
|
|
|
all_paths = [p.split(".") for p in self.getpaths()]
|
|
|
|
|
for pth in all_paths:
|
|
|
|
|
if name in pth:
|
|
|
|
|
return opts[".".join(pth)]
|
|
|
|
|
raise NotFoundError("name: {0} not found".format(name))
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def unwrap_from_path(self, path):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""convenience method to extract and Option() object from the Config()
|
|
|
|
|
and it is **fast**: finds the option directly in the appropriate
|
|
|
|
|
namespace
|
|
|
|
|
|
|
|
|
|
:returns: Option()
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if '.' in path:
|
|
|
|
|
homeconfig, path = self._cfgimpl_get_home_by_path(path)
|
|
|
|
|
return getattr(homeconfig._cfgimpl_descr, path)
|
|
|
|
|
return getattr(self._cfgimpl_descr, path)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __delattr__(self, name):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"if you use delattr you are responsible for all bad things happening"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if name.startswith('_cfgimpl_'):
|
|
|
|
|
del self.__dict__[name]
|
|
|
|
|
return
|
|
|
|
|
self._cfgimpl_value_owners[name] = 'default'
|
|
|
|
|
opt = getattr(self._cfgimpl_descr, name)
|
|
|
|
|
if isinstance(opt, OptionDescription):
|
|
|
|
|
raise AttributeError("can't option subgroup")
|
|
|
|
|
self._cfgimpl_values[name] = getattr(opt, 'default', None)
|
|
|
|
|
|
|
|
|
|
def setoption(self, name, value, who=None):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""effectively modifies the value of an Option()
|
|
|
|
|
(typically called by the __setattr__)
|
|
|
|
|
|
|
|
|
|
:param who: is an owner's name
|
|
|
|
|
who is **not necessarily** a owner, because it cannot be a list
|
|
|
|
|
:type who: string
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
child = getattr(self._cfgimpl_descr, name)
|
|
|
|
|
if who == None:
|
|
|
|
|
if child.is_multi():
|
2012-10-05 16:00:07 +02:00
|
|
|
|
newowner = [self._cfgimpl_owner for i in range(len(value))]
|
2012-07-13 09:37:35 +02:00
|
|
|
|
else:
|
|
|
|
|
newowner = self._cfgimpl_owner
|
|
|
|
|
else:
|
|
|
|
|
if type(child) != SymLinkOption:
|
|
|
|
|
if child.is_multi():
|
2012-07-23 14:30:06 +02:00
|
|
|
|
if type(value) != Multi:
|
|
|
|
|
if type(value) == list:
|
|
|
|
|
value = Multi(value, self, child)
|
|
|
|
|
else:
|
|
|
|
|
raise ConfigError("invalid value for option:"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
" {0} that is set to multi".format(name))
|
|
|
|
|
newowner = [who for i in range(len(value))]
|
|
|
|
|
else:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
newowner = who
|
2012-07-13 09:37:35 +02:00
|
|
|
|
if type(child) != SymLinkOption:
|
2012-09-18 15:31:32 +02:00
|
|
|
|
if child.has_callback() and who=='default':
|
2012-09-18 15:25:35 +02:00
|
|
|
|
raise TypeError("trying to set a value to an option "
|
|
|
|
|
"wich has a callback: {0}".format(name))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
child.setoption(self, value, who)
|
|
|
|
|
if (value is None and who != 'default' and not child.is_multi()):
|
|
|
|
|
child.setowner(self, 'default')
|
2012-07-23 14:30:06 +02:00
|
|
|
|
self._cfgimpl_values[name] = copy(child.getdefault())
|
2012-07-13 09:37:35 +02:00
|
|
|
|
elif (value == [] and who != 'default' and child.is_multi()):
|
|
|
|
|
child.setowner(self, ['default' for i in range(len(child.getdefault()))])
|
2012-07-23 14:30:06 +02:00
|
|
|
|
self._cfgimpl_values[name] = Multi(copy(child.getdefault()),
|
|
|
|
|
config=self, child=child)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
else:
|
2012-07-13 09:37:35 +02:00
|
|
|
|
child.setowner(self, newowner)
|
|
|
|
|
else:
|
|
|
|
|
homeconfig = self._cfgimpl_get_toplevel()
|
|
|
|
|
child.setoption(homeconfig, value, who)
|
|
|
|
|
|
|
|
|
|
def set(self, **kwargs):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
|
|
|
|
"do what I mean"-interface to option setting. Searches all paths
|
|
|
|
|
starting from that config for matches of the optional arguments
|
|
|
|
|
and sets the found option if the match is not ambiguous.
|
|
|
|
|
:param kwargs: dict of name strings to values.
|
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
all_paths = [p.split(".") for p in self.getpaths(allpaths=True)]
|
|
|
|
|
for key, value in kwargs.iteritems():
|
|
|
|
|
key_p = key.split('.')
|
|
|
|
|
candidates = [p for p in all_paths if p[-len(key_p):] == key_p]
|
|
|
|
|
if len(candidates) == 1:
|
|
|
|
|
name = '.'.join(candidates[0])
|
|
|
|
|
homeconfig, name = self._cfgimpl_get_home_by_path(name)
|
|
|
|
|
try:
|
|
|
|
|
getattr(homeconfig, name)
|
|
|
|
|
except MandatoryError:
|
|
|
|
|
pass
|
|
|
|
|
except Exception, e:
|
|
|
|
|
raise e # HiddenOptionError or DisabledOptionError
|
|
|
|
|
homeconfig.setoption(name, value, self._cfgimpl_owner)
|
|
|
|
|
elif len(candidates) > 1:
|
|
|
|
|
raise AmbigousOptionError(
|
|
|
|
|
'more than one option that ends with %s' % (key, ))
|
|
|
|
|
else:
|
|
|
|
|
raise NoMatchingOptionFound(
|
2012-10-05 16:00:07 +02:00
|
|
|
|
'there is no option that matches %s'
|
2012-07-13 09:37:35 +02:00
|
|
|
|
' or the option is hidden or disabled'% (key, ))
|
|
|
|
|
|
|
|
|
|
def get(self, name):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
2012-10-11 16:16:43 +02:00
|
|
|
|
same as a find_first() method in a config that has identical names
|
|
|
|
|
that is : Returns the first item of an option named 'name'
|
|
|
|
|
|
2012-10-05 16:00:07 +02:00
|
|
|
|
much like the attribute access way, except that
|
|
|
|
|
the search for the option is performed recursively in the whole
|
|
|
|
|
configuration tree.
|
|
|
|
|
**carefull**: very slow !
|
2012-10-11 16:16:43 +02:00
|
|
|
|
|
|
|
|
|
:returns: option value.
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
paths = self.getpaths(allpaths=True)
|
|
|
|
|
pathsvalues = []
|
|
|
|
|
for path in paths:
|
|
|
|
|
pathname = path.split('.')[-1]
|
|
|
|
|
if pathname == name:
|
|
|
|
|
try:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
value = getattr(self, path)
|
|
|
|
|
return value
|
2012-07-13 09:37:35 +02:00
|
|
|
|
except Exception, e:
|
|
|
|
|
raise e
|
2012-10-05 16:00:07 +02:00
|
|
|
|
raise NotFoundError("option {0} not found in config".format(name))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
|
|
def _cfgimpl_get_home_by_path(self, path):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
""":returns: tuple (config, name)"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
path = path.split('.')
|
|
|
|
|
for step in path[:-1]:
|
|
|
|
|
self = getattr(self, step)
|
|
|
|
|
return self, path[-1]
|
|
|
|
|
|
|
|
|
|
def _cfgimpl_get_toplevel(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
":returns: root config"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
while self._cfgimpl_parent is not None:
|
|
|
|
|
self = self._cfgimpl_parent
|
|
|
|
|
return self
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-26 10:54:57 +02:00
|
|
|
|
def _cfgimpl_get_path(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"the path in the attribute access meaning."
|
2012-07-26 10:54:57 +02:00
|
|
|
|
subpath = []
|
|
|
|
|
obj = self
|
|
|
|
|
while obj._cfgimpl_parent is not None:
|
|
|
|
|
subpath.insert(0, obj._cfgimpl_descr._name)
|
|
|
|
|
obj = obj._cfgimpl_parent
|
|
|
|
|
return ".".join(subpath)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# ______________________________________________________________________
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def cfgimpl_previous_value(self, path):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"stores the previous value"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
home, name = self._cfgimpl_get_home_by_path(path)
|
|
|
|
|
return home._cfgimpl_previous_values[name]
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def get_previous_value(self, name):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"for the time being, only the previous Option's value is accessible"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self._cfgimpl_previous_values[name]
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# ______________________________________________________________________
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def add_warning(self, warning):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"Config implements its own warning pile. Could be useful"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self._cfgimpl_get_toplevel()._cfgimpl_warnings.append(warning)
|
|
|
|
|
|
|
|
|
|
def get_warnings(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"Config implements its own warning pile"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self._cfgimpl_get_toplevel()._cfgimpl_warnings
|
|
|
|
|
# ____________________________________________________________
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# Config()'s status
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def cfgimpl_freeze(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"cannot modify the frozen `Option`'s"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
|
|
|
|
rootconfig._cfgimpl_frozen = True
|
|
|
|
|
self._cfgimpl_frozen = True
|
|
|
|
|
|
|
|
|
|
def cfgimpl_unfreeze(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"can modify the Options that are frozen"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
|
|
|
|
rootconfig._cfgimpl_frozen = False
|
|
|
|
|
self._cfgimpl_frozen = False
|
|
|
|
|
|
|
|
|
|
def is_frozen(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"freeze flag at Config level"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
2012-09-11 16:32:24 +02:00
|
|
|
|
return rootconfig._cfgimpl_frozen
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
|
|
def cfgimpl_read_only(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"convenience method to freeze, hidde and disable"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
self.cfgimpl_freeze()
|
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
2012-09-14 11:55:32 +02:00
|
|
|
|
rootconfig.cfgimpl_disable_property('hidden')
|
|
|
|
|
rootconfig.cfgimpl_enable_property('disabled')
|
2012-07-13 09:37:35 +02:00
|
|
|
|
rootconfig._cfgimpl_mandatory = True
|
|
|
|
|
|
|
|
|
|
def cfgimpl_read_write(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"convenience method to freeze, hidde and disable"
|
2012-09-24 15:58:37 +02:00
|
|
|
|
self.cfgimpl_freeze()
|
2012-07-13 09:37:35 +02:00
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
2012-09-14 11:55:32 +02:00
|
|
|
|
rootconfig.cfgimpl_enable_property('hidden')
|
2012-09-20 10:51:35 +02:00
|
|
|
|
rootconfig.cfgimpl_enable_property('disabled')
|
2012-07-13 09:37:35 +02:00
|
|
|
|
rootconfig._cfgimpl_mandatory = False
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
|
|
|
|
def cfgimpl_non_mandatory(self):
|
|
|
|
|
"""mandatory at the Config level means that the Config raises an error
|
|
|
|
|
if a mandatory option is found"""
|
|
|
|
|
if self._cfgimpl_parent != None:
|
|
|
|
|
raise MethodCallError("this method root_mandatory shall"
|
|
|
|
|
" not be used with non-root Confit() object")
|
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
|
|
|
|
rootconfig._cfgimpl_mandatory = False
|
|
|
|
|
|
|
|
|
|
def cfgimpl_mandatory(self):
|
|
|
|
|
"""mandatory at the Config level means that the Config raises an error
|
|
|
|
|
if a mandatory option is found"""
|
|
|
|
|
if self._cfgimpl_parent != None:
|
|
|
|
|
raise MethodCallError("this method root_mandatory shall"
|
|
|
|
|
" not be used with non-root Confit() object")
|
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
|
|
|
|
rootconfig._cfgimpl_mandatory = True
|
|
|
|
|
|
|
|
|
|
def is_mandatory(self):
|
|
|
|
|
"all mandatory Options shall have a value"
|
|
|
|
|
rootconfig = self._cfgimpl_get_toplevel()
|
|
|
|
|
return rootconfig._cfgimpl_mandatory
|
2012-07-13 09:37:35 +02:00
|
|
|
|
# ____________________________________________________________
|
|
|
|
|
def getkey(self):
|
|
|
|
|
return self._cfgimpl_descr.getkey(self)
|
|
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
|
return hash(self.getkey())
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"Config comparison"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return self.getkey() == other.getkey()
|
|
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"Config comparison"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
return not self == other
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# ______________________________________________________________________
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __iter__(self):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"iteration only on Options (not OptionDescriptions)"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
for child in self._cfgimpl_descr._children:
|
|
|
|
|
if isinstance(child, Option):
|
|
|
|
|
try:
|
|
|
|
|
yield child._name, getattr(self, child._name)
|
|
|
|
|
except:
|
2012-09-19 09:31:02 +02:00
|
|
|
|
pass # option with properties
|
2012-07-13 09:37:35 +02:00
|
|
|
|
|
|
|
|
|
def iter_groups(self, group_type=None):
|
|
|
|
|
"iteration on OptionDescriptions"
|
|
|
|
|
if group_type == None:
|
|
|
|
|
groups = group_types
|
|
|
|
|
else:
|
|
|
|
|
if group_type not in group_types:
|
|
|
|
|
raise TypeError("Unknown group_type: {0}".format(group_type))
|
|
|
|
|
groups = [group_type]
|
|
|
|
|
for child in self._cfgimpl_descr._children:
|
|
|
|
|
if isinstance(child, OptionDescription):
|
|
|
|
|
try:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
if child.get_group_type() in groups:
|
2012-07-13 09:37:35 +02:00
|
|
|
|
yield child._name, getattr(self, child._name)
|
|
|
|
|
except:
|
|
|
|
|
pass # hidden, disabled option
|
2012-10-05 16:00:07 +02:00
|
|
|
|
# ______________________________________________________________________
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def __str__(self, indent=""):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"Config's string representation"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
lines = []
|
|
|
|
|
children = [(child._name, child)
|
|
|
|
|
for child in self._cfgimpl_descr._children]
|
|
|
|
|
children.sort()
|
|
|
|
|
for name, child in children:
|
|
|
|
|
if self._cfgimpl_value_owners.get(name, None) == 'default':
|
|
|
|
|
continue
|
|
|
|
|
value = getattr(self, name)
|
|
|
|
|
if isinstance(value, Config):
|
|
|
|
|
substr = value.__str__(indent + " ")
|
|
|
|
|
else:
|
|
|
|
|
substr = "%s %s = %s" % (indent, name, value)
|
|
|
|
|
if substr:
|
|
|
|
|
lines.append(substr)
|
|
|
|
|
if indent and not lines:
|
|
|
|
|
return '' # hide subgroups with all default values
|
|
|
|
|
lines.insert(0, "%s[%s]" % (indent, self._cfgimpl_descr._name,))
|
|
|
|
|
return '\n'.join(lines)
|
|
|
|
|
|
2012-07-26 16:55:01 +02:00
|
|
|
|
def getpaths(self, include_groups=False, allpaths=False, mandatory=False):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""returns a list of all paths in self, recursively, taking care of
|
2012-08-13 12:49:58 +02:00
|
|
|
|
the context of properties (hidden/disabled)
|
2012-10-11 16:16:43 +02:00
|
|
|
|
|
|
|
|
|
:param include_groups: if true, OptionDescription are included
|
|
|
|
|
:param allpaths: all the options (event the properties protected ones)
|
|
|
|
|
:param mandatory: includes the mandatory options
|
|
|
|
|
:returns: list of all paths
|
2012-07-13 09:37:35 +02:00
|
|
|
|
"""
|
|
|
|
|
paths = []
|
|
|
|
|
for path in self._cfgimpl_descr.getpaths(include_groups=include_groups):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
try:
|
2012-07-13 09:37:35 +02:00
|
|
|
|
value = getattr(self, path)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
|
2012-07-26 16:55:01 +02:00
|
|
|
|
except MandatoryError:
|
|
|
|
|
if mandatory or allpaths:
|
|
|
|
|
paths.append(path)
|
2012-09-11 15:18:38 +02:00
|
|
|
|
except PropertiesOptionError:
|
2012-07-26 16:55:01 +02:00
|
|
|
|
if allpaths:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
paths.append(path) # option which have properties added
|
2012-07-13 09:37:35 +02:00
|
|
|
|
else:
|
2012-09-11 15:18:38 +02:00
|
|
|
|
paths.append(path)
|
2012-10-05 16:00:07 +02:00
|
|
|
|
return paths
|
|
|
|
|
|
2012-10-11 16:16:43 +02:00
|
|
|
|
def find(self, bytype=None, byname=None, byvalue=None, byattrs=None):
|
|
|
|
|
"""
|
|
|
|
|
finds an option recursively in the config
|
|
|
|
|
|
|
|
|
|
:param bytype: Option class (BoolOption, StrOption, ...)
|
|
|
|
|
:param byname: filter by Option._name
|
|
|
|
|
:param byvalue: filter by the option's value
|
|
|
|
|
:param byattrs: dict of option attributes (default, callback...)
|
|
|
|
|
:returns: list of matching Option objects
|
|
|
|
|
"""
|
|
|
|
|
def _filter_by_name():
|
|
|
|
|
if byname is not None:
|
|
|
|
|
pathname = path.split('.')[-1]
|
|
|
|
|
if pathname == byname:
|
|
|
|
|
yield option
|
|
|
|
|
def _filter_by_value():
|
|
|
|
|
if byvalue is not None:
|
|
|
|
|
try:
|
|
|
|
|
value = getattr(self, path)
|
|
|
|
|
if value == byvalue:
|
|
|
|
|
yield option
|
|
|
|
|
except Exception, e: # a property restricts the acces to value
|
|
|
|
|
pass
|
|
|
|
|
def _filter_by_type():
|
|
|
|
|
if bytype is not None:
|
|
|
|
|
if isinstance(option, bytype):
|
|
|
|
|
find_results.append(self.unwrap_from_path(path))
|
|
|
|
|
find_results = []
|
|
|
|
|
paths = self.getpaths(allpaths=True)
|
|
|
|
|
for path in paths:
|
|
|
|
|
option = self.unwrap_from_path(path)
|
|
|
|
|
if _filter_by_name() is not None:
|
|
|
|
|
find_results.extend(list( _filter_by_name() ))
|
|
|
|
|
if _filter_by_value() is not None:
|
|
|
|
|
find_results.extend(list( _filter_by_value() ))
|
|
|
|
|
if _filter_by_type() is not None:
|
|
|
|
|
find_results.extend(list( _filter_by_type() ))
|
|
|
|
|
return find_results
|
|
|
|
|
|
2012-07-13 09:37:35 +02:00
|
|
|
|
def make_dict(config, flatten=False):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""export the whole config into a `dict`
|
|
|
|
|
:returns: dict of Option's name (or path) and values"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
paths = config.getpaths()
|
|
|
|
|
pathsvalues = []
|
|
|
|
|
for path in paths:
|
|
|
|
|
if flatten:
|
|
|
|
|
pathname = path.split('.')[-1]
|
|
|
|
|
else:
|
|
|
|
|
pathname = path
|
|
|
|
|
try:
|
2012-10-05 16:00:07 +02:00
|
|
|
|
value = getattr(config, path)
|
|
|
|
|
pathsvalues.append((pathname, value))
|
2012-07-13 09:37:35 +02:00
|
|
|
|
except:
|
2012-08-13 12:49:58 +02:00
|
|
|
|
pass # this just a hidden or disabled option
|
2012-07-13 09:37:35 +02:00
|
|
|
|
options = dict(pathsvalues)
|
|
|
|
|
return options
|
|
|
|
|
|
2012-07-24 15:35:44 +02:00
|
|
|
|
def mandatory_warnings(config):
|
2012-10-05 16:00:07 +02:00
|
|
|
|
"""convenience function to trace Options that are mandatory and
|
|
|
|
|
where no value has been set
|
|
|
|
|
|
|
|
|
|
:returns: generator of mandatory Option's path
|
|
|
|
|
"""
|
2012-07-27 09:54:32 +02:00
|
|
|
|
mandatory = config._cfgimpl_get_toplevel()._cfgimpl_mandatory
|
|
|
|
|
config._cfgimpl_get_toplevel()._cfgimpl_mandatory = True
|
2012-09-11 15:18:38 +02:00
|
|
|
|
for path in config._cfgimpl_descr.getpaths(include_groups=True):
|
2012-07-24 15:35:44 +02:00
|
|
|
|
try:
|
2012-07-26 16:55:01 +02:00
|
|
|
|
value = getattr(config, path)
|
2012-07-24 15:35:44 +02:00
|
|
|
|
except MandatoryError:
|
|
|
|
|
yield path
|
2012-09-11 15:18:38 +02:00
|
|
|
|
except PropertiesOptionError:
|
|
|
|
|
pass
|
2012-07-27 09:54:32 +02:00
|
|
|
|
config._cfgimpl_get_toplevel()._cfgimpl_mandatory = mandatory
|