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