diff --git a/tiramisu/storage/dictionary/option.py b/tiramisu/storage/dictionary/option.py new file mode 100644 index 0000000..cdddce6 --- /dev/null +++ b/tiramisu/storage/dictionary/option.py @@ -0,0 +1,165 @@ +# -*- coding: utf-8 -*- +"" +# Copyright (C) 2014 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 +# +# ____________________________________________________________ +from tiramisu.i18n import _ +from tiramisu.setting import groups + + +#____________________________________________________________ +# +# Base +class Base(object): + __slots__ = ('_name', '_requires', '_properties', '_readonly', + '_calc_properties', '_informations', + '_state_readonly', '_state_requires', '_stated', + '_multi', '_validator', '_validator_params', '_default', + '_default_multi', '_state_callback', '_callback', + '_callback_params', '_multitype', '_consistencies', + '_warnings_only', '_master_slaves', '_state_consistencies', + '_extra', '__weakref__') + + def __init__(self): + try: + self._consistencies + except AttributeError: + self._consistencies = [] + try: + self._callback + except AttributeError: + self._callback = None + try: + self._callback_params + except AttributeError: + self._callback_params = None + try: + self._validator + except AttributeError: + self._validator = None + try: + self._validator_params + except AttributeError: + self._validator_params = None + + def _add_consistency(self, func, all_cons_opts): + self._consistencies.append((func, all_cons_opts)) + + def _get_consistencies(self): + return self._consistencies + + def _get_id(self): + return id(self) + + +class OptionDescription(Base): + __slots__ = ('_children', '_cache_paths', '_cache_consistencies', + '_group_type', '_is_build_cache') + + def __init__(self): + pass + + def _add_children(self, child_names, children): + self._children = (tuple(child_names), tuple(children)) + + def impl_already_build_caches(self): + return self._is_build_cache + + def impl_get_opt_by_path(self, path): + try: + return self._cache_paths[0][self._cache_paths[1].index(path)] + except ValueError: + raise AttributeError(_('no option for path {0}').format(path)) + + def impl_get_path_by_opt(self, opt): + try: + return self._cache_paths[1][self._cache_paths[0].index(opt)] + except ValueError: + raise AttributeError(_('no option {0} found').format(opt)) + + def impl_get_group_type(self): + return getattr(groups, self._group_type) + + def impl_build_cache_option(self, _currpath=None, cache_path=None, cache_option=None): + if _currpath is None and self._cache_paths is not None: + # cache already set + return + if _currpath is None: + save = True + _currpath = [] + else: + save = False + if cache_path is None: + cache_path = [] + cache_option = [] + for option in self.impl_getchildren(): + attr = option._name + cache_option.append(option) + cache_path.append(str('.'.join(_currpath + [attr]))) + if option.__class__.__name__ == 'OptionDescription': + _currpath.append(attr) + option.impl_build_cache_option(_currpath, cache_path, + cache_option) + _currpath.pop() + if save: + self._cache_paths = (tuple(cache_option), tuple(cache_path)) + + def impl_get_options_paths(self, bytype, byname, _subpath, only_first): + def _filter_by_name(): + if byname is None or path == byname or \ + path.endswith('.' + byname): + return True + return False + + def _filter_by_type(): + if bytype is None: + return True + if isinstance(option, bytype): + return True + return False + + find_results = [] + opts, paths = self._cache_paths + for index in range(0, len(paths)): + option = opts[index] + if option.__class__.__name__ == '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_type(): + continue + retval = (path, option) + if only_first: + return retval + find_results.append(retval) + return find_results + + def impl_getchildren(self): + return self._children[1] + + def __getattr__(self, name): + if name == '_name': + return object.__getattribute__(self, name) + try: + return self._children[1][self._children[0].index(name)] + except ValueError: + raise AttributeError(_('unknown Option {0} ' + 'in OptionDescription {1}' + '').format(name, self._name))