0634e5e8c52bfe24fa22843d6624a4a304369767
[tiramisu.git] / tiramisu / storage / dictionary / option.py
1 # -*- coding: utf-8 -*-
2 ""
3 # Copyright (C) 2014 Team tiramisu (see AUTHORS for all contributors)
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 #
19 # ____________________________________________________________
20 from tiramisu.i18n import _
21 from tiramisu.setting import groups
22
23
24 #____________________________________________________________
25 #
26 # Base
27 class Base(object):
28     __slots__ = ('_name', '_requires', '_properties', '_readonly',
29                  '_calc_properties', '_informations',
30                  '_state_readonly', '_state_requires', '_stated',
31                  '_multi', '_validator', '_validator_params', '_default',
32                  '_default_multi', '_state_callback', '_callback',
33                  '_callback_params', '_multitype', '_consistencies',
34                  '_warnings_only', '_master_slaves', '_state_consistencies',
35                  '_extra', '__weakref__')
36
37     def __init__(self):
38         try:
39             self._consistencies
40         except AttributeError:
41             self._consistencies = []
42         try:
43             self._callback
44         except AttributeError:
45             self._callback = None
46         try:
47             self._callback_params
48         except AttributeError:
49             self._callback_params = None
50         try:
51             self._validator
52         except AttributeError:
53             self._validator = None
54         try:
55             self._validator_params
56         except AttributeError:
57             self._validator_params = None
58
59     def _add_consistency(self, func, all_cons_opts, params):
60         self._consistencies.append((func, all_cons_opts, params))
61
62     def _get_consistencies(self):
63         return self._consistencies
64
65     def _get_id(self):
66         return id(self)
67
68
69 class OptionDescription(Base):
70     __slots__ = ('_children', '_cache_paths', '_cache_consistencies',
71                  '_group_type', '_is_build_cache')
72
73     def __init__(self):
74         pass
75
76     def _add_children(self, child_names, children):
77         self._children = (tuple(child_names), tuple(children))
78
79     def impl_already_build_caches(self):
80         return self._is_build_cache
81
82     def impl_get_opt_by_path(self, path):
83         try:
84             return self._cache_paths[0][self._cache_paths[1].index(path)]
85         except ValueError:
86             raise AttributeError(_('no option for path {0}').format(path))
87
88     def impl_get_path_by_opt(self, opt):
89         try:
90             return self._cache_paths[1][self._cache_paths[0].index(opt)]
91         except ValueError:
92             raise AttributeError(_('no option {0} found').format(opt))
93
94     def impl_get_group_type(self):
95         return getattr(groups, self._group_type)
96
97     def impl_build_cache_option(self, _currpath=None, cache_path=None, cache_option=None):
98         if _currpath is None and self._cache_paths is not None:
99             # cache already set
100             return
101         if _currpath is None:
102             save = True
103             _currpath = []
104         else:
105             save = False
106         if cache_path is None:
107             cache_path = []
108             cache_option = []
109         for option in self.impl_getchildren():
110             attr = option._name
111             cache_option.append(option)
112             cache_path.append(str('.'.join(_currpath + [attr])))
113             if option.__class__.__name__ == 'OptionDescription':
114                 _currpath.append(attr)
115                 option.impl_build_cache_option(_currpath, cache_path,
116                                                cache_option)
117                 _currpath.pop()
118         if save:
119             self._cache_paths = (tuple(cache_option), tuple(cache_path))
120
121     def impl_get_options_paths(self, bytype, byname, _subpath, only_first):
122         def _filter_by_name():
123             if byname is None or path == byname or \
124                     path.endswith('.' + byname):
125                 return True
126             return False
127
128         def _filter_by_type():
129             if bytype is None:
130                 return True
131             if isinstance(option, bytype):
132                 return True
133             return False
134
135         find_results = []
136         opts, paths = self._cache_paths
137         for index in range(0, len(paths)):
138             option = opts[index]
139             if option.__class__.__name__ == 'OptionDescription':
140                 continue
141             path = paths[index]
142             if _subpath is not None and not path.startswith(_subpath + '.'):
143                 continue
144             if not _filter_by_name():
145                 continue
146             if not _filter_by_type():
147                 continue
148             retval = (path, option)
149             if only_first:
150                 return retval
151             find_results.append(retval)
152         return find_results
153
154     def impl_getchildren(self):
155         return self._children[1]
156
157     def __getattr__(self, name):
158         if name == '_name':
159             return object.__getattribute__(self, name)
160         try:
161             return self._children[1][self._children[0].index(name)]
162         except ValueError:
163             raise AttributeError(_('unknown Option {0} '
164                                    'in OptionDescription {1}'
165                                    '').format(name, self._name))