ee72ec10a3409e4aad581c5428b0eca534122f80
[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 tiramisu.i18n import _
23 from tiramisu.setting import log, undefined
24 from tiramisu.error import SlaveError, ConfigError
25 from .baseoption import DynSymLinkOption, SymLinkOption, Option
26
27
28 class MasterSlaves(object):
29     __slots__ = ('master', 'slaves')
30
31     def __init__(self, name, childs, validate=True):
32         #if master (same name has group) is set
33         #for collect all slaves
34         self.master = None
35         slaves = []
36         for child in childs:
37             if isinstance(child, SymLinkOption):  # pragma: optional cover
38                 raise ValueError(_("master group {0} shall not have "
39                                    "a symlinkoption").format(name))
40             if not isinstance(child, Option):  # pragma: optional cover
41                 raise ValueError(_("master group {0} shall not have "
42                                    "a subgroup").format(name))
43             if not child.impl_is_multi():  # pragma: optional cover
44                 raise ValueError(_("not allowed option {0} "
45                                    "in group {1}"
46                                    ": this option is not a multi"
47                                    "").format(child.impl_getname(), name))
48             if child.impl_getname() == name:
49                 self.master = child
50             else:
51                 slaves.append(child)
52         if self.master is None:  # pragma: optional cover
53             raise ValueError(_('master group with wrong'
54                                ' master name for {0}'
55                                ).format(name))
56         if validate:
57             callback, callback_params = self.master.impl_get_callback()
58             if callback is not None and callback_params != {}:  # pragma: optional cover
59                 for key, callbacks in callback_params.items():
60                     for callbk in callbacks:
61                         if isinstance(callbk, tuple):
62                             if callbk[0] in slaves:
63                                 raise ValueError(_("callback of master's option shall "
64                                                    "not refered a slave's ones"))
65         #everything is ok, store references
66         self.slaves = tuple(slaves)
67         for child in childs:
68             child._master_slaves = self
69
70     def is_master(self, opt):
71         return opt == self.master or (isinstance(opt, DynSymLinkOption) and
72                                       opt._opt == self.master)
73
74     def getmaster(self, opt):
75         if isinstance(opt, DynSymLinkOption):
76             suffix = opt.impl_getsuffix()
77             name = self.master.impl_getname() + suffix
78             base_path = opt._dyn.split('.')[0] + '.'
79             path = base_path + name
80             master = self.master._impl_to_dyn(name, path)
81         else:  # pragma: no dynoptiondescription cover
82             master = self.master
83         return master
84
85     def getslaves(self, opt):
86         if isinstance(opt, DynSymLinkOption):
87             for slave in self.slaves:
88                 suffix = opt.impl_getsuffix()
89                 name = slave.impl_getname() + suffix
90                 base_path = opt._dyn.split('.')[0] + '.'
91                 path = base_path + name
92                 yield slave._impl_to_dyn(name, path)
93         else:  # pragma: no dynoptiondescription cover
94             for slave in self.slaves:
95                 yield slave
96
97     def in_same_group(self, opt):
98         if isinstance(opt, DynSymLinkOption):
99             return opt._opt == self.master or opt._opt in self.slaves
100         else:  # pragma: no dynoptiondescription cover
101             return opt == self.master or opt in self.slaves
102
103     def reset(self, opt, values):
104         #FIXME pas de opt ???
105         for slave in self.getslaves(opt):
106             values.reset(slave)
107
108     def pop(self, opt, values, index):
109         #FIXME pas test de meta ...
110         for slave in self.getslaves(opt):
111             if not values.is_default_owner(slave, validate_properties=False,
112                                            validate_meta=False):
113                 values._get_cached_item(slave, validate=False,
114                                         validate_properties=False
115                                         ).pop(index, force=True)
116         pass
117
118     def getitem(self, values, opt, path, validate, force_permissive,
119                 force_properties, validate_properties, slave_path=undefined,
120                 slave_value=undefined):
121         if self.is_master(opt):
122             return self._getmaster(values, opt, path, validate,
123                                    force_permissive, force_properties,
124                                    validate_properties, slave_path,
125                                    slave_value)
126         else:
127             return self._getslave(values, opt, path, validate,
128                                   force_permissive, force_properties,
129                                   validate_properties)
130
131     def _getmaster(self, values, opt, path, validate, force_permissive,
132                    force_properties, validate_properties, c_slave_path,
133                    c_slave_value):
134         value = values._get_validated_value(opt, path, validate,
135                                             force_permissive,
136                                             force_properties,
137                                             validate_properties)
138         if validate is True:
139             masterlen = len(value)
140             for slave in self.getslaves(opt):
141                 try:
142                     slave_path = slave.impl_getpath(values._getcontext())
143                     if c_slave_path == slave_path:
144                         slave_value = c_slave_value
145                     else:
146                         slave_value = values._get_validated_value(slave,
147                                                                   slave_path,
148                                                                   False,
149                                                                   False,
150                                                                   None, False,
151                                                                   None)  # not undefined
152                     slavelen = len(slave_value)
153                     self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
154                 except ConfigError:  # pragma: optional cover
155                     pass
156         return value
157
158     def _getslave(self, values, opt, path, validate, force_permissive,
159                   force_properties, validate_properties):
160         value = values._get_validated_value(opt, path, validate,
161                                             force_permissive,
162                                             force_properties,
163                                             validate_properties,
164                                             None)  # not undefined
165         return self.get_slave_value(values, opt, value, validate, validate_properties)
166
167     def setitem(self, values, opt, value, path):
168         if self.is_master(opt):
169             masterlen = len(value)
170             for slave in self.getslaves(opt):
171                 slave_path = slave.impl_getpath(values._getcontext())
172                 slave_value = values._get_validated_value(slave,
173                                                           slave_path,
174                                                           False,
175                                                           False,
176                                                           None, False,
177                                                           None)  # not undefined
178                 slavelen = len(slave_value)
179                 self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
180         else:
181             self.validate_slave_length(self.get_length(values, opt,
182                                                        slave_path=path), len(value),
183                                        opt.impl_getname(), opt, setitem=True)
184
185     def get_length(self, values, opt, validate=True, slave_path=undefined,
186                    slave_value=undefined):
187         """get master len with slave option"""
188         masterp = self.getmaster(opt).impl_getpath(values._getcontext())
189         if slave_value is undefined:
190             slave_path = undefined
191         return len(self.getitem(values, self.getmaster(opt), masterp, validate, False,
192                                 None, True, slave_path, slave_value))
193
194     def validate_slave_length(self, masterlen, valuelen, name, opt, setitem=False):
195         if valuelen > masterlen or (valuelen < masterlen and setitem):  # pragma: optional cover
196             log.debug('validate_slave_length: masterlen: {0}, valuelen: {1}, '
197                       'setitem: {2}'.format(masterlen, valuelen, setitem))
198             raise SlaveError(_("invalid len for the slave: {0}"
199                                " which has {1} as master").format(
200                                    name, self.getmaster(opt).impl_getname()))
201
202     def get_slave_value(self, values, opt, value, validate=True,
203                         validate_properties=True):
204         """
205         if master has length 0:
206             return []
207         if master has length bigger than 0:
208             if default owner:
209                 if has callback:
210                     if return a list:
211                         list same length as master: return list
212                         list is smaller than master: return list + None
213                         list is greater than master: raise SlaveError
214                 if has default value:
215                     list same length as master: return list
216                     list is smaller than master: return list + None
217                     list is greater than master: raise SlaveError
218                 if has default_multi value:
219                     return default_multi * master's length
220             if has value:
221                 list same length as master: return list
222                 list is smaller than master: return list + None
223                 list is greater than master: raise SlaveError
224         """
225         #if slave, had values until master's one
226         path = opt.impl_getpath(values._getcontext())
227         masterlen = self.get_length(values, opt, validate, path, value)
228         valuelen = len(value)
229         if validate:
230             self.validate_slave_length(masterlen, valuelen, opt.impl_getname(), opt)
231         if valuelen < masterlen:
232             for num in range(0, masterlen - valuelen):
233                 index = valuelen + num
234                 value.append(values._get_validated_value(opt, path, True,
235                                                          False, None,
236                                                          validate_properties,
237                                                          index=index),
238                              setitem=False,
239                              force=True)
240         return value