requires could be apply to a slave and properties could be different
[tiramisu.git] / tiramisu / value.py
1 # -*- coding: utf-8 -*-
2 "takes care of the option's values and multi values"
3 # Copyright (C) 2013-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 from time import time
19 import sys
20 import weakref
21 from .error import ConfigError, SlaveError, PropertiesOptionError
22 from .setting import owners, expires_time, undefined
23 from .autolib import carry_out_calculation
24 from .i18n import _
25 from .option import SymLinkOption, DynSymLinkOption, Option
26
27
28 class Values(object):
29     """The `Config`'s root is indeed  in charge of the `Option()`'s values,
30     but the values are physicaly located here, in `Values`, wich is also
31     responsible of a caching utility.
32     """
33     __slots__ = ('context', '_p_', '__weakref__')
34
35     def __init__(self, context, storage):
36         """
37         Initializes the values's dict.
38
39         :param context: the context is the home config's values
40
41         """
42         self.context = weakref.ref(context)
43         # the storage type is dictionary or sqlite3
44         self._p_ = storage
45
46     def _getcontext(self):
47         """context could be None, we need to test it
48         context is None only if all reference to `Config` object is deleted
49         (for example we delete a `Config` and we manipulate a reference to
50         old `SubConfig`, `Values`, `Multi` or `Settings`)
51         """
52         context = self.context()
53         if context is None:  # pragma: optional cover
54             raise ConfigError(_('the context does not exist anymore'))
55         return context
56
57     def _get_multi(self, opt, path):
58         return Multi([], self.context, opt, path)
59
60     def _getdefaultvalue(self, opt, path, with_meta, index, submulti_index):
61         # if value has callback and is not set
62         if opt.impl_has_callback():
63             callback, callback_params = opt.impl_get_callback()
64             value = carry_out_calculation(opt, context=self._getcontext(),
65                                           callback=callback,
66                                           callback_params=callback_params,
67                                           index=index)
68             if isinstance(value, list) and index is not None:
69                 #if return a list and index is set, return value only if
70                 #it's a submulti without submulti_index and without list of list
71                 if opt.impl_is_submulti() and submulti_index is undefined and \
72                         (len(value) == 0 or not isinstance(value[0], list)):
73                     return value
74             else:
75                 return value
76         if with_meta:
77             meta = self._getcontext().cfgimpl_get_meta()
78             if meta is not None:
79                 try:
80                     value = meta.cfgimpl_get_values(
81                     )._get_cached_value(opt, path, index=index, from_masterslave=True)
82                     if isinstance(value, Multi):
83                         if index is not None:
84                             value = value[index]
85                         else:
86                             value = list(value)
87                     return value
88                 except PropertiesOptionError:
89                     pass
90         # now try to get default value
91         value = opt.impl_getdefault()
92         if opt.impl_is_multi() and index is not None:
93             if value == []:
94                 value = opt.impl_getdefault_multi()
95             else:
96                 try:
97                     value = value[index]
98                 except IndexError:
99                     value = opt.impl_getdefault_multi()
100         return value
101
102     def _getvalue(self, opt, path, is_default, self_properties,
103                   index=undefined, submulti_index=undefined, with_meta=True,
104                   masterlen=undefined):
105         """actually retrieves the value
106
107         :param opt: the `option.Option()` object
108         :returns: the option's value (or the default value if not set)
109         """
110         force_default = 'frozen' in self_properties and \
111             'force_default_on_freeze' in self_properties
112         # not default value
113         if not is_default and not force_default:
114             if opt.impl_is_master_slaves('slave'):
115                 return self._p_.getvalue(path, index)
116             else:
117                 value = self._p_.getvalue(path)
118                 if index is not None:
119                     try:
120                         return value[index]
121                     except IndexError:
122                         #value is smaller than expected
123                         #so return default value
124                         pass
125                 else:
126                     return value
127         return self._getdefaultvalue(opt, path, with_meta, index, submulti_index)
128
129     def get_modified_values(self):
130         context = self._getcontext()
131         if context._impl_descr is not None:
132             for opt, path in context.cfgimpl_get_description(
133             )._get_force_store_value():
134                 self._getowner(opt, path, force_permissive=True)
135         return self._p_.get_modified_values()
136
137     def __contains__(self, opt):
138         """
139         implements the 'in' keyword syntax in order provide a pythonic way
140         to kow if an option have a value
141
142         :param opt: the `option.Option()` object
143         """
144         path = opt.impl_getpath(self._getcontext())
145         return self._contains(path)
146
147     def _contains(self, path):
148         return self._p_.hasvalue(path)
149
150     def __delitem__(self, opt):
151         """overrides the builtins `del()` instructions"""
152         self.reset(opt)
153
154     def reset(self, opt, path=None, validate=True):
155         if path is None:
156             path = opt.impl_getpath(self._getcontext())
157         context = self._getcontext()
158         if validate:
159             context.cfgimpl_get_settings().validate_properties(opt, False,
160                                                                True, path)
161
162         hasvalue = self._contains(path)
163
164         setting = context.cfgimpl_get_settings()
165         setting_properties = setting._getproperties(read_write=False)
166         if 'validator' in setting_properties and validate and hasvalue:
167             fake_context = context._gen_fake_values()
168             fake_value = fake_context.cfgimpl_get_values()
169             fake_value.reset(opt, path, validate=False)
170             opt.impl_validate(getattr(fake_context, path),
171                               fake_context, 'validator' in setting_properties)
172         context.cfgimpl_reset_cache()
173         if opt.impl_is_master_slaves('master'):
174             opt.impl_get_master_slaves().reset(opt, self)
175         if hasvalue:
176             self._p_.resetvalue(path)
177
178     def _isempty(self, opt, value, force_allow_empty_list=False, index=None):
179         "convenience method to know if an option is empty"
180         if value is undefined:
181             return False
182         else:
183             empty = opt._empty
184             if index in [None, undefined] and opt.impl_is_multi():
185                 if force_allow_empty_list:
186                     allow_empty_list = True
187                 else:
188                     allow_empty_list = opt.impl_allow_empty_list()
189                     if allow_empty_list is undefined:
190                         if opt.impl_is_master_slaves('slave'):
191                             allow_empty_list = True
192                         else:
193                             allow_empty_list = False
194                 isempty = (not allow_empty_list and value == []) or \
195                     None in value or empty in value
196             else:
197                 isempty = value is None or value == empty
198         return isempty
199
200     def __getitem__(self, opt):
201         "enables us to use the pythonic dictionary-like access to values"
202         return self.getitem(opt)
203
204     def getitem(self, opt, validate=True, force_permissive=False):
205         """
206         """
207         return self._get_cached_value(opt, validate=validate,
208                                       force_permissive=force_permissive)
209
210     def _get_cached_value(self, opt, path=None, validate=True,
211                           force_permissive=False, force_properties=None,
212                           validate_properties=True,
213                           setting_properties=undefined, self_properties=undefined,
214                           index=None, from_masterslave=False, with_meta=True,
215                           masterlen=undefined):
216         untrusted_cached_properties = force_properties is None
217         context = self._getcontext()
218         if path is None:
219             path = opt.impl_getpath(context)
220         ntime = None
221         if setting_properties is undefined:
222             setting_properties = context.cfgimpl_get_settings(
223             )._getproperties(read_write=False)
224         if self_properties is undefined:
225             self_properties = context.cfgimpl_get_settings()._getproperties(
226                 opt, path, read_write=False,
227                 setting_properties=setting_properties, index=index)
228         if 'cache' in setting_properties and self._p_.hascache(path, index):
229             if 'expire' in setting_properties:
230                 ntime = int(time())
231             is_cached, value = self._p_.getcache(path, ntime, index)
232             if is_cached:
233                 if opt.impl_is_multi() and not isinstance(value, Multi) and index is None:
234                     value = Multi(value, self.context, opt, path)
235                 if not untrusted_cached_properties:
236                     # revalidate properties (because not default properties)
237                     context.cfgimpl_get_settings().validate_properties(opt, False, False, value=value,
238                                                                        path=path,
239                                                                        force_permissive=force_permissive,
240                                                                        force_properties=force_properties,
241                                                                        setting_properties=setting_properties,
242                                                                        self_properties=self_properties,
243                                                                        index=index)
244                 return value
245         if not from_masterslave and opt.impl_is_master_slaves():
246             val = opt.impl_get_master_slaves().getitem(self, opt, path,
247                                                        validate,
248                                                        force_permissive,
249                                                        force_properties,
250                                                        validate_properties,
251                                                        setting_properties=setting_properties,
252                                                        index=index,
253                                                        self_properties=self_properties)
254         else:
255             val = self._get_validated_value(opt, path, validate,
256                                             force_permissive,
257                                             force_properties,
258                                             validate_properties,
259                                             setting_properties=setting_properties,
260                                             self_properties=self_properties,
261                                             with_meta=with_meta,
262                                             masterlen=masterlen,
263                                             index=index)
264         # cache doesn't work with SubMulti yet
265         if not isinstance(val, SubMulti) and 'cache' in setting_properties and validate and validate_properties \
266                 and force_permissive is False and force_properties is None:
267             if 'expire' in setting_properties:
268                 if ntime is None:
269                     ntime = int(time())
270                 ntime = ntime + expires_time
271             self._p_.setcache(path, val, ntime, index)
272         return val
273
274     def _get_validated_value(self, opt, path, validate, force_permissive,
275                              force_properties, validate_properties,
276                              index=None, submulti_index=undefined,
277                              with_meta=True, setting_properties=undefined,
278                              self_properties=undefined, masterlen=undefined):
279         """same has getitem but don't touch the cache
280         index is None for slave value, if value returned is not a list, just return []
281         """
282         context = self._getcontext()
283         setting = context.cfgimpl_get_settings()
284         if setting_properties is undefined:
285             setting_properties = setting._getproperties(read_write=False)
286         if self_properties is undefined:
287             self_properties = setting._getproperties(opt, path, read_write=False, index=index)
288         is_default = self._is_default_owner(opt, path,
289                                             validate_properties=False,
290                                             validate_meta=False,
291                                             self_properties=self_properties,
292                                             index=index)
293         try:
294             value = self._getvalue(opt, path, is_default, self_properties,
295                                    index=index, submulti_index=submulti_index,
296                                    with_meta=with_meta,
297                                    masterlen=masterlen)
298             config_error = None
299         except ConfigError as err:
300             # For calculating properties, we need value (ie for mandatory
301             # value).
302             # If value is calculating with a PropertiesOptionError's option
303             # _getvalue raise a ConfigError.
304             # We can not raise ConfigError if this option should raise
305             # PropertiesOptionError too. So we get config_error and raise
306             # ConfigError if properties did not raise.
307             # cannot assign config_err directly in python 3.3
308             config_error = err
309             # value is not set, for 'undefined' (cannot set None because of
310             # mandatory property)
311             value = undefined
312         else:
313             if index is undefined:
314                 force_index = None
315             else:
316                 force_index = index
317             if opt.impl_is_multi():
318                 if force_index is None:
319                     value = Multi(value, self.context, opt, path)
320                 elif opt.impl_is_submulti() and submulti_index is undefined:
321                     value = SubMulti(value, self.context, opt, path,
322                                      force_index)
323
324             if validate:
325                 if submulti_index is undefined:
326                     force_submulti_index = None
327                 else:
328                     force_submulti_index = submulti_index
329                 try:
330                     opt.impl_validate(value, context,
331                                       'validator' in setting_properties,
332                                       force_index=force_index,
333                                       force_submulti_index=force_submulti_index)
334                 except ValueError, err:
335                     config_error = err
336                     value = None
337
338             if is_default and 'force_store_value' in self_properties:
339                 if isinstance(value, Multi):
340                     item = list(value)
341                 else:
342                     item = value
343                 self.setitem(opt, item, path, is_write=False,
344                              force_permissive=force_permissive)
345         if validate_properties:
346             if config_error is not None:
347                 # should not raise PropertiesOptionError if option is
348                 # mandatory
349                 val_props = undefined
350             else:
351                 val_props = value
352             setting.validate_properties(opt, False, False, value=val_props,
353                                         path=path,
354                                         force_permissive=force_permissive,
355                                         force_properties=force_properties,
356                                         setting_properties=setting_properties,
357                                         self_properties=self_properties,
358                                         index=index)
359         if config_error is not None:
360             raise config_error
361         return value
362
363     def __setitem__(self, opt, value):  # pragma: optional cover
364         raise ConfigError(_('you should only set value with config'))
365
366     def setitem(self, opt, value, path, force_permissive=False,
367                 is_write=True):
368         # is_write is, for example, used with "force_store_value"
369         # user didn't change value, so not write
370         # valid opt
371         context = self._getcontext()
372         setting_properties = context.cfgimpl_get_settings()._getproperties(read_write=False)
373         if 'validator' in setting_properties:
374             fake_context = context._gen_fake_values()
375             fake_context.cfgimpl_get_values()._setitem(opt, value, path,
376                                                        force_permissive,
377                                                        is_write,
378                                                        setting_properties)
379             opt.impl_validate(value, fake_context)
380         self._setitem(opt, value, path, force_permissive, is_write,
381                       setting_properties, validate_properties=False)
382
383     def _setitem(self, opt, value, path, force_permissive, is_write,
384                  setting_properties, validate_properties=True):
385         if opt.impl_is_master_slaves():
386             opt.impl_get_master_slaves().setitem(self, opt, value, path)
387         self._setvalue(opt, path, value, force_permissive=force_permissive,
388                        is_write=is_write,
389                        setting_properties=setting_properties,
390                        validate_properties=validate_properties)
391
392     def _setvalue(self, opt, path, value, force_permissive=False,
393                   is_write=True, validate_properties=True,
394                   setting_properties=undefined, index=None):
395         context = self._getcontext()
396         context.cfgimpl_reset_cache()
397         if validate_properties:
398             setting = context.cfgimpl_get_settings()
399             setting.validate_properties(opt, False, is_write,
400                                         value=value, path=path,
401                                         force_permissive=force_permissive,
402                                         setting_properties=setting_properties)
403         if isinstance(value, Multi):
404             value = list(value)
405             if opt.impl_is_submulti():
406                 for idx, val in enumerate(value):
407                     if isinstance(val, SubMulti):
408                         value[idx] = list(val)
409         owner = context.cfgimpl_get_settings().getowner()
410         if opt.impl_is_master_slaves('slave') and index is None:
411             try:
412                 self._p_.resetvalue(path)
413             except ValueError:
414                 pass
415             for idx, val in enumerate(value):
416                 self._p_.setvalue(path, val, owner, idx)
417         else:
418             self._p_.setvalue(path, value, owner, index)
419
420     def _is_meta(self, opt, path):
421         context = self._getcontext()
422         setting = context.cfgimpl_get_settings()
423         self_properties = setting._getproperties(opt, path, read_write=False)
424         if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties:
425             return False
426         if self._p_.getowner(path, owners.default, only_default=True) is not owners.default:
427             return False
428         if context.cfgimpl_get_meta() is not None:
429             return True
430         return False
431
432     def getowner(self, opt, index=None, force_permissive=False):
433         """
434         retrieves the option's owner
435
436         :param opt: the `option.Option` object
437         :param force_permissive: behaves as if the permissive property
438                                  was present
439         :returns: a `setting.owners.Owner` object
440         """
441         if isinstance(opt, SymLinkOption) and \
442                 not isinstance(opt, DynSymLinkOption):
443             opt = opt._impl_getopt()
444         path = opt.impl_getpath(self._getcontext())
445         return self._getowner(opt, path, index=index, force_permissive=force_permissive)
446
447     def _getowner(self, opt, path, validate_properties=True,
448                   force_permissive=False, validate_meta=undefined,
449                   self_properties=undefined, only_default=False,
450                   index=None):
451         """get owner of an option
452         """
453         if not isinstance(opt, Option) and not isinstance(opt,
454                                                           DynSymLinkOption):
455             raise ConfigError(_('owner only avalaible for an option'))
456         context = self._getcontext()
457         if self_properties is undefined:
458             self_properties = context.cfgimpl_get_settings()._getproperties(
459                 opt, path, read_write=False)
460         if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties:
461             return owners.default
462         if validate_properties:
463             self._get_cached_value(opt, path, True, force_permissive, None, True,
464                                    self_properties=self_properties)
465         owner = self._p_.getowner(path, owners.default, only_default=only_default, index=index)
466         if validate_meta is undefined:
467             if opt.impl_is_master_slaves('slave'):
468                 master = opt.impl_get_master_slaves().getmaster(opt)
469                 masterp = master.impl_getpath(context)
470                 validate_meta = self._is_meta(opt, masterp)
471             else:
472                 validate_meta = True
473         if validate_meta:
474             meta = context.cfgimpl_get_meta()
475             if owner is owners.default and meta is not None:
476                 owner = meta.cfgimpl_get_values()._getowner(opt, path,
477                                                             validate_properties=validate_properties,
478                                                             force_permissive=force_permissive,
479                                                             self_properties=self_properties,
480                                                             only_default=only_default, index=index)
481         return owner
482
483     def setowner(self, opt, owner):
484         """
485         sets a owner to an option
486
487         :param opt: the `option.Option` object
488         :param owner: a valid owner, that is a `setting.owners.Owner` object
489         """
490         if not isinstance(owner, owners.Owner):  # pragma: optional cover
491             raise TypeError(_("invalid generic owner {0}").format(str(owner)))
492
493         path = opt.impl_getpath(self._getcontext())
494         if not self._p_.hasvalue(path):  # pragma: optional cover
495             raise ConfigError(_('no value for {0} cannot change owner to {1}'
496                                 '').format(path, owner))
497         self._getcontext().cfgimpl_get_settings().validate_properties(opt,
498                                                                       False,
499                                                                       True,
500                                                                       path)
501
502         self._p_.setowner(path, owner)
503
504     def is_default_owner(self, opt, validate_properties=True,
505                          validate_meta=True, index=None):
506         """
507         :param config: *must* be only the **parent** config
508                        (not the toplevel config)
509         :return: boolean
510         """
511         path = opt.impl_getpath(self._getcontext())
512         return self._is_default_owner(opt, path,
513                                       validate_properties=validate_properties,
514                                       validate_meta=validate_meta, index=index)
515
516     def _is_default_owner(self, opt, path, validate_properties=True,
517                           validate_meta=True, self_properties=undefined,
518                           index=None):
519         if not opt.impl_is_master_slaves('slave'):
520             index = None
521         d = self._getowner(opt, path, validate_properties,
522                            validate_meta=validate_meta,
523                            self_properties=self_properties, only_default=True,
524                            index=index)
525         return d == owners.default
526
527     def reset_cache(self, only_expired):
528         """
529         clears the cache if necessary
530         """
531         if only_expired:
532             self._p_.reset_expired_cache(int(time()))
533         else:
534             self._p_.reset_all_cache()
535
536     # information
537     def set_information(self, key, value):
538         """updates the information's attribute
539
540         :param key: information's key (ex: "help", "doc"
541         :param value: information's value (ex: "the help string")
542         """
543         self._p_.set_information(key, value)
544
545     def get_information(self, key, default=undefined):
546         """retrieves one information's item
547
548         :param key: the item string (ex: "help")
549         """
550         try:
551             return self._p_.get_information(key)
552         except ValueError:  # pragma: optional cover
553             if default is not undefined:
554                 return default
555             else:
556                 raise ValueError(_("information's item"
557                                    " not found: {0}").format(key))
558
559     def mandatory_warnings(self, force_permissive=False, validate=True):
560         """convenience function to trace Options that are mandatory and
561         where no value has been set
562
563         :param force_permissive: do raise with permissives properties
564         :type force_permissive: `bool`
565         :param validate: validate value when calculating properties
566         :type validate: `bool`
567
568         :returns: generator of mandatory Option's path
569         """
570         context = self._getcontext()
571         settings = context.cfgimpl_get_settings()
572         setting_properties = context.cfgimpl_get_settings()._getproperties(
573             read_write=False)
574
575         def _mandatory_warnings(description, currpath=None):
576             if currpath is None:
577                 currpath = []
578             for opt in description._impl_getchildren(context=context):
579                 name = opt.impl_getname()
580                 path = '.'.join(currpath + [name])
581
582                 if opt.impl_is_optiondescription():
583                     try:
584                         settings.validate_properties(opt, True, False, path=path,
585                                                      force_permissive=force_permissive,
586                                                      setting_properties=setting_properties)
587                     except PropertiesOptionError as err:
588                         pass
589                     else:
590                         for path in _mandatory_warnings(opt, currpath + [name]):
591                             yield path
592                 else:
593                     if isinstance(opt, SymLinkOption) and \
594                             not isinstance(opt, DynSymLinkOption):
595                         true_opt = opt._impl_getopt()
596                         true_path = descr.impl_get_path_by_opt(true_opt)
597                     else:
598                         true_opt = opt
599                         true_path = path
600                     #FIXME attention c'est réutilisé donc jamais complet ??
601                     self_properties = settings._getproperties(true_opt, true_path,
602                                                               read_write=False,
603                                                               setting_properties=setting_properties)
604                     if 'mandatory' in self_properties:
605                         try:
606                             self._get_cached_value(true_opt, path=true_path,
607                                                    force_properties=frozenset(('mandatory',)),
608                                                    force_permissive=force_permissive,
609                                                    setting_properties=setting_properties,
610                                                    self_properties=self_properties,
611                                                    validate=validate)
612                         except PropertiesOptionError as err:
613                             if err.proptype == ['mandatory']:
614                                 yield path
615                         except ConfigError as err:
616                             if validate:
617                                 raise err
618                             else:
619                                 #assume that uncalculated value is an empty value
620                                 yield path
621
622         descr = self._getcontext().cfgimpl_get_description()
623         for path in _mandatory_warnings(descr):
624             yield path
625
626     def force_cache(self):
627         """parse all option to force data in cache
628         """
629         context = self.context()
630         if not 'cache' in context.cfgimpl_get_settings():
631             raise ConfigError(_('can force cache only if cache '
632                                 'is actived in config'))
633         #remove all cached properties and value to update "expired" time
634         context.cfgimpl_reset_cache()
635         for path in context.cfgimpl_get_description().impl_getpaths(
636                 include_groups=True):
637             try:
638                 context.getattr(path)
639             except PropertiesOptionError:
640                 pass
641
642     def __getstate__(self):
643         return {'_p_': self._p_}
644
645     def _impl_setstate(self, storage):
646         self._p_._storage = storage
647
648     def __setstate__(self, states):
649         self._p_ = states['_p_']
650
651
652 # ____________________________________________________________
653 # multi types
654 class Multi(list):
655     """multi options values container
656     that support item notation for the values of multi options"""
657     __slots__ = ('opt', 'path', 'context', '__weakref__')
658
659     def __init__(self, value, context, opt, path):
660         """
661         :param value: the Multi wraps a list value
662         :param context: the home config that has the values
663         :param opt: the option object that have this Multi value
664         :param path: path of the option
665         """
666         if value is None:
667             value = []
668         if not opt.impl_is_submulti() and isinstance(value, Multi):  # pragma: optional cover
669             raise ValueError(_('{0} is already a Multi ').format(
670                 opt.impl_getname()))
671         self.opt = opt
672         self.path = path
673         if not isinstance(context, weakref.ReferenceType):  # pragma: optional cover
674             raise ValueError('context must be a Weakref')
675         self.context = context
676         if not isinstance(value, list):
677             if not '_index' in self.__slots__ and opt.impl_is_submulti():
678                 value = [[value]]
679             else:
680                 value = [value]
681         elif value != [] and not '_index' in self.__slots__ and \
682                 opt.impl_is_submulti() and not isinstance(value[0], list):
683             value = [value]
684         super(Multi, self).__init__(value)
685         if opt.impl_is_submulti():
686             if not '_index' in self.__slots__:
687                 for idx, val in enumerate(self):
688                     if not isinstance(val, SubMulti):
689                         super(Multi, self).__setitem__(idx, SubMulti(val,
690                                                                      context,
691                                                                      opt, path,
692                                                                      idx))
693                     self[idx].submulti = weakref.ref(self)
694
695     def _getcontext(self):
696         """context could be None, we need to test it
697         context is None only if all reference to `Config` object is deleted
698         (for example we delete a `Config` and we manipulate a reference to
699         old `SubConfig`, `Values`, `Multi` or `Settings`)
700         """
701         context = self.context()
702         if context is None:  # pragma: optional cover
703             raise ConfigError(_('the context does not exist anymore'))
704         return context
705
706     def __setitem__(self, index, value):
707         self._setitem(index, value)
708
709     def _setitem(self, index, value, validate=True):
710         context = self._getcontext()
711         setting = context.cfgimpl_get_settings()
712         setting_properties = setting._getproperties(read_write=False)
713         if 'validator' in setting_properties and validate:
714             fake_context = context._gen_fake_values()
715             fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
716                 self.opt, path=self.path, validate=False)
717             fake_multi._setitem(index, value, validate=False)
718             self._validate(value, fake_context, index, True)
719         #assume not checking mandatory property
720         super(Multi, self).__setitem__(index, value)
721         self._store()
722
723     #def __repr__(self, *args, **kwargs):
724     #    return super(Multi, self).__repr__(*args, **kwargs)
725
726     def __getitem__(self, index):
727         value = super(Multi, self).__getitem__(index)
728         if isinstance(value, PropertiesOptionError):
729             raise value
730         return value
731
732     def _get_validated_value(self, index):
733         values = self._getcontext().cfgimpl_get_values()
734         return values._get_validated_value(self.opt, self.path,
735                                            True, False, None, True,
736                                            index=index)
737
738     def append(self, value=undefined, force=False, setitem=True, validate=True):
739         """the list value can be updated (appened)
740         only if the option is a master
741         """
742         if not force and self.opt.impl_is_master_slaves('slave'):  # pragma: optional cover
743             raise SlaveError(_("cannot append a value on a multi option {0}"
744                                " which is a slave").format(self.opt.impl_getname()))
745         index = self.__len__()
746         if value is undefined:
747             value = self._get_validated_value(index)
748         if validate and value not in [None, undefined]:
749             context = self._getcontext()
750             setting = context.cfgimpl_get_settings()
751             setting_properties = setting._getproperties(read_write=False)
752             if 'validator' in setting_properties:
753                 fake_context = context._gen_fake_values()
754                 fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
755                     self.opt, path=self.path, validate=False)
756                 fake_multi.append(value, validate=False, force=True)
757                 self._validate(value, fake_context, index, True)
758         if not '_index' in self.__slots__ and self.opt.impl_is_submulti():
759             if not isinstance(value, SubMulti):
760                 value = SubMulti(value, self.context, self.opt, self.path, index)
761             value.submulti = weakref.ref(self)
762         super(Multi, self).append(value)
763         if setitem:
764             self._store(force=force)
765
766     def append_properties_error(self, err):
767         super(Multi, self).append(err)
768
769     def sort(self, cmp=None, key=None, reverse=False):
770         if self.opt.impl_is_master_slaves():
771             raise SlaveError(_("cannot sort multi option {0} if master or slave"
772                                "").format(self.opt.impl_getname()))
773         if sys.version_info[0] >= 3:
774             if cmp is not None:
775                 raise ValueError(_('cmp is not permitted in python v3 or '
776                                    'greater'))
777             super(Multi, self).sort(key=key, reverse=reverse)
778         else:
779             super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
780         self._store()
781
782     def reverse(self):
783         if self.opt.impl_is_master_slaves():
784             raise SlaveError(_("cannot reverse multi option {0} if master or "
785                                "slave").format(self.opt.impl_getname()))
786         super(Multi, self).reverse()
787         self._store()
788
789     def insert(self, index, value, validate=True):
790         if self.opt.impl_is_master_slaves():
791             raise SlaveError(_("cannot insert multi option {0} if master or "
792                                "slave").format(self.opt.impl_getname()))
793         context = self._getcontext()
794         setting = setting = context.cfgimpl_get_settings()
795         setting_properties = setting._getproperties(read_write=False)
796         if 'validator' in setting_properties and validate and value is not None:
797             fake_context = context._gen_fake_values()
798             fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
799                 self.opt, path=self.path, validate=False)
800             fake_multi.insert(index, value, validate=False)
801             self._validate(value, fake_context, index, True)
802         super(Multi, self).insert(index, value)
803         self._store()
804
805     def extend(self, iterable, validate=True):
806         if self.opt.impl_is_master_slaves():
807             raise SlaveError(_("cannot extend multi option {0} if master or "
808                                "slave").format(self.opt.impl_getname()))
809         try:
810             index = self._index
811         except:
812             index = None
813         context = self._getcontext()
814         setting = context.cfgimpl_get_settings()
815         setting_properties = setting._getproperties(read_write=False)
816         if 'validator' in setting_properties and validate:
817             fake_context = context._gen_fake_values()
818             fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
819                 self.opt, path=self.path, validate=False)
820             fake_multi.extend(iterable, validate=False)
821             self._validate(iterable, fake_context, index)
822         super(Multi, self).extend(iterable)
823         self._store()
824
825     def _validate(self, value, fake_context, force_index, submulti=False):
826         self.opt.impl_validate(value, context=fake_context,
827                                force_index=force_index)
828
829     def pop(self, index, force=False):
830         """the list value can be updated (poped)
831         only if the option is a master
832
833         :param index: remove item a index
834         :type index: int
835         :param force: force pop item (withoud check master/slave)
836         :type force: boolean
837         :returns: item at index
838         """
839         context = self._getcontext()
840         if not force:
841             if self.opt.impl_is_master_slaves('slave'):  # pragma: optional cover
842                 raise SlaveError(_("cannot pop a value on a multi option {0}"
843                                    " which is a slave").format(self.opt.impl_getname()))
844             if self.opt.impl_is_master_slaves('master'):
845                 self.opt.impl_get_master_slaves().pop(self.opt,
846                                                       context.cfgimpl_get_values(), index)
847         #set value without valid properties
848         ret = super(Multi, self).pop(index)
849         self._store(force=force)
850         return ret
851
852     def _store(self, force=False):
853         self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
854                                                           self,
855                                                           validate_properties=not force)
856
857
858 class SubMulti(Multi):
859     __slots__ = ('_index', 'submulti')
860
861     def __init__(self, value, context, opt, path, index):
862         """
863         :param index: index (only for slave with submulti)
864         :type index: `int`
865         """
866         self._index = index
867         super(SubMulti, self).__init__(value, context, opt, path)
868
869     def append(self, value=undefined):
870         super(SubMulti, self).append(value, force=True)
871
872     def pop(self, index):
873         return super(SubMulti, self).pop(index, force=True)
874
875     def __setitem__(self, index, value):
876         self._setitem(index, value)
877
878     def _store(self, force=False):
879         #force is unused here
880         self._getcontext().cfgimpl_get_values()._setvalue(self.opt,
881                                                           self.path,
882                                                           self.submulti())
883
884     def _validate(self, value, fake_context, force_index, submulti=False):
885         if value is not None:
886             if submulti is False:
887                 super(SubMulti, self)._validate(value, fake_context,
888                                                 force_index, submulti)
889             else:
890                 self.opt.impl_validate(value, context=fake_context,
891                                        force_index=self._index,
892                                        force_submulti_index=force_index)
893
894     def _get_validated_value(self, index):
895         values = self._getcontext().cfgimpl_get_values()
896         return values._get_validated_value(self.opt, self.path,
897                                            True, False, None, True,
898                                            index=index,
899                                            submulti_index=self._index)