tiramisu/tiramisu/config.py

535 lines
21 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"pretty small and local configuration management tool"
2013-02-21 17:07:00 +01:00
# Copyright (C) 2012-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 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
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from copy import copy
2012-10-05 16:00:07 +02:00
from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError,
AmbigousOptionError, ConflictConfigError, NoMatchingOptionFound,
2012-10-15 15:06:41 +02:00
MandatoryError, MethodCallError, NoValueReturned)
2012-10-05 16:00:07 +02:00
from tiramisu.option import (OptionDescription, Option, SymLinkOption,
apply_requires)
2013-02-07 16:20:21 +01:00
from tiramisu.setting import groups, owners, Setting
2013-02-21 17:07:00 +01:00
from tiramisu.value import OptionValues
2012-11-12 12:06:58 +01:00
# ____________________________________________________________
class Config(object):
2012-11-15 10:55:14 +01:00
"main configuration management entry"
_cfgimpl_toplevel = None
2012-10-05 16:00:07 +02:00
2013-02-07 16:20:21 +01:00
def __init__(self, descr, parent=None, context=None):
2012-10-05 16:00:07 +02:00
""" Configuration option management master class
2013-02-19 11:24:17 +01:00
2012-10-05 16:00:07 +02:00
:param descr: describes the configuration schema
:type descr: an instance of ``option.OptionDescription``
:param parent: is None if the ``Config`` is root parent Config otherwise
:type parent: ``Config``
2013-02-07 16:20:21 +01:00
:param context: the current root config
:type context: `Config`
2012-10-05 16:00:07 +02:00
"""
# main option description
self._cfgimpl_descr = descr
# sub option descriptions
self._cfgimpl_subconfigs = {}
self._cfgimpl_parent = parent
if context is None:
self._cfgimpl_context = self
else:
self._cfgimpl_context = context
2013-02-07 16:20:21 +01:00
if parent == None:
self._cfgimpl_settings = Setting()
self._cfgimpl_values = OptionValues(self._cfgimpl_context)
2013-02-07 16:20:21 +01:00
else:
if context is None:
raise ConfigError("cannot find a value for this config")
self._cfgimpl_settings = None
self._cfgimpl_values = None
2012-10-05 16:00:07 +02:00
"warnings are a great idea, let's make up a better use of it"
self._cfgimpl_warnings = []
self._cfgimpl_toplevel = self._cfgimpl_get_toplevel()
2012-11-12 12:06:58 +01:00
self._cfgimpl_build()
2013-02-07 16:20:21 +01:00
def cfgimpl_get_settings(self):
return self._cfgimpl_context._cfgimpl_settings
def cfgimpl_set_settings(self, settings):
if not isinstance(settings, Setting):
raise ConfigError("setting not allowed")
self._cfgimpl_context._cfgimpl_settings = settings
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`
"""
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: '
'{0}'.format(dup._name))
2012-09-20 10:51:35 +02:00
2012-11-12 12:06:58 +01:00
def _cfgimpl_build(self):
2012-10-05 16:00:07 +02:00
"""
- builds the config object from the schema
- settles various default values for options
"""
self._validate_duplicates(self._cfgimpl_descr._children)
2013-02-21 17:07:00 +01:00
if self._cfgimpl_descr.group_type == groups.master:
mastername = self._cfgimpl_descr._name
masteropt = getattr(self._cfgimpl_descr, mastername)
self._cfgimpl_values.master_groups[masteropt] = []
for child in self._cfgimpl_descr._children:
if isinstance(child, OptionDescription):
self._validate_duplicates(child._children)
self._cfgimpl_subconfigs[child] = Config(child, parent=self,
2013-02-07 16:20:21 +01:00
context=self._cfgimpl_context)
2013-02-21 17:07:00 +01:00
if (self._cfgimpl_descr.group_type == groups.master and
child != masteropt):
self._cfgimpl_values.master_groups[child] = []
self._cfgimpl_values.master_groups[masteropt].append(child)
2013-02-07 16:20:21 +01:00
2013-02-21 17:07:00 +01:00
if self._cfgimpl_descr.group_type == groups.master:
print self._cfgimpl_values.master_groups
# ____________________________________________________________
2012-10-05 16:00:07 +02:00
# attribute methods
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"
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)
if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
self._validate(name, getattr(self._cfgimpl_descr, name))
2013-02-21 17:07:00 +01:00
self.setoption(name, value)
2012-10-05 16:00:07 +02:00
2012-10-16 15:09:52 +02:00
def _validate(self, name, opt_or_descr, permissive=False):
2012-10-05 16:00:07 +02:00
"validation for the setattr and the getattr"
2012-11-30 10:47:35 +01:00
apply_requires(opt_or_descr, self, permissive=permissive)
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)
for proper in copy(properties):
2013-02-07 16:20:21 +01:00
if not self._cfgimpl_context._cfgimpl_settings.has_property(proper):
2012-09-14 11:55:32 +02:00
properties.remove(proper)
2012-10-16 15:09:52 +02:00
if permissive:
2013-02-07 16:20:21 +01:00
for perm in self._cfgimpl_context._cfgimpl_settings.permissive:
2012-10-16 15:09:52 +02:00
if perm in properties:
properties.remove(perm)
2012-09-14 11:55:32 +02:00
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)
def __getattr__(self, name):
2012-10-16 15:09:52 +02:00
return self._getattr(name)
2013-02-21 17:07:00 +01:00
# def fill_multi(self, opt, result, use_default_multi=False, default_multi=None):
# """fills a multi option with default and calculated values
# """
# # FIXME C'EST ENCORE DU N'IMPORTE QUOI
# if not isinstance(result, list):
# _result = [result]
# else:
# _result = result
# return Multi(_result, self._cfgimpl_context, opt)
2012-10-16 15:09:52 +02:00
def _getattr(self, name, permissive=False):
2012-11-08 09:03:28 +01:00
"""
attribute notation mechanism for accessing the value of an option
:param name: attribute name
:param permissive: permissive doesn't raise some property error
2013-02-07 16:20:21 +01:00
(see ``permissive``)
2012-11-08 09:03:28 +01:00
:return: option's value if name is an option name, OptionDescription
2012-11-19 10:45:03 +01:00
otherwise
2012-11-08 09:03:28 +01:00
"""
2012-10-05 16:00:07 +02:00
# attribute access by passing a path,
# for instance getattr(self, "creole.general.family.adresse_ip_eth0")
if '.' in name:
homeconfig, name = self._cfgimpl_get_home_by_path(name)
2012-10-16 15:09:52 +02:00
return homeconfig._getattr(name, permissive)
opt_or_descr = getattr(self._cfgimpl_descr, name)
2012-10-05 16:00:07 +02:00
# symlink options
if type(opt_or_descr) == SymLinkOption:
rootconfig = self._cfgimpl_get_toplevel()
return getattr(rootconfig, opt_or_descr.path)
2012-10-16 15:09:52 +02:00
self._validate(name, opt_or_descr, permissive)
if isinstance(opt_or_descr, OptionDescription):
if opt_or_descr not in self._cfgimpl_subconfigs:
raise AttributeError("%s with name %s object has no attribute %s" %
(self.__class__, opt_or_descr._name, name))
return self._cfgimpl_subconfigs[opt_or_descr]
# special attributes
if name.startswith('_cfgimpl_'):
# if it were in __dict__ it would have been found already
return self.__dict__[name]
return self._cfgimpl_context._cfgimpl_values[opt_or_descr]
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()
"""
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
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()
"""
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
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__)
"""
child = getattr(self._cfgimpl_descr, name)
2013-02-21 17:07:00 +01:00
child.setoption(self, value)
def set(self, **kwargs):
2012-10-05 16:00:07 +02:00
"""
2012-11-20 17:14:58 +01:00
do what I mean"-interface to option setting. Searches all paths
2012-10-05 16:00:07 +02:00
starting from that config for matches of the optional arguments
and sets the found option if the match is not ambiguous.
2013-02-21 17:07:00 +01:00
2012-10-05 16:00:07 +02:00
:param kwargs: dict of name strings to values.
"""
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
2013-02-21 17:07:00 +01:00
homeconfig.setoption(name, value)
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'
' or the option is hidden or disabled'% (key, ))
def get(self, name):
2012-10-05 16:00:07 +02:00
"""
2012-11-20 17:14:58 +01:00
same as a `find_first()` method in a config that has identical names:
it returns the first item of an option named `name`
2012-10-11 16:16:43 +02:00
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
"""
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
except Exception, e:
raise e
2012-10-05 16:00:07 +02:00
raise NotFoundError("option {0} not found in config".format(name))
def _cfgimpl_get_home_by_path(self, path):
2012-10-05 16:00:07 +02:00
""":returns: tuple (config, name)"""
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"
while self._cfgimpl_parent is not None:
self = self._cfgimpl_parent
return self
2012-10-05 16:00:07 +02:00
def _cfgimpl_get_path(self):
2012-10-05 16:00:07 +02:00
"the path in the attribute access meaning."
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
# ______________________________________________________________________
2013-02-07 16:20:21 +01:00
# def cfgimpl_previous_value(self, path):
# "stores the previous value"
# home, name = self._cfgimpl_get_home_by_path(path)
# # FIXME fucking name
# return home._cfgimpl_context._cfgimpl_values.previous_values[name]
# def get_previous_value(self, name):
# "for the time being, only the previous Option's value is accessible"
# return self._cfgimpl_context._cfgimpl_values.previous_values[name]
2012-10-05 16:00:07 +02:00
# ______________________________________________________________________
def add_warning(self, warning):
2012-10-05 16:00:07 +02:00
"Config implements its own warning pile. Could be useful"
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"
return self._cfgimpl_get_toplevel()._cfgimpl_warnings
# ____________________________________________________________
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-12-05 09:41:53 +01:00
if not isinstance(other, Config):
2012-11-30 15:08:34 +01:00
return False
return self.getkey() == other.getkey()
def __ne__(self, other):
2012-10-05 16:00:07 +02:00
"Config comparison"
return not self == other
2012-10-05 16:00:07 +02:00
# ______________________________________________________________________
def __iter__(self):
2012-11-20 17:14:58 +01:00
"""Pythonesque way of parsing group's ordered options.
iteration only on Options (not OptionDescriptions)"""
for child in self._cfgimpl_descr._children:
if not isinstance(child, OptionDescription):
try:
yield child._name, getattr(self, child._name)
except:
2012-09-19 09:31:02 +02:00
pass # option with properties
def iter_groups(self, group_type=None):
2012-11-20 17:14:58 +01:00
"""iteration on groups objects only.
All groups are returned if `group_type` is `None`, otherwise the groups
can be filtered by categories (families, or whatever).
2013-02-21 17:07:00 +01:00
2012-12-10 14:38:25 +01:00
:param group_type: if defined, is an instance of `groups.GroupType`
or `groups.MasterGroupType` that lives in
2013-02-07 16:20:21 +01:00
`setting.groups`
2012-12-06 18:14:57 +01:00
2012-11-20 17:14:58 +01:00
"""
2012-12-06 18:14:57 +01:00
if group_type is not None:
2012-12-10 14:38:25 +01:00
if not isinstance(group_type, groups.GroupType):
raise TypeError("Unknown group_type: {0}".format(group_type))
for child in self._cfgimpl_descr._children:
if isinstance(child, OptionDescription):
2012-11-06 15:19:36 +01:00
try:
2012-12-06 18:14:57 +01:00
if group_type is not None:
if child.get_group_type() == group_type:
yield child._name, getattr(self, child._name)
else:
2012-11-06 15:19:36 +01:00
yield child._name, getattr(self, child._name)
except:
2012-12-06 18:14:57 +01:00
pass
2012-10-05 16:00:07 +02:00
# ______________________________________________________________________
2012-11-28 10:14:16 +01:00
def __str__(self):
2012-10-05 16:00:07 +02:00
"Config's string representation"
lines = []
2012-11-28 10:14:16 +01:00
for name, grp in self.iter_groups():
lines.append("[%s]" % name)
for name, value in self:
try:
lines.append("%s = %s" % (name, value))
except:
pass
return '\n'.join(lines)
2012-11-28 10:14:16 +01:00
__repr__ = __str__
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
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
"""
paths = []
for path in self._cfgimpl_descr.getpaths(include_groups=include_groups):
2012-10-05 16:00:07 +02:00
try:
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
else:
2012-09-11 15:18:38 +02:00
paths.append(path)
2012-10-05 16:00:07 +02:00
return paths
2012-10-12 11:35:07 +02:00
def _find(self, bytype, byname, byvalue, byattrs, first):
"""
convenience method for finding an option that lives only in the subtree
:param first: return only one option if True, a list otherwise
:return: find list or an exception if nothing has been found
2012-10-12 11:35:07 +02:00
"""
def _filter_by_attrs():
if byattrs is None:
return True
for key, value in byattrs.items():
if not hasattr(option, key):
return False
else:
if getattr(option, key) != value:
return False
2012-10-12 11:35:07 +02:00
else:
continue
return True
def _filter_by_name():
if byname is None:
return True
pathname = path.split('.')[-1]
if pathname == byname:
return True
else:
return False
def _filter_by_value():
if byvalue is None:
return True
try:
value = getattr(self, path)
if value == byvalue:
return True
except: # a property restricts the access of the value
2012-10-12 11:35:07 +02:00
pass
return False
def _filter_by_type():
if bytype is None:
return True
if isinstance(option, bytype):
return True
return False
find_results = []
paths = self.getpaths(allpaths=True)
for path in paths:
try:
option = self.unwrap_from_path(path)
except PropertiesOptionError, err:
continue
2012-10-12 11:35:07 +02:00
if not _filter_by_name():
continue
if not _filter_by_value():
continue
if not _filter_by_type():
continue
if not _filter_by_attrs():
continue
if first:
return option
else:
find_results.append(option)
if find_results == []:
raise NotFoundError("no option found in config with these criteria")
2012-10-12 11:35:07 +02:00
else:
return find_results
2012-10-11 16:16:43 +02:00
def find(self, bytype=None, byname=None, byvalue=None, byattrs=None):
2012-10-12 11:35:07 +02:00
"""
finds a list of options 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
"""
return self._find(bytype, byname, byvalue, byattrs, first=False)
def find_first(self, bytype=None, byname=None, byvalue=None, byattrs=None):
2012-10-11 16:16:43 +02:00
"""
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
"""
2012-10-12 11:35:07 +02:00
return self._find(bytype, byname, byvalue, byattrs, first=True)
2012-10-11 16:16:43 +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"""
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))
except:
pass # this just a hidden or disabled option
options = dict(pathsvalues)
return options
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
2013-02-07 16:20:21 +01:00
FIXME : CAREFULL : not multi-user
2012-10-05 16:00:07 +02:00
"""
2013-02-07 16:20:21 +01:00
mandatory = config._cfgimpl_context._cfgimpl_settings.mandatory
config._cfgimpl_context._cfgimpl_settings.mandatory = True
2012-09-11 15:18:38 +02:00
for path in config._cfgimpl_descr.getpaths(include_groups=True):
try:
value = config._getattr(path, permissive=True)
except MandatoryError:
yield path
2012-09-11 15:18:38 +02:00
except PropertiesOptionError:
pass
2013-02-07 16:20:21 +01:00
config._cfgimpl_context._cfgimpl_settings.mandatory = mandatory