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