_calc_requirement is a set, not a tuple
[tiramisu.git] / tiramisu / config.py
index 84f1cc4..dd0b0f9 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
-"pretty small and local configuration management tool"
-# Copyright (C) 2012 Team tiramisu (see AUTHORS for all contributors)
+"options handler global entry point"
+# 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 rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
 # the whole pypy projet is under MIT licence
 # ____________________________________________________________
-from copy import copy
-from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError,
-    AmbigousOptionError, ConflictConfigError, NoMatchingOptionFound,
-    MandatoryError, MethodCallError, NoValueReturned)
-from tiramisu.option import (OptionDescription, Option, SymLinkOption,
-    Multi, apply_requires)
-from tiramisu.setting import settings, group_types
+from time import time
+from tiramisu.error import PropertiesOptionError, ConfigError
+from tiramisu.option import OptionDescription, Option, SymLinkOption, \
+    BaseInformation
+from tiramisu.setting import groups, Settings, default_encoding, storage_type
+from tiramisu.value import Values
+from tiramisu.i18n import _
 
-# ____________________________________________________________
-class Config(object):
-    "main configuration management entry"
-    _cfgimpl_toplevel = None
 
-    def __init__(self, descr, parent=None):
-        """ 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``
-        """
-        self._cfgimpl_descr = descr
-        self._cfgimpl_value_owners = {}
-        self._cfgimpl_parent = parent
-        "`Config()` indeed is in charge of the `Option()`'s values"
-        self._cfgimpl_values = {}
-        self._cfgimpl_previous_values = {}
-        "warnings are a great idea, let's make up a better use of it"
-        self._cfgimpl_warnings = []
-        self._cfgimpl_toplevel = self._cfgimpl_get_toplevel()
-        '`freeze()` allows us to carry out this calculation again if necessary'
-        self._cfgimpl_build()
-
-    def _validate_duplicates(self, children):
-        """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:
-                raise ConflictConfigError('duplicate option name: '
-                    '{0}'.format(dup._name))
+def gen_id(config):
+    return str(id(config)) + str(time())
 
-    def _cfgimpl_build(self):
-        """
-        - builds the config object from the schema
-        - settles various default values for options
-        """
-        self._validate_duplicates(self._cfgimpl_descr._children)
-        for child in self._cfgimpl_descr._children:
-            if isinstance(child, Option):
-                if child.is_multi():
-                    childdef = Multi(copy(child.getdefault()), config=self,
-                                     opt=child)
-                    self._cfgimpl_values[child._name] = childdef
-                    self._cfgimpl_previous_values[child._name] = list(childdef)
-                else:
-                    childdef = child.getdefault()
-                    self._cfgimpl_values[child._name] = childdef
-                    self._cfgimpl_previous_values[child._name] = childdef
-                child.setowner(self, 'default')
-            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):
-        """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
-        for child in self._cfgimpl_descr._children:
-            if isinstance(child, Option):
-                if child._name not in self._cfgimpl_values:
-                    if child.is_multi():
-                        self._cfgimpl_values[child._name] = Multi(
-                                copy(child.getdefault()), config=self, child=child)
-                    else:
-                        self._cfgimpl_values[child._name] = copy(child.getdefault())
-                    child.setowner(self, 'default')
-            elif isinstance(child, OptionDescription):
-                if child._name not in self._cfgimpl_values:
-                    self._cfgimpl_values[child._name] = Config(child, parent=self)
 
-    # ____________________________________________________________
-    # attribute methods
-    def __setattr__(self, name, value):
-        "attribute notation mechanism for the setting of the value of an option"
-        if name.startswith('_cfgimpl_'):
-            self.__dict__[name] = value
-            return
-        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))
-        self.setoption(name, value, settings.owner)
-
-    def _validate(self, name, opt_or_descr, permissive=False):
-        "validation for the setattr and the getattr"
-        apply_requires(opt_or_descr, self, permissive=permissive)
-        if not isinstance(opt_or_descr, Option) and \
-                not isinstance(opt_or_descr, OptionDescription):
-            raise TypeError('Unexpected object: {0}'.format(repr(opt_or_descr)))
-        properties = copy(opt_or_descr.properties)
-        for proper in copy(properties):
-            if not settings.has_property(proper):
-                properties.remove(proper)
-        if permissive:
-            for perm in settings.permissive:
-                if perm in properties:
-                    properties.remove(perm)
-        if properties != []:
-            raise PropertiesOptionError("trying to access"
-                    " to an option named: {0} with properties"
-                    " {1}".format(name, str(properties)),
-                    properties)
-
-    def _is_empty(self, opt):
-        "convenience method to know if an option is empty"
-        if (not opt.is_multi() and self._cfgimpl_values[opt._name] == None) or \
-            (opt.is_multi() and (self._cfgimpl_values[opt._name] == [] or \
-                None in self._cfgimpl_values[opt._name])):
-            return True
-        return False
-
-    def _test_mandatory(self, path, opt):
-        # mandatory options
-        mandatory = settings.mandatory
-        if opt.is_mandatory() and mandatory:
-            if self._is_empty(opt) and \
-                    opt.is_empty_by_default():
-                raise MandatoryError("option: {0} is mandatory "
-                                      "and shall have a value".format(path))
-
-    def __getattr__(self, name):
-        return self._getattr(name)
+class SubConfig(BaseInformation):
+    "sub configuration management entry"
+    __slots__ = ('_impl_context', '_impl_descr', '_impl_path')
 
-    def _get_master_len(self, slave_name):
-        try:
-            parent_cfg = self
-            if parent_cfg is None:
-                return None
-            master_name = parent_cfg._cfgimpl_descr.get_master_name()
-            master_value = parent_cfg._cfgimpl_values[master_name]
-            return len(master_value)
-        except TypeError, err:
-            # in this case we just don't care about the len
-            return None
-
-    def _valid_len(self, slave_name, slave_value):
-        master_len = self._get_master_len(slave_name)
-        if master_len == None:
-            return True
-        if master_len != len(slave_value):
-            raise ValueError("invalid len for the group of"
-                    " the option {0}".format(slave_name))
-
-    def fill_multi(self, name, result, default_multi=None):
-        """fills a multi option with default and calculated values
-        """
-        value = self._cfgimpl_values[name]
-        master_len = self._get_master_len(name)
-        if not isinstance(result, list):
-            if master_len is None:
-                master_len = 1
-            # a list is built with the same len as the master
-            _result = []
-            for i in range(master_len):
-                _result.append(result)
-        elif default_multi != None:
-            if master_len != None:
-                slave_len = len(result)
-                if slave_len > master_len:
-                    raise ValueError("invalid value's len for"
-                            "the option: {1}".format(name))
-                if slave_len != master_len:
-                    delta_len = master_len - len(result)
-                    for i in range(delta_len):
-                        _result.append(default_multi)
-        else:
-            _result = result
-        return Multi(_result, value.config, opt=value.opt)
-
-    def _getattr(self, name, permissive=False):
-        """
-        attribute notation mechanism for accessing the value of an option
-        :param name: attribute name
-        :param permissive: permissive doesn't raise some property error
-                          (see ``settings.permissive``)
-        :return: option's value if name is an option name, OptionDescription
-                 otherwise
-        """
-        # 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)
-            return homeconfig._getattr(name, permissive)
-        opt_or_descr = getattr(self._cfgimpl_descr, name)
-        # symlink options
-        if type(opt_or_descr) == SymLinkOption:
-            rootconfig = self._cfgimpl_get_toplevel()
-            return getattr(rootconfig, opt_or_descr.path)
-        if name not in self._cfgimpl_values:
-            raise AttributeError("%s object has no attribute %s" %
-                                 (self.__class__, name))
-        self._validate(name, opt_or_descr, permissive)
-        # 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))
-        if not isinstance(opt_or_descr, OptionDescription):
-            # options with callbacks
-            if opt_or_descr.has_callback():
-                value = self._cfgimpl_values[name]
-                if (not opt_or_descr.is_frozen() or \
-                        not opt_or_descr.is_forced_on_freeze()) and \
-                        not opt_or_descr.is_default_owner(self):
-                    self._valid_len(name, value)
-                    return value
-                try:
-                    result = opt_or_descr.getcallback_value(
-                            self._cfgimpl_get_toplevel())
-                except NoValueReturned, err:
-                    pass
-                else:
-                    if opt_or_descr.is_multi():
-                        _result = self.fill_multi(name, result)
-                    else:
-                        # this result **shall not** be a list
-                        if isinstance(result, list):
-                            raise ConfigError('invalid calculated value returned'
-                                ' for option {0} : shall not be a list'.format(name))
-                        _result = result
-                    if _result != None and not opt_or_descr.validate(_result,
-                                settings.validator):
-                        raise ConfigError('invalid calculated value returned'
-                            ' for option {0}'.format(name))
-                    self._cfgimpl_values[name] = _result
-                    opt_or_descr.setowner(self, 'default')
-            # frozen and force default
-            if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze():
-                value = opt_or_descr.getdefault()
-                if opt_or_descr.is_multi():
-                    value = self.fill_multi(name, result, opt_or_descr.getdefault_multi())
-                self._cfgimpl_values[name] = value
-                opt_or_descr.setowner(self, 'default')
-            self._test_mandatory(name, opt_or_descr)
-        value = self._cfgimpl_values[name]
-        self._valid_len(name, value)
-        return value
-
-    def unwrap_from_name(self, name):
-        """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))
-
-    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 setoption(self, name, value, who=None):
-        """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
-        """
-        child = getattr(self._cfgimpl_descr, name)
-        if type(child) != SymLinkOption:
-            if who == None:
-                who = settings.owner
-            if child.is_multi():
-                if type(value) != Multi:
-                    if type(value) == list:
-                        value = Multi(value, self, child)
-                    else:
-                        raise ConfigError("invalid value for option:"
-                                   " {0} that is set to multi".format(name))
-            child.setoption(self, value, who)
-            child.setowner(self, who)
-        else:
-            homeconfig = self._cfgimpl_get_toplevel()
-            child.setoption(homeconfig, value, who)
-
-    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, settings.owner)
-            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):
-        """
-        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.
-        **carefull**: very slow !
+    def __init__(self, descr, context, subpath=None):
+        """ Configuration option management master class
 
-        :returns: option value.
+        :param descr: describes the configuration schema
+        :type descr: an instance of ``option.OptionDescription``
+        :param context: the current root config
+        :type context: `Config`
+        :type subpath: `str` with the path name
         """
-        paths = self.getpaths(allpaths=True)
-        pathsvalues = []
-        for path in paths:
-            pathname = path.split('.')[-1]
-            if pathname == name:
-                try:
-                    value = getattr(self, path)
-                    return value
-                except Exception, e:
-                    raise e
-        raise NotFoundError("option {0} not found in config".format(name))
-
-    def _cfgimpl_get_home_by_path(self, path):
+        # main option description
+        if not isinstance(descr, OptionDescription):
+            raise ValueError(_('descr must be an optiondescription, not {0}'
+                               ).format(type(descr)))
+        self._impl_descr = descr
+        # sub option descriptions
+        if not isinstance(context, SubConfig):
+            raise ValueError('context must be a SubConfig')
+        self._impl_context = context
+        self._impl_path = subpath
+
+    def cfgimpl_reset_cache(self, only_expired=False, only=('values',
+                                                            'settings')):
+        self.cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only)
+
+    def cfgimpl_get_home_by_path(self, path, force_permissive=False,
+                                 force_properties=None):
         """:returns: tuple (config, name)"""
         path = path.split('.')
         for step in path[:-1]:
-            self = getattr(self, step)
+            self = self._getattr(step,
+                                 force_permissive=force_permissive,
+                                 force_properties=force_properties)
         return self, path[-1]
 
-    def _cfgimpl_get_toplevel(self):
-        ":returns: root config"
-        while self._cfgimpl_parent is not None:
-            self = self._cfgimpl_parent
-        return self
-
-    def _cfgimpl_get_path(self):
-        "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)
-    # ______________________________________________________________________
-    def cfgimpl_previous_value(self, path):
-        "stores the previous value"
-        home, name = self._cfgimpl_get_home_by_path(path)
-        return home._cfgimpl_previous_values[name]
-
-    def get_previous_value(self, name):
-        "for the time being, only the previous Option's value is accessible"
-        return self._cfgimpl_previous_values[name]
-    # ______________________________________________________________________
-    def add_warning(self, warning):
-        "Config implements its own warning pile. Could be useful"
-        self._cfgimpl_get_toplevel()._cfgimpl_warnings.append(warning)
-
-    def get_warnings(self):
-        "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())
+        return hash(self.cfgimpl_get_description().impl_getkey(self))
 
     def __eq__(self, other):
-        "Config comparison"
-        if not isinstance(other, OptionDescription):
+        "Config's comparison"
+        if not isinstance(other, Config):
             return False
-        return self.getkey() == other.getkey()
+        return self.cfgimpl_get_description().impl_getkey(self) == \
+            other.cfgimpl_get_description().impl_getkey(other)
 
     def __ne__(self, other):
-        "Config comparison"
+        "Config's comparison"
+        if not isinstance(other, Config):
+            return False
         return not self == other
+
     # ______________________________________________________________________
     def __iter__(self):
         """Pythonesque way of parsing group's ordered options.
         iteration only on Options (not OptionDescriptions)"""
-        for child in self._cfgimpl_descr._children:
+        for child in self.cfgimpl_get_description().impl_getchildren():
             if not isinstance(child, OptionDescription):
                 try:
                     yield child._name, getattr(self, child._name)
-                except:
-                    pass # option with properties
+                except GeneratorExit:
+                    raise StopIteration
+                except PropertiesOptionError:
+                    pass  # option with properties
+
+    def iter_all(self):
+        """A way of parsing options **and** groups.
+        iteration on Options and OptionDescriptions."""
+        for child in self.cfgimpl_get_description().impl_getchildren():
+            try:
+                yield child._name, getattr(self, child._name)
+            except GeneratorExit:
+                raise StopIteration
+            except PropertiesOptionError:
+                pass  # option with properties
 
     def iter_groups(self, group_type=None):
         """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).
+
+        :param group_type: if defined, is an instance of `groups.GroupType`
+                           or `groups.MasterGroupType` that lives in
+                           `setting.groups`
         """
-        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 group_type is not None:
+            if not isinstance(group_type, groups.GroupType):
+                raise TypeError(_("unknown group_type: {0}").format(group_type))
+        for child in self.cfgimpl_get_description().impl_getchildren():
             if isinstance(child, OptionDescription):
                 try:
-                    if child.get_group_type() in groups:
+                    if group_type is None or (group_type is not None and
+                                              child.impl_get_group_type()
+                                              == group_type):
                         yield child._name, getattr(self, child._name)
-                except:
-                    pass # hidden, disabled option
+                except GeneratorExit:
+                    raise StopIteration
+                except PropertiesOptionError:
+                    pass
     # ______________________________________________________________________
+
     def __str__(self):
         "Config's string representation"
         lines = []
         for name, grp in self.iter_groups():
-            lines.append("[%s]" % name)
+            lines.append("[{0}]".format(name))
         for name, value in self:
             try:
-                lines.append("%s = %s" % (name, value))
-            except:
+                lines.append("{0} = {1}".format(name, value))
+            except UnicodeEncodeError:
+                lines.append("{0} = {1}".format(name,
+                                                value.encode(default_encoding)))
+            except PropertiesOptionError:
                 pass
         return '\n'.join(lines)
 
     __repr__ = __str__
 
+    def cfgimpl_get_context(self):
+        return self._impl_context
 
-    def getpaths(self, include_groups=False, allpaths=False, mandatory=False):
-        """returns a list of all paths in self, recursively, taking care of
-        the context of properties (hidden/disabled)
+    def cfgimpl_get_description(self):
+        if self._impl_descr is None:
+            raise ConfigError(_('no option description found for this config'
+                                ' (may be metaconfig without meta)'))
+        else:
+            return self._impl_descr
 
-        :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):
-            try:
-                value = getattr(self, path)
+    def cfgimpl_get_settings(self):
+        return self.cfgimpl_get_context()._impl_settings
 
-            except MandatoryError:
-                if mandatory or allpaths:
-                    paths.append(path)
-            except PropertiesOptionError:
-                if allpaths:
-                    paths.append(path) # option which have properties added
+    def cfgimpl_get_values(self):
+        return self.cfgimpl_get_context()._impl_values
+
+    # ____________________________________________________________
+    # attribute methods
+    def __setattr__(self, name, value):
+        "attribute notation mechanism for the setting of the value of an option"
+        if name.startswith('_impl_'):
+            #self.__dict__[name] = value
+            object.__setattr__(self, name, value)
+            return
+        self._setattr(name, value)
+
+    def _setattr(self, name, value, force_permissive=False):
+        if '.' in name:
+            homeconfig, name = self.cfgimpl_get_home_by_path(name)
+            return homeconfig.__setattr__(name, value)
+        child = getattr(self.cfgimpl_get_description(), name)
+        if not isinstance(child, SymLinkOption):
+            if self._impl_path is None:
+                path = name
             else:
-                 paths.append(path)
-        return paths
+                path = self._impl_path + '.' + name
+            self.cfgimpl_get_values().setitem(child, value, path,
+                                              force_permissive=force_permissive)
+        else:
+            context = self.cfgimpl_get_context()
+            path = context.cfgimpl_get_description().impl_get_path_by_opt(
+                child._opt)
+            context._setattr(path, value, force_permissive=force_permissive)
+
+    def __delattr__(self, name):
+        child = getattr(self.cfgimpl_get_description(), name)
+        self.cfgimpl_get_values().__delitem__(child)
+
+    def __getattr__(self, name):
+        return self._getattr(name)
 
-    def _find(self, bytype, byname, byvalue, byattrs, first):
+    def _getattr(self, name, force_permissive=False, force_properties=None,
+                 validate=True):
         """
-            :param first: return only one option if True, a list otherwise
+        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
+                 otherwise
+        """
+        # 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, force_permissive=force_permissive,
+                force_properties=force_properties)
+            return homeconfig._getattr(name, force_permissive=force_permissive,
+                                       force_properties=force_properties,
+                                       validate=validate)
+        # special attributes
+        if name.startswith('_impl_') or name.startswith('cfgimpl_') \
+                or name.startswith('impl_'):
+            # if it were in __dict__ it would have been found already
+            return object.__getattribute__(self, name)
+        opt_or_descr = getattr(self.cfgimpl_get_description(), name)
+        # symlink options
+        if self._impl_path is None:
+            subpath = name
+        else:
+            subpath = self._impl_path + '.' + name
+        if isinstance(opt_or_descr, SymLinkOption):
+            context = self.cfgimpl_get_context()
+            path = context.cfgimpl_get_description().impl_get_path_by_opt(
+                opt_or_descr._opt)
+            return context._getattr(path, validate=validate,
+                                    force_properties=force_properties,
+                                    force_permissive=force_permissive)
+        elif isinstance(opt_or_descr, OptionDescription):
+            self.cfgimpl_get_settings().validate_properties(
+                opt_or_descr, True, False, path=subpath,
+                force_permissive=force_permissive,
+                force_properties=force_properties)
+            return SubConfig(opt_or_descr, self.cfgimpl_get_context(), subpath)
+        else:
+            return self.cfgimpl_get_values().getitem(
+                opt_or_descr, path=subpath,
+                validate=validate,
+                force_properties=force_properties,
+                force_permissive=force_permissive)
+
+    def find(self, bytype=None, byname=None, byvalue=None, type_='option'):
+        """
+            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
+            :returns: list of matching Option objects
+        """
+        return self.cfgimpl_get_context()._find(bytype, byname, byvalue,
+                                                first=False,
+                                                type_=type_,
+                                                _subpath=self.cfgimpl_get_path()
+                                                )
+
+    def find_first(self, bytype=None, byname=None, byvalue=None,
+                   type_='option', display_error=True):
+        """
+            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
+            :returns: list of matching Option objects
+        """
+        return self.cfgimpl_get_context()._find(
+            bytype, byname, byvalue, first=True, type_=type_,
+            _subpath=self.cfgimpl_get_path(), display_error=display_error)
+
+    def _find(self, bytype, byname, byvalue, first, type_='option',
+              _subpath=None, check_properties=True, display_error=True):
+        """
+        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
         """
-        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
         def _filter_by_name():
-            if byname is None:
-                return True
-            pathname = path.split('.')[-1]
-            if pathname == byname:
-                return True
-            else:
-                return False
+            try:
+                if byname is None or path == byname or \
+                        path.endswith('.' + byname):
+                    return True
+            except IndexError:
+                pass
+            return False
+
         def _filter_by_value():
             if byvalue is None:
                 return True
@@ -533,9 +305,11 @@ class Config(object):
                 value = getattr(self, path)
                 if value == byvalue:
                     return True
-            except Exception, e: # a property restricts the acces to value
+            except PropertiesOptionError:  # a property is a restriction
+                                           # upon the access of the value
                 pass
             return False
+
         def _filter_by_type():
             if bytype is None:
                 return True
@@ -543,82 +317,361 @@ class Config(object):
                 return True
             return False
 
+        #def _filter_by_attrs():
+        #    if byattrs is None:
+        #        return True
+        #    for key, val in byattrs.items():
+        #        print "----", path, key
+        #        if path == key or path.endswith('.' + key):
+        #            if value == val:
+        #                return True
+        #            else:
+        #                return False
+        #    return False
+        if type_ not in ('option', 'path', 'context', 'value'):
+            raise ValueError(_('unknown type_ type {0}'
+                               'for _find').format(type_))
         find_results = []
-        paths = self.getpaths(allpaths=True)
-        for path in paths:
-            option = self.unwrap_from_path(path)
+        opts, paths = self.cfgimpl_get_description()._cache_paths
+        for index in range(0, len(paths)):
+            option = opts[index]
+            if isinstance(option, OptionDescription):
+                continue
+            path = paths[index]
+            if _subpath is not None and not path.startswith(_subpath + '.'):
+                continue
             if not _filter_by_name():
                 continue
             if not _filter_by_value():
                 continue
+            #remove option with propertyerror, ...
+            if check_properties:
+                try:
+                    value = getattr(self, path)
+                except PropertiesOptionError:
+                    # a property restricts the access of the value
+                    continue
             if not _filter_by_type():
                 continue
-            if not _filter_by_attrs():
-                continue
+            #if not _filter_by_attrs():
+            #    continue
+            if type_ == 'value':
+                retval = value
+            elif type_ == 'path':
+                retval = path
+            elif type_ == 'option':
+                retval = option
+            elif type_ == 'context':
+                retval = self.cfgimpl_get_context()
             if first:
-                return option
+                return retval
             else:
-                find_results.append(option)
-        if first:
-            return None
+                find_results.append(retval)
+        if find_results == []:
+            if display_error:
+                raise AttributeError(_("no option found in config"
+                                       " with these criteria"))
+            else:
+                #translation is slow
+                raise AttributeError()
         else:
             return find_results
 
-    def find(self, bytype=None, byname=None, byvalue=None, byattrs=None):
+    def make_dict(self, flatten=False, _currpath=None, withoption=None,
+                  withvalue=None):
+        """exports the whole config into a `dict`, for example:
+
+        >>> print cfg.make_dict()
+        {'od2.var4': None, 'od2.var5': None, 'od2.var6': None}
+
+
+
+        :param flatten: returns a dict(name=value) instead of a dict(path=value)
+                        ::
+
+                            >>> print cfg.make_dict(flatten=True)
+                            {'var5': None, 'var4': None, 'var6': None}
+
+        :param withoption: returns the options that are present in the very same
+                           `OptionDescription` than the `withoption` itself::
+
+                                >>> print cfg.make_dict(withoption='var1')
+                                {'od2.var4': None, 'od2.var5': None,
+                                'od2.var6': None,
+                                'od2.var1': u'value',
+                                'od1.var1': None,
+                                'od1.var3': None,
+                                'od1.var2': None}
+
+        :param withvalue: returns the options that have the value `withvalue`
+                          ::
+
+                            >>> print c.make_dict(withoption='var1',
+                                                  withvalue=u'value')
+                            {'od2.var4': None,
+                            'od2.var5': None,
+                            'od2.var6': None,
+                            'od2.var1': u'value'}
+
+        :returns: dict of Option's name (or path) and values
         """
-            finds a list of options recursively in the config
+        pathsvalues = []
+        if _currpath is None:
+            _currpath = []
+        if withoption is None and withvalue is not None:
+            raise ValueError(_("make_dict can't filtering with value without "
+                               "option"))
+        if withoption is not None:
+            mypath = self.cfgimpl_get_path()
+            for path in self.cfgimpl_get_context()._find(bytype=Option,
+                                                         byname=withoption,
+                                                         byvalue=withvalue,
+                                                         first=False,
+                                                         type_='path',
+                                                         _subpath=mypath):
+                path = '.'.join(path.split('.')[:-1])
+                opt = self.cfgimpl_get_context().cfgimpl_get_description(
+                ).impl_get_opt_by_path(path)
+                if mypath is not None:
+                    if mypath == path:
+                        withoption = None
+                        withvalue = None
+                        break
+                    else:
+                        tmypath = mypath + '.'
+                        if not path.startswith(tmypath):
+                            raise AttributeError(_('unexpected path {0}, '
+                                                   'should start with {1}'
+                                                   '').format(path, mypath))
+                        path = path[len(tmypath):]
+                self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
+        #withoption can be set to None below !
+        if withoption is None:
+            for opt in self.cfgimpl_get_description().impl_getchildren():
+                path = opt._name
+                self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
+        if _currpath == []:
+            options = dict(pathsvalues)
+            return options
+        return pathsvalues
+
+    def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
+        if isinstance(opt, OptionDescription):
+            try:
+                pathsvalues += getattr(self, path).make_dict(flatten,
+                                                             _currpath +
+                                                             path.split('.'))
+            except PropertiesOptionError:
+                pass  # this just a hidden or disabled option
+        else:
+            try:
+                value = self._getattr(opt._name)
+                if flatten:
+                    name = opt._name
+                else:
+                    name = '.'.join(_currpath + [opt._name])
+                pathsvalues.append((name, value))
+            except PropertiesOptionError:
+                pass  # this just a hidden or disabled option
 
-            :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 cfgimpl_get_path(self):
+        descr = self.cfgimpl_get_description()
+        context_descr = self.cfgimpl_get_context().cfgimpl_get_description()
+        return context_descr.impl_get_path_by_opt(descr)
+
+
+class CommonConfig(SubConfig):
+    "abstract base class for the Config and the MetaConfig"
+    __slots__ = ('_impl_values', '_impl_settings', '_impl_meta')
+
+    def _init_storage(self, config_id, is_persistent):
+        if config_id is None:
+            config_id = gen_id(self)
+        import_lib = 'tiramisu.storage.{0}.storage'.format(storage_type)
+        return __import__(import_lib, globals(), locals(), ['Storage'],
+                          -1).Storage(config_id, is_persistent)
+
+    def _impl_build_all_paths(self):
+        self.cfgimpl_get_description().impl_build_cache()
+
+    def read_only(self):
+        "read only is a global config's setting, see `settings.py`"
+        self.cfgimpl_get_settings().read_only()
+
+    def read_write(self):
+        "read write is a global config's setting, see `settings.py`"
+        self.cfgimpl_get_settings().read_write()
+
+    def getowner(self, path):
+        """convenience method to retrieve an option's owner
+        from the config itself
         """
-        return self._find(bytype, byname, byvalue, byattrs, first=False)
+        opt = self.cfgimpl_get_description().impl_get_opt_by_path(path)
+        return self.cfgimpl_get_values().getowner(opt)
+
+    def unwrap_from_path(self, path, force_permissive=False):
+        """convenience method to extract and Option() object from the Config()
+        and it is **fast**: finds the option directly in the appropriate
+        namespace
 
-    def find_first(self, bytype=None, byname=None, byvalue=None, byattrs=None):
+        :returns: Option()
         """
-            finds an option recursively in the config
+        if '.' in path:
+            homeconfig, path = self.cfgimpl_get_home_by_path(
+                path, force_permissive=force_permissive)
+            return getattr(homeconfig.cfgimpl_get_description(), path)
+        return getattr(self.cfgimpl_get_description(), path)
 
-            :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 cfgimpl_get_path(self):
+        return None
+
+    def cfgimpl_get_meta(self):
+        return self._impl_meta
+
+
+# ____________________________________________________________
+class Config(CommonConfig):
+    "main configuration management entry"
+    __slots__ = tuple()
+
+    def __init__(self, descr, config_id=None, is_persistent=False):
+        """ Configuration option management master class
+
+        :param descr: describes the configuration schema
+        :type descr: an instance of ``option.OptionDescription``
+        :param context: the current root config
+        :type context: `Config`
         """
-        return self._find(bytype, byname, byvalue, byattrs, first=True)
-
-def make_dict(config, flatten=False):
-    """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
+        storage = self._init_storage(config_id, is_persistent)
+        self._impl_settings = Settings(self, storage)
+        self._impl_values = Values(self, storage)
+        super(Config, self).__init__(descr, self)
+        self._impl_build_all_paths()
+        self._impl_meta = None
+        self._impl_informations = {}
+
+    def cfgimpl_reset_cache(self,
+                            only_expired=False,
+                            only=('values', 'settings')):
+        if 'values' in only:
+            self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
+        if 'settings' in only:
+            self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
+
+
+class MetaConfig(CommonConfig):
+    __slots__ = ('_impl_children',)
+
+    def __init__(self, children, meta=True, config_id=None, is_persistent=False):
+        if not isinstance(children, list):
+            raise ValueError(_("metaconfig's children must be a list"))
+        self._impl_descr = None
+        self._impl_path = None
+        if meta:
+            for child in children:
+                if not isinstance(child, CommonConfig):
+                    raise ValueError(_("metaconfig's children "
+                                       "must be config, not {0}"
+                                       ).format(type(child)))
+                if self._impl_descr is None:
+                    self._impl_descr = child.cfgimpl_get_description()
+                elif not self._impl_descr is child.cfgimpl_get_description():
+                    raise ValueError(_('all config in metaconfig must '
+                                       'have the same optiondescription'))
+                if child.cfgimpl_get_meta() is not None:
+                    raise ValueError(_("child has already a metaconfig's"))
+                child._impl_meta = self
+
+        if config_id is None:
+            config_id = gen_id(self)
+        self._impl_children = children
+        storage = self._init_storage(config_id, is_persistent)
+        self._impl_settings = Settings(self, storage)
+        self._impl_values = Values(self, storage)
+        self._impl_meta = None
+        self._impl_informations = {}
+
+    def cfgimpl_get_children(self):
+        return self._impl_children
+
+    def cfgimpl_get_context(self):
+        "a meta config is a config wich has a setting, that is itself"
+        return self
+
+    def cfgimpl_reset_cache(self,
+                            only_expired=False,
+                            only=('values', 'settings')):
+        if 'values' in only:
+            self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
+        if 'settings' in only:
+            self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
+        for child in self._impl_children:
+            child.cfgimpl_reset_cache(only_expired=only_expired, only=only)
+
+    def set_contexts(self, path, value):
+        for child in self._impl_children:
+            try:
+                if not isinstance(child, MetaConfig):
+                    setattr(child, path, value)
+                else:
+                    child.set_contexts(path, value)
+            except PropertiesOptionError:
+                pass
+
+    def find_first_contexts(self, byname=None, bypath=None, byvalue=None,
+                            type_='context', display_error=True):
+        ret = []
         try:
-            value = getattr(config, path)
-            pathsvalues.append((pathname, value))
-        except:
-            pass # this just a hidden or disabled option
-    options = dict(pathsvalues)
-    return options
+            if bypath is None and byname is not None and \
+                    self.cfgimpl_get_description() is not None:
+                bypath = self._find(bytype=None, byvalue=None, byname=byname,
+                                    first=True, type_='path',
+                                    check_properties=False)
+        except ConfigError:
+            pass
+        for child in self._impl_children:
+            try:
+                if not isinstance(child, MetaConfig):
+                    if bypath is not None:
+                        if byvalue is not None:
+                            if getattr(child, bypath) == byvalue:
+                                ret.append(child)
+                        else:
+                            #not raise
+                            getattr(child, bypath)
+                            ret.append(child)
+                    else:
+                        ret.append(child.find_first(byname=byname,
+                                                    byvalue=byvalue,
+                                                    type_=type_,
+                                                    display_error=False))
+                else:
+                    ret.extend(child.find_first_contexts(byname=byname,
+                                                         bypath=bypath,
+                                                         byvalue=byvalue,
+                                                         type_=type_,
+                                                         display_error=False))
+            except AttributeError:
+                pass
+        return ret
+
 
 def mandatory_warnings(config):
     """convenience function to trace Options that are mandatory and
     where no value has been set
 
     :returns: generator of mandatory Option's path
+
     """
-    mandatory = settings.mandatory
-    settings.mandatory = True
-    for path in config._cfgimpl_descr.getpaths(include_groups=True):
+    #if value in cache, properties are not calculated
+    config.cfgimpl_reset_cache(only=('values',))
+    for path in config.cfgimpl_get_description().impl_getpaths(
+            include_groups=True):
         try:
-            value = config._getattr(path, permissive=True)
-        except MandatoryError:
-            yield path
-        except PropertiesOptionError:
-            pass
-    settings.mandatory = mandatory
+            config._getattr(path, force_properties=frozenset(('mandatory',)))
+            # XXX raise Exception("ca passe ici")
+            # XXX depuis l'exterieur on donne un paht maintenant ! perturbant !
+        except PropertiesOptionError, err:
+            if err.proptype == ['mandatory']:
+                yield path
+    config.cfgimpl_reset_cache(only=('values',))