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