21048c9af9b8e9d61188692eeba82d92f90f4a16
[tiramisu.git] / tiramisu / option / masterslave.py
1 # -*- coding: utf-8 -*-
2 "master slave support"
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 it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # The original `Config` design model is unproudly borrowed from
19 # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
20 # the whole pypy projet is under MIT licence
21 # ____________________________________________________________
22 from ..i18n import _
23 from ..setting import log, undefined, debug
24 from ..error import SlaveError, PropertiesOptionError
25 from ..storage import get_storages_option
26
27
28 StorageMasterSlaves = get_storages_option('masterslaves')
29
30
31 class MasterSlaves(object):
32     __slots__ = ('_p_')
33
34     def __init__(self, name, childs=None, validate=True, add=True):
35         #if master (same name has group) is set
36         #for collect all slaves
37         if isinstance(name, StorageMasterSlaves):
38             self._p_ = name
39         else:
40             slaves = []
41             if childs[0].impl_getname() == name:
42                 master = childs[0]
43             else:
44                 raise ValueError(_('master group with wrong'
45                                    ' master name for {0}'
46                                    ).format(name))
47             for child in childs[1:]:
48                 if child.impl_getdefault() != []:
49                     raise ValueError(_("not allowed default value for option {0} "
50                                        "in group {1}").format(child.impl_getname(),
51                                                               name))
52                 slaves.append(child)
53             if validate:
54                 callback, callback_params = master.impl_get_callback()
55                 if callback is not None and callback_params != {}:  # pragma: optional cover
56                     for key, callbacks in callback_params.items():
57                         for callbk in callbacks:
58                             if isinstance(callbk, tuple):
59                                 if callbk[0] in slaves:
60                                     raise ValueError(_("callback of master's option shall "
61                                                        "not refered a slave's ones"))
62             #everything is ok, store references
63             self._p_ = StorageMasterSlaves(master, slaves)
64             if add:
65                 for child in childs:
66                     child._set_master_slaves(self)
67
68     def is_master(self, opt):
69         master = self._p_._sm_getmaster().impl_getname()
70         return opt.impl_getname() == master or (opt.impl_is_dynsymlinkoption() and
71                                       opt._opt.impl_getname() == master)
72
73     def getmaster(self, opt):
74         master = self._p_._sm_getmaster()
75         if opt.impl_is_dynsymlinkoption():
76             suffix = opt.impl_getsuffix()
77             name = master.impl_getname() + suffix
78             base_path = opt._dyn.split('.')[0] + '.'
79             path = base_path + name
80             master = master._impl_to_dyn(name, path)
81         return master
82
83     def getslaves(self, opt):
84         if opt.impl_is_dynsymlinkoption():
85             for slave in self._p_._sm_getslaves():
86                 suffix = opt.impl_getsuffix()
87                 name = slave.impl_getname() + suffix
88                 base_path = opt._dyn.split('.')[0] + '.'
89                 path = base_path + name
90                 yield slave._impl_to_dyn(name, path)
91         else:  # pragma: no dynoptiondescription cover
92             for slave in self._p_._sm_getslaves():
93                 yield slave
94
95     def in_same_group(self, opt):
96         if opt.impl_is_dynsymlinkoption():
97             return opt._opt == self._p_._sm_getmaster() or opt._opt in self._p_._sm_getslaves()
98         else:  # pragma: no dynoptiondescription cover
99             return opt == self._p_._sm_getmaster() or opt in self._p_._sm_getslaves()
100
101     def reset(self, opt, values, setting_properties):
102         for slave in self.getslaves(opt):
103             values.reset(slave, validate=False, _setting_properties=setting_properties)
104
105     def pop(self, opt, values, index):
106         for slave in self.getslaves(opt):
107             if not values.is_default_owner(slave, validate_properties=False,
108                                            validate_meta=False, index=index):
109                 multi = values._get_cached_value(slave, validate=False,
110                                                validate_properties=False,
111                                                )
112                 if isinstance(multi, Exception):
113                     raise multi
114                 multi.pop(index, force=True)
115
116     def getitem(self, values, opt, path, validate, force_permissive,
117                 trusted_cached_properties, validate_properties, session,
118                 slave_path=undefined, slave_value=undefined,
119                 setting_properties=undefined, self_properties=undefined, index=None):
120         if self.is_master(opt):
121             return self._getmaster(values, opt, path, validate,
122                                    force_permissive,
123                                    validate_properties, slave_path,
124                                    slave_value, self_properties, index,
125                                    setting_properties, session)
126         else:
127             return self._getslave(values, opt, path, validate,
128                                   force_permissive, trusted_cached_properties,
129                                   validate_properties, setting_properties,
130                                   self_properties, index,
131                                   session)
132
133     def _getmaster(self, values, opt, path, validate, force_permissive,
134                    validate_properties, c_slave_path,
135                    c_slave_value, self_properties, index,
136                    setting_properties, session):
137         value = values._get_cached_value(opt, path=path, validate=validate,
138                                          force_permissive=force_permissive,
139                                          validate_properties=validate_properties,
140                                          self_properties=self_properties,
141                                          from_masterslave=True, index=index,
142                                          setting_properties=setting_properties)
143         if isinstance(value, Exception):
144             return value
145         if index is None and validate is True:
146             masterlen = len(value)
147             for slave in self.getslaves(opt):
148                 slave_path = slave.impl_getpath(values._getcontext())
149                 slavelen = values._p_.get_max_length(slave_path, session)
150                 self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
151         return value
152
153     def _getslave(self, values, opt, path, validate, force_permissive,
154                   trusted_cached_properties, validate_properties, setting_properties,
155                   self_properties, index, session):
156         """
157         if master has length 0:
158             return []
159         if master has length bigger than 0:
160             if default owner:
161                 if has callback:
162                     if return a list:
163                         list same length as master: return list
164                         list is smaller than master: return list + None
165                         list is greater than master: raise SlaveError
166                 if has default value:
167                     list same length as master: return list
168                     list is smaller than master: return list + None
169                     list is greater than master: raise SlaveError
170                 if has default_multi value:
171                     return default_multi * master's length
172             if has value:
173                 list same length as master: return list
174                 list is smaller than master: return list + None
175                 list is greater than master: raise SlaveError
176         """
177         master = self.getmaster(opt)
178         context = values._getcontext()
179         masterp = master.impl_getpath(context)
180         masterlen = self.get_length(values, opt, session, validate, undefined,
181                                     undefined, force_permissive,
182                                     master=master)
183         if isinstance(masterlen, Exception):
184             return masterlen
185         master_is_meta = values._is_meta(master, masterp, session)
186         multi = values._get_multi(opt, path)
187         #if masterlen is [], test properties (has no value, don't get any value)
188         #if masterlen == 0:
189         if validate_properties:
190             props = context.cfgimpl_get_settings().validate_properties(opt, False,
191                                                                        False,
192                                                                        value=multi,
193                                                                        path=path,
194                                                                        force_permissive=force_permissive,
195                                                                        setting_properties=setting_properties)
196             if props:
197                 return props
198         #else:
199         if index is None:
200             indexes = range(0, masterlen)
201         else:
202             indexes = [index]
203         for idx in indexes:
204             value = values._get_cached_value(opt, path, validate,
205                                              force_permissive,
206                                              trusted_cached_properties,
207                                              validate_properties,
208                                              with_meta=master_is_meta,
209                                              index=idx,
210                                              # not self_properties,
211                                              # depends to index
212                                              #self_properties=self_properties,
213                                              setting_properties=setting_properties,
214                                              masterlen=masterlen,
215                                              from_masterslave=True)
216             if isinstance(value, Exception):
217                 if isinstance(value, PropertiesOptionError):
218                     err = value
219                     if index is None:
220                         multi.append_properties_error(value)
221                     else:
222                         multi = value
223                 else:
224                     return value
225             elif index is None:
226                 multi.append(value, setitem=False, force=True, validate=validate,
227                              force_permissive=force_permissive)
228             else:
229                 multi = value
230         return multi
231
232     def validate(self, values, opt, value, path, session):
233         if self.is_master(opt):
234             masterlen = len(value)
235             #for regen slave path
236             base_path = '.'.join(path.split('.')[:-1]) + '.'
237             for slave in self.getslaves(opt):
238                 slave_path = base_path + slave.impl_getname()
239                 slavelen = values._p_.get_max_length(slave_path, session)
240                 self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
241         else:
242             val_len = self.get_length(values, opt, session, slave_path=path)
243             if isinstance(val_len, Exception):
244                 return val_len
245             self.validate_slave_length(val_len,
246                                        len(value),
247                                        opt.impl_getname(), opt, setitem=True)
248
249     def get_length(self, values, opt, session, validate=True, slave_path=undefined,
250                    slave_value=undefined, force_permissive=False, master=None,
251                    masterp=None):
252         """get master len with slave option"""
253         if master is None:
254             master = self.getmaster(opt)
255         if masterp is None:
256             masterp = master.impl_getpath(values._getcontext())
257         if slave_value is undefined:
258             slave_path = undefined
259         value = self.getitem(values, master, masterp, validate,
260                              force_permissive, None, True, session, slave_path=slave_path,
261                              slave_value=slave_value)
262         if isinstance(value, Exception):
263             return value
264         return len(value)
265
266     def validate_slave_length(self, masterlen, valuelen, name, opt, setitem=False):
267         if valuelen > masterlen or (valuelen < masterlen and setitem):  # pragma: optional cover
268             if debug:
269                 log.debug('validate_slave_length: masterlen: {0}, valuelen: {1}, '
270                           'setitem: {2}'.format(masterlen, valuelen, setitem))
271             raise SlaveError(_("invalid len for the slave: {0}"
272                                " which has {1} as master").format(
273                                    name, self.getmaster(opt).impl_getname()))