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