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