tiramisu/tiramisu/config.py

534 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
# ____________________________________________________________
2013-04-03 12:20:26 +02:00
#from inspect import getmembers, ismethod
from tiramisu.error import (PropertiesOptionError, NotFoundError,
AmbigousOptionError, NoMatchingOptionFound, MandatoryError)
2012-10-05 16:00:07 +02:00
from tiramisu.option import (OptionDescription, Option, SymLinkOption,
2013-04-03 12:20:26 +02:00
apply_requires)
from tiramisu.setting import groups, Setting
2013-02-22 11:09:17 +01:00
from tiramisu.value import Values
2013-04-03 12:20:26 +02:00
class SubConfig(object):
"sub configuration management entry"
__slots__ = ('_cfgimpl_descr', '_cfgimpl_subconfigs', '_cfgimpl_parent',
'_cfgimpl_context')
def __init__(self, descr, parent, context): # FIXME , slots):
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``
2013-04-03 12:20:26 +02:00
:param parent: parent's `Config`
: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
2013-04-03 12:20:26 +02:00
self._cfgimpl_subconfigs = None
self._cfgimpl_parent = parent
2013-04-03 12:20:26 +02:00
self._cfgimpl_context = context
#self._cfgimpl_build(slots)
2013-04-03 12:20:26 +02:00
def cfgimpl_get_context(self):
return self._cfgimpl_context
2013-02-07 16:20:21 +01:00
def cfgimpl_get_settings(self):
return self._cfgimpl_context._cfgimpl_settings
2013-04-03 12:20:26 +02:00
def cfgimpl_get_values(self):
return self._cfgimpl_context._cfgimpl_values
2013-02-07 16:20:21 +01:00
2013-02-27 11:09:13 +01:00
def cfgimpl_get_description(self):
return self._cfgimpl_descr
# ____________________________________________________________
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
object.__setattr__(self, name, value)
return
2013-04-03 12:20:26 +02:00
self._setattr(name, value)
def _setattr(self, name, value, force_permissive=False):
2012-09-19 09:31:02 +02:00
if '.' in name:
2013-03-14 16:07:26 +01:00
homeconfig, name = self.cfgimpl_get_home_by_path(name)
2013-04-03 12:20:26 +02:00
return homeconfig.__setattr__(name, value)
if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
2013-04-03 12:20:26 +02:00
self._validate(name, getattr(self._cfgimpl_descr, name), force_permissive=force_permissive)
2013-02-21 17:07:00 +01:00
self.setoption(name, value)
2012-10-05 16:00:07 +02:00
2013-04-03 12:20:26 +02:00
def _validate(self, name, opt_or_descr, force_permissive=False):
2012-10-05 16:00:07 +02:00
"validation for the setattr and the getattr"
2013-04-03 12:20:26 +02:00
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)))
2013-04-03 12:20:26 +02:00
properties = set(self.cfgimpl_get_settings().get_properties(opt_or_descr))
#remove this properties, those properties are validate in value/setting
properties = properties - set(['mandatory', 'frozen'])
set_properties = set(self.cfgimpl_get_settings().get_properties())
properties = properties & set_properties
if force_permissive is True or self.cfgimpl_get_settings().has_property('permissive'):
properties = properties - set(self.cfgimpl_get_settings().get_permissive())
properties = properties - set(self.cfgimpl_get_settings().get_permissive(self.cfgimpl_get_description()))
2013-03-14 11:31:44 +01:00
properties = list(properties)
2012-09-14 11:55:32 +02:00
if properties != []:
raise PropertiesOptionError("trying to access"
2013-04-03 12:20:26 +02:00
" to an option named: {0} with properties"
" {1}".format(name, str(properties)),
properties)
def __getattr__(self, name):
2012-10-16 15:09:52 +02:00
return self._getattr(name)
2013-04-03 12:20:26 +02:00
def _getattr(self, name, force_permissive=False, force_properties=None):
2012-11-08 09:03:28 +01:00
"""
attribute notation mechanism for accessing the value of an option
:param name: attribute name
: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:
2013-03-14 16:07:26 +01:00
homeconfig, name = self.cfgimpl_get_home_by_path(name)
2013-04-03 12:20:26 +02:00
return homeconfig._getattr(name)
opt_or_descr = getattr(self._cfgimpl_descr, name)
2012-10-05 16:00:07 +02:00
# symlink options
if type(opt_or_descr) == SymLinkOption:
2013-04-03 12:20:26 +02:00
rootconfig = self.cfgimpl_get_context()
path = rootconfig.cfgimpl_get_description().get_path_by_opt(opt_or_descr.opt)
return getattr(rootconfig, path)
2013-04-03 12:20:26 +02:00
self._validate(name, opt_or_descr, force_permissive=force_permissive)
if isinstance(opt_or_descr, OptionDescription):
2013-04-03 12:20:26 +02:00
children = self.cfgimpl_get_description()._children
if opt_or_descr not in children[1]:
raise AttributeError("{0} with name {1} object has "
"no attribute {2}".format(self.__class__,
opt_or_descr._name,
name))
return SubConfig(opt_or_descr, self, self._cfgimpl_context)
# special attributes
if name.startswith('_cfgimpl_'):
# if it were in __dict__ it would have been found already
2013-04-03 12:20:26 +02:00
object.__getattr__(self, name)
return self.cfgimpl_get_values()._getitem(opt_or_descr,
force_properties=force_properties)
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)
2013-03-14 16:07:26 +01:00
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_path(self):
2012-10-05 16:00:07 +02:00
"the path in the attribute access meaning."
2013-04-03 12:20:26 +02:00
#FIXME optimisation
subpath = []
obj = self
while obj._cfgimpl_parent is not None:
subpath.insert(0, obj._cfgimpl_descr._name)
obj = obj._cfgimpl_parent
return ".".join(subpath)
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
2013-02-25 16:24:30 +01:00
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)"""
2013-04-03 12:20:26 +02:00
for child in self._cfgimpl_descr._children[1]:
if not isinstance(child, OptionDescription):
try:
yield child._name, getattr(self, child._name)
2013-03-25 14:21:30 +01:00
except GeneratorExit:
raise StopIteration
except:
2013-02-25 16:24:30 +01:00
pass # option with properties
2013-03-07 11:02:18 +01:00
def iter_all(self):
"""A way of parsing options **and** groups.
iteration on Options and OptionDescriptions."""
2013-04-03 12:20:26 +02:00
for child in self._cfgimpl_descr._children[1]:
2013-03-07 11:02:18 +01:00
try:
yield child._name, getattr(self, child._name)
2013-03-25 14:21:30 +01:00
except GeneratorExit:
raise StopIteration
2013-03-07 11:02:18 +01:00
except:
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))
2013-04-03 12:20:26 +02:00
for child in self._cfgimpl_descr._children[1]:
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)
2013-03-25 14:21:30 +01:00
except GeneratorExit:
raise StopIteration
2012-11-06 15:19:36 +01:00
except:
2012-12-06 18:14:57 +01:00
pass
2012-10-05 16:00:07 +02:00
# ______________________________________________________________________
2013-04-03 12:20:26 +02:00
2013-03-14 11:31:44 +01:00
def cfgimpl_set_permissive(self, permissive):
if not isinstance(permissive, list):
raise TypeError('permissive must be a list')
2013-04-03 12:20:26 +02:00
self.cfgimpl_get_settings().set_permissive(permissive, self.cfgimpl_get_description())
2013-03-14 11:31:44 +01: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):
if allpaths:
paths.append(path)
else:
try:
2013-04-03 12:20:26 +02:00
getattr(self, path)
except MandatoryError:
if mandatory:
paths.append(path)
except PropertiesOptionError:
pass
else:
2012-07-26 16:55:01 +02:00
paths.append(path)
2012-10-05 16:00:07 +02:00
return paths
2013-04-03 12:20:26 +02:00
def getpath(self):
descr = self.cfgimpl_get_description()
context_descr = self.cfgimpl_get_context().cfgimpl_get_description()
return context_descr.get_path_by_opt(descr)
def get(self, name):
path = self.getpath()
return self.cfgimpl_get_context().get(name, _subpath=path)
def find(self, bytype=None, byname=None, byvalue=None, byattrs=None):
path = self.getpath()
return self.cfgimpl_get_context().find(bytype=bytype, byname=byname,
byvalue=byvalue,
byattrs=byattrs,
_subpath=path)
def find_first(self, bytype=None, byname=None, byvalue=None, byattrs=None):
path = self.getpath()
return self.cfgimpl_get_context().find_first(bytype=bytype,
byname=byname,
byvalue=byvalue,
byattrs=byattrs,
_subpath=path)
# ____________________________________________________________
class Config(SubConfig):
"main configuration management entry"
__slots__ = ('_cfgimpl_settings', '_cfgimpl_values')
def __init__(self, descr, valid_opt_names=True):
""" Configuration option management master class
: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``
:param context: the current root config
:type context: `Config`
"""
self._cfgimpl_settings = Setting()
self._cfgimpl_values = Values(self)
#if valid_opt_names:
# # some api members shall not be used as option's names !
# #FIXME fait une boucle infini ...
# #methods = getmembers(self, ismethod)
# #slots = tuple([key for key, value in methods
# # if not key.startswith("_")])
# slots = []
#else:
# slots = []
super(Config, self).__init__(descr, None, self) # , slots)
self._cfgimpl_build_all_paths()
def _cfgimpl_build_all_paths(self):
self._cfgimpl_descr.build_cache()
def unwrap_from_path(self, path):
"""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)
def set(self, **kwargs):
"""
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.
"""
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)
elif len(candidates) > 1:
raise AmbigousOptionError(
'more than one option that ends with %s' % (key, ))
else:
raise NoMatchingOptionFound(
'there is no option that matches %s'
' or the option is hidden or disabled' % (key, ))
def get(self, name, _subpath=None):
"""
same as a `find_first()` method in a config that has identical names:
it returns the first item of an option named `name`
much like the attribute access way, except that
the search for the option is performed recursively in the whole
configuration tree.
:returns: option value.
"""
return self._find(byname=name, bytype=None, byvalue=None, byattrs=None,
first=True, getvalue=True, _subpath=_subpath)
def _find(self, bytype, byname, byvalue, byattrs, first, getvalue=False,
_subpath=None):
2012-10-12 11:35:07 +02:00
"""
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_name():
if byname is None:
return True
2013-04-03 12:20:26 +02:00
if path == byname or path.endswith('.' + byname):
2012-10-12 11:35:07 +02:00
return True
else:
return False
2013-04-03 12:20:26 +02:00
2012-10-12 11:35:07 +02:00
def _filter_by_value():
if byvalue is None:
return True
try:
value = getattr(self, path)
if value == byvalue:
return True
2013-02-22 11:09:17 +01:00
except: # a property restricts the access of the value
2012-10-12 11:35:07 +02:00
pass
return False
2013-04-03 12:20:26 +02:00
2012-10-12 11:35:07 +02:00
def _filter_by_type():
if bytype is None:
return True
if isinstance(option, bytype):
return True
return False
2013-04-03 12:20:26 +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
else:
continue
return True
2012-10-12 11:35:07 +02:00
find_results = []
2013-04-03 12:20:26 +02:00
opts, paths = self.cfgimpl_get_description()._cache_paths
for index in range(0, len(paths)):
path = paths[index]
option = opts[index]
if isinstance(option, OptionDescription):
continue
if _subpath is not None and not path.startswith(_subpath + '.'):
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
2013-04-03 12:20:26 +02:00
#remove option with propertyerror, ...
try:
value = getattr(self, path)
except: # a property restricts the access of the value
continue
2012-10-12 11:35:07 +02:00
if first:
2013-04-03 12:20:26 +02:00
if getvalue:
return value
else:
return option
2012-10-12 11:35:07 +02:00
else:
2013-04-03 12:20:26 +02:00
if getvalue:
find_results.append(value)
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
2013-04-03 12:20:26 +02:00
def find(self, bytype=None, byname=None, byvalue=None, byattrs=None, _subpath=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
"""
2013-04-03 12:20:26 +02:00
return self._find(bytype, byname, byvalue, byattrs, first=False, _subpath=_subpath)
2012-10-12 11:35:07 +02:00
2013-04-03 12:20:26 +02:00
def find_first(self, bytype=None, byname=None, byvalue=None, byattrs=None, _subpath=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
"""
2013-04-03 12:20:26 +02:00
return self._find(bytype, byname, byvalue, byattrs, first=True, _subpath=_subpath)
2012-10-11 16:16:43 +02:00
2013-02-22 11:09:17 +01: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:
2013-02-22 11:09:17 +01:00
pass # this just a hidden or disabled option
options = dict(pathsvalues)
return options
2013-02-22 11:09:17 +01: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
"""
2013-04-03 12:20:26 +02:00
for path in config.cfgimpl_get_description().getpaths(include_groups=True):
try:
2013-04-03 12:20:26 +02:00
config._getattr(path, force_properties=('mandatory',))
except MandatoryError:
yield path
2012-09-11 15:18:38 +02:00
except PropertiesOptionError:
pass