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)
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.
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
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 # ____________________________________________________________
21 from tiramisu.error import ConfigError, SlaveError, PropertiesOptionError
22 from tiramisu.setting import owners, expires_time, undefined
23 from tiramisu.autolib import carry_out_calculation
24 from tiramisu.i18n import _
25 from tiramisu.option import SymLinkOption, DynSymLinkOption, Option
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.
33 __slots__ = ('context', '_p_', '__weakref__')
35 def __init__(self, context, storage):
37 Initializes the values's dict.
39 :param context: the context is the home config's values
42 self.context = weakref.ref(context)
43 # the storage type is dictionary or sqlite3
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`)
52 context = self.context()
53 if context is None: # pragma: optional cover
54 raise ConfigError(_('the context does not exist anymore'))
57 def _getvalue(self, opt, path, is_default, index=undefined,
58 with_meta=True, setting_properties=undefined):
59 """actually retrieves the value
61 :param opt: the `option.Option()` object
62 :returns: the option's value (or the default value if not set)
64 if opt.impl_is_optiondescription(): # pragma: optional cover
65 raise ValueError(_('optiondescription has no value'))
66 setting = self._getcontext().cfgimpl_get_settings()
67 force_default = 'frozen' in setting._getitem(opt, path,
68 self_properties=setting_properties) and \
69 'force_default_on_freeze' in setting._getitem(opt, path,
70 self_properties=setting_properties)
71 if not is_default and not force_default:
72 value = self._p_.getvalue(path)
73 if index is not undefined:
77 #value is smaller than expected
78 #so return default 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, config=self._getcontext(),
88 callback_params=callback_params,
91 if isinstance(value, list) and index is not undefined:
92 #if submulti and return a list of list, just return list
93 if opt.impl_is_submulti():
95 if isinstance(val, list):
103 meta = self._getcontext().cfgimpl_get_meta()
105 #FIXME : possible problème de longueur si slave en SymLinkOption
107 value = meta.cfgimpl_get_values(
108 )._get_cached_item(opt, path)
109 if isinstance(value, Multi):
110 if index is not undefined:
115 except PropertiesOptionError:
117 # now try to get default value
118 value = opt.impl_getdefault()
119 if opt.impl_is_multi() and index is not undefined:
121 value = opt.impl_getdefault_multi()
126 value = opt.impl_getdefault_multi()
129 def get_modified_values(self):
130 context = self._getcontext()
131 if context._impl_descr is not None:
132 for opt, path in context.cfgimpl_get_description(
133 )._get_force_store_value():
134 self._getowner(opt, path, force_permissive=True)
135 return self._p_.get_modified_values()
137 def __contains__(self, opt):
139 implements the 'in' keyword syntax in order provide a pythonic way
140 to kow if an option have a value
142 :param opt: the `option.Option()` object
144 path = opt.impl_getpath(self._getcontext())
145 return self._contains(path)
147 def _contains(self, path):
148 return self._p_.hasvalue(path)
150 def __delitem__(self, opt):
151 """overrides the builtins `del()` instructions"""
154 def reset(self, opt, path=None, validate=True):
156 path = opt.impl_getpath(self._getcontext())
157 context = self._getcontext()
159 context.cfgimpl_get_settings().validate_properties(opt, False,
161 if self._contains(path):
163 setting = context.cfgimpl_get_settings()
164 opt.impl_validate(opt.impl_getdefault(),
165 context, 'validator' in setting)
166 context.cfgimpl_reset_cache()
167 if opt.impl_is_master_slaves('master'):
168 opt.impl_get_master_slaves().reset(opt, self)
169 self._p_.resetvalue(path)
171 def _isempty(self, opt, value):
172 "convenience method to know if an option is empty"
174 if value is not undefined:
175 empty_not_multi = not opt.impl_is_multi() and (value is None or
177 empty_multi = opt.impl_is_multi() and (value == [] or
181 empty_multi = empty_not_multi = False
182 return empty_not_multi or empty_multi
184 def __getitem__(self, opt):
185 "enables us to use the pythonic dictionary-like access to values"
186 return self.getitem(opt)
188 def getitem(self, opt, validate=True, force_permissive=False):
191 return self._get_cached_item(opt, validate=validate,
192 force_permissive=force_permissive)
194 def _get_cached_item(self, opt, path=None, validate=True,
195 force_permissive=False, force_properties=None,
196 validate_properties=True,
197 setting_properties=undefined):
199 path = opt.impl_getpath(self._getcontext())
201 if setting_properties is undefined:
202 setting_properties = self._getcontext().cfgimpl_get_settings(
204 if 'cache' in setting_properties and self._p_.hascache(path):
205 if 'expire' in setting_properties:
207 is_cached, value = self._p_.getcache(path, ntime)
209 if opt.impl_is_multi() and not isinstance(value, Multi):
210 #load value so don't need to validate if is not a Multi
211 value = Multi(value, self.context, opt, path)
213 val = self._getitem(opt, path, validate, force_permissive,
214 force_properties, validate_properties,
216 if 'cache' in setting_properties and validate and validate_properties \
217 and force_permissive is False and force_properties is None:
218 if 'expire' in setting_properties:
221 ntime = ntime + expires_time
222 self._p_.setcache(path, val, ntime)
225 def _getitem(self, opt, path, validate, force_permissive, force_properties,
226 validate_properties, setting_properties=undefined):
227 if opt.impl_is_master_slaves():
228 return opt.impl_get_master_slaves().getitem(self, opt, path,
233 setting_properties=setting_properties)
235 return self._get_validated_value(opt, path, validate,
239 setting_properties=setting_properties)
241 def _get_validated_value(self, opt, path, validate, force_permissive,
242 force_properties, validate_properties,
243 index=undefined, submulti_index=undefined,
244 with_meta=True, setting_properties=undefined):
245 """same has getitem but don't touch the cache
246 index is None for slave value, if value returned is not a list, just return []
248 context = self._getcontext()
249 setting = context.cfgimpl_get_settings()
250 is_default = self._is_default_owner(opt, path,
251 validate_properties=False,
253 setting_properties=setting_properties)
259 value = self._getvalue(opt, path, is_default, index=gv_index,
261 setting_properties=setting_properties)
263 except ConfigError as err:
264 # For calculating properties, we need value (ie for mandatory
266 # If value is calculating with a PropertiesOptionError's option
267 # _getvalue raise a ConfigError.
268 # We can not raise ConfigError if this option should raise
269 # PropertiesOptionError too. So we get config_error and raise
270 # ConfigError if properties did not raise.
271 # cannot assign config_err directly in python 3.3
273 # value is not set, for 'undefined' (cannot set None because of
274 # mandatory property)
277 if config_error is None:
278 if index is undefined:
282 if opt.impl_is_multi():
283 #for slave is a multi
284 if index is None and not isinstance(value, list):
286 if force_index is None:
287 value = Multi(value, self.context, opt, path)
288 elif opt.impl_is_submulti() and submulti_index is undefined:
289 value = SubMulti(value, self.context, opt, path,
293 if submulti_index is undefined:
294 force_submulti_index = None
296 force_submulti_index = submulti_index
297 if setting_properties is undefined:
298 setting_properties = setting._getproperties()
299 opt.impl_validate(value, context,
300 'validator' in setting_properties,
301 force_index=force_index,
302 force_submulti_index=force_submulti_index)
303 #FIXME pas de test avec les metas ...
304 #FIXME et les symlinkoption ...
305 if is_default and 'force_store_value' in setting._getitem(opt,
307 self_properties=setting_properties):
308 if isinstance(value, Multi):
312 self.setitem(opt, item, path, is_write=False,
313 force_permissive=force_permissive)
314 if validate_properties:
315 setting.validate_properties(opt, False, False, value=value,
317 force_permissive=force_permissive,
318 force_properties=force_properties,
319 self_properties=setting_properties)
320 if config_error is not None:
324 def __setitem__(self, opt, value): # pragma: optional cover
325 raise ConfigError(_('you should only set value with config'))
327 def setitem(self, opt, value, path, force_permissive=False,
329 # is_write is, for example, used with "force_store_value"
330 # user didn't change value, so not write
332 context = self._getcontext()
333 setting_properties = context.cfgimpl_get_settings()._getproperties()
334 opt.impl_validate(value, context,
335 'validator' in setting_properties)
336 if opt.impl_is_master_slaves():
337 opt.impl_get_master_slaves().setitem(self, opt, value, path)
338 self._setvalue(opt, path, value, force_permissive=force_permissive,
340 setting_properties=setting_properties)
342 def _setvalue(self, opt, path, value, force_permissive=False,
343 is_write=True, validate_properties=True,
344 setting_properties=undefined):
345 context = self._getcontext()
346 context.cfgimpl_reset_cache()
347 if validate_properties:
348 setting = context.cfgimpl_get_settings()
349 setting.validate_properties(opt, False, is_write,
350 value=value, path=path,
351 force_permissive=force_permissive,
352 self_properties=setting_properties)
353 owner = context.cfgimpl_get_settings().getowner()
354 if isinstance(value, Multi):
356 if opt.impl_is_submulti():
357 for idx, val in enumerate(value):
358 if isinstance(val, SubMulti):
359 value[idx] = list(val)
360 self._p_.setvalue(path, value, owner)
362 def _is_meta(self, opt, path):
363 context = self._getcontext()
364 setting = context.cfgimpl_get_settings()
365 settings = setting._getitem(opt, path)
366 if 'frozen' in settings and 'force_default_on_freeze' in settings:
368 if self._p_.getowner(path, owners.default) is not owners.default:
370 if context.cfgimpl_get_meta() is not None:
374 def getowner(self, opt, force_permissive=False):
376 retrieves the option's owner
378 :param opt: the `option.Option` object
379 :param force_permissive: behaves as if the permissive property
381 :returns: a `setting.owners.Owner` object
383 if isinstance(opt, SymLinkOption) and \
384 not isinstance(opt, DynSymLinkOption):
385 opt = opt._impl_getopt()
386 path = opt.impl_getpath(self._getcontext())
387 return self._getowner(opt, path, force_permissive=force_permissive)
389 def _getowner(self, opt, path, validate_properties=True,
390 force_permissive=False, validate_meta=undefined,
391 setting_properties=undefined):
392 if not isinstance(opt, Option) and not isinstance(opt,
394 raise ConfigError(_('owner only avalaible for an option'))
395 context = self._getcontext()
396 setting = context.cfgimpl_get_settings()
397 settings = setting._getitem(opt, path,
398 self_properties=setting_properties)
399 if 'frozen' in settings and 'force_default_on_freeze' in settings:
400 return owners.default
401 if validate_properties:
402 self._getitem(opt, path, True, force_permissive, None, True)
403 owner = self._p_.getowner(path, owners.default)
404 if validate_meta is undefined:
405 if opt.impl_is_master_slaves('slave'):
406 master = opt.impl_get_master_slaves().getmaster(opt)
407 masterp = master.impl_getpath(context)
408 validate_meta = self._is_meta(opt, masterp)
412 meta = context.cfgimpl_get_meta()
413 if owner is owners.default and meta is not None:
414 owner = meta.cfgimpl_get_values()._getowner(opt, path)
417 def setowner(self, opt, owner):
419 sets a owner to an option
421 :param opt: the `option.Option` object
422 :param owner: a valid owner, that is a `setting.owners.Owner` object
424 if not isinstance(owner, owners.Owner): # pragma: optional cover
425 raise TypeError(_("invalid generic owner {0}").format(str(owner)))
427 path = opt.impl_getpath(self._getcontext())
428 self._setowner(opt, path, owner)
430 def _setowner(self, opt, path, owner):
431 if not self._p_.hasvalue(path): # pragma: optional cover
432 raise ConfigError(_('no value for {0} cannot change owner to {1}'
433 '').format(path, owner))
434 self._getcontext().cfgimpl_get_settings().validate_properties(opt,
439 self._p_.setowner(path, owner)
441 def is_default_owner(self, opt, validate_properties=True,
444 :param config: *must* be only the **parent** config
445 (not the toplevel config)
448 path = opt.impl_getpath(self._getcontext())
449 return self._is_default_owner(opt, path,
450 validate_properties=validate_properties,
451 validate_meta=validate_meta)
453 def _is_default_owner(self, opt, path, validate_properties=True,
454 validate_meta=True, setting_properties=undefined):
455 return self._getowner(opt, path, validate_properties,
456 validate_meta=validate_meta,
457 setting_properties=setting_properties) == \
460 def reset_cache(self, only_expired):
462 clears the cache if necessary
465 self._p_.reset_expired_cache(int(time()))
467 self._p_.reset_all_cache()
470 def set_information(self, key, value):
471 """updates the information's attribute
473 :param key: information's key (ex: "help", "doc"
474 :param value: information's value (ex: "the help string")
476 self._p_.set_information(key, value)
478 def get_information(self, key, default=undefined):
479 """retrieves one information's item
481 :param key: the item string (ex: "help")
484 return self._p_.get_information(key)
485 except ValueError: # pragma: optional cover
486 if default is not undefined:
489 raise ValueError(_("information's item"
490 " not found: {0}").format(key))
492 def mandatory_warnings(self, force_permissive=False):
493 """convenience function to trace Options that are mandatory and
494 where no value has been set
496 :returns: generator of mandatory Option's path
499 def _mandatory_warnings(description):
500 #if value in cache, properties are not calculated
502 for opt in description._impl_getchildren(
503 context=self._getcontext()):
504 if opt.impl_is_optiondescription():
505 _ret.extend(_mandatory_warnings(opt))
506 elif isinstance(opt, SymLinkOption) and \
507 not isinstance(opt, DynSymLinkOption):
510 path = opt.impl_getpath(self._getcontext())
512 self._get_cached_item(opt, path=path,
513 force_properties=frozenset(('mandatory',)),
514 force_permissive=force_permissive)
515 except PropertiesOptionError as err:
516 if err.proptype == ['mandatory']:
519 self.reset_cache(False)
520 descr = self._getcontext().cfgimpl_get_description()
521 ret = _mandatory_warnings(descr)
522 self.reset_cache(False)
525 def force_cache(self):
526 """parse all option to force data in cache
528 context = self.context()
529 if not 'cache' in context.cfgimpl_get_settings():
530 raise ConfigError(_('can force cache only if cache '
531 'is actived in config'))
532 #remove all cached properties and value to update "expired" time
533 context.cfgimpl_reset_cache()
534 for path in context.cfgimpl_get_description().impl_getpaths(
535 include_groups=True):
537 context.getattr(path)
538 except PropertiesOptionError:
541 def __getstate__(self):
542 return {'_p_': self._p_}
544 def _impl_setstate(self, storage):
545 self._p_._storage = storage
547 def __setstate__(self, states):
548 self._p_ = states['_p_']
551 # ____________________________________________________________
555 """multi options values container
556 that support item notation for the values of multi options"""
557 __slots__ = ('opt', 'path', 'context', '__weakref__')
559 def __init__(self, value, context, opt, path):
561 :param value: the Multi wraps a list value
562 :param context: the home config that has the values
563 :param opt: the option object that have this Multi value
564 :param path: path of the option
568 if not opt.impl_is_submulti() and isinstance(value, Multi): # pragma: optional cover
569 raise ValueError(_('{0} is already a Multi ').format(
573 if not isinstance(context, weakref.ReferenceType): # pragma: optional cover
574 raise ValueError('context must be a Weakref')
575 self.context = context
576 if not isinstance(value, list):
577 if not '_index' in self.__slots__ and opt.impl_is_submulti():
581 elif value != [] and not '_index' in self.__slots__ and \
582 opt.impl_is_submulti() and not isinstance(value[0], list):
584 super(Multi, self).__init__(value)
585 if opt.impl_is_submulti():
586 if not '_index' in self.__slots__:
587 for idx, val in enumerate(self):
588 if not isinstance(val, SubMulti):
589 super(Multi, self).__setitem__(idx, SubMulti(val,
594 self[idx].submulti = weakref.ref(self)
596 def _getcontext(self):
597 """context could be None, we need to test it
598 context is None only if all reference to `Config` object is deleted
599 (for example we delete a `Config` and we manipulate a reference to
600 old `SubConfig`, `Values`, `Multi` or `Settings`)
602 context = self.context()
603 if context is None: # pragma: optional cover
604 raise ConfigError(_('the context does not exist anymore'))
607 def __setitem__(self, index, value):
608 self._setitem(index, value)
610 def _setitem(self, index, value):
611 self._validate(value, index, True)
612 #assume not checking mandatory property
613 super(Multi, self).__setitem__(index, value)
614 self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
617 #def __repr__(self, *args, **kwargs):
618 # return super(Multi, self).__repr__(*args, **kwargs)
620 #def __getitem__(self, y):
621 # return super(Multi, self).__getitem__(y)
623 def _get_validated_value(self, index):
624 values = self._getcontext().cfgimpl_get_values()
625 return values._get_validated_value(self.opt, self.path,
626 True, False, None, True,
629 def append(self, value=undefined, force=False, setitem=True):
630 """the list value can be updated (appened)
631 only if the option is a master
633 if not force and self.opt.impl_is_master_slaves('slave'): # pragma: optional cover
634 raise SlaveError(_("cannot append a value on a multi option {0}"
635 " which is a slave").format(self.opt.impl_getname()))
636 index = self.__len__()
637 if value is undefined:
639 value = self._get_validated_value(index)
642 self._validate(value, index, True)
643 if not '_index' in self.__slots__ and self.opt.impl_is_submulti():
644 if not isinstance(value, SubMulti):
645 value = SubMulti(value, self.context, self.opt, self.path, index)
646 value.submulti = weakref.ref(self)
647 super(Multi, self).append(value)
651 def sort(self, cmp=None, key=None, reverse=False):
652 if self.opt.impl_is_master_slaves():
653 raise SlaveError(_("cannot sort multi option {0} if master or slave"
654 "").format(self.opt.impl_getname()))
655 if sys.version_info[0] >= 3:
657 raise ValueError(_('cmp is not permitted in python v3 or '
659 super(Multi, self).sort(key=key, reverse=reverse)
661 super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
665 if self.opt.impl_is_master_slaves():
666 raise SlaveError(_("cannot reverse multi option {0} if master or "
667 "slave").format(self.opt.impl_getname()))
668 super(Multi, self).reverse()
671 def insert(self, index, obj):
672 #FIXME obj should be undefined
673 if self.opt.impl_is_master_slaves():
674 raise SlaveError(_("cannot insert multi option {0} if master or "
675 "slave").format(self.opt.impl_getname()))
676 self._validate(obj, index, True)
677 super(Multi, self).insert(index, obj)
680 def extend(self, iterable):
681 if self.opt.impl_is_master_slaves():
682 raise SlaveError(_("cannot extend multi option {0} if master or "
683 "slave").format(self.opt.impl_getname()))
688 self._validate(iterable, index)
689 super(Multi, self).extend(iterable)
692 def _validate(self, value, force_index, submulti=False):
693 if value is not None:
694 self.opt.impl_validate(value, context=self._getcontext(),
695 force_index=force_index)
697 def pop(self, index, force=False):
698 """the list value can be updated (poped)
699 only if the option is a master
701 :param index: remove item a index
703 :param force: force pop item (withoud check master/slave)
705 :returns: item at index
707 context = self._getcontext()
709 if self.opt.impl_is_master_slaves('slave'): # pragma: optional cover
710 raise SlaveError(_("cannot pop a value on a multi option {0}"
711 " which is a slave").format(self.opt.impl_getname()))
712 if self.opt.impl_is_master_slaves('master'):
713 self.opt.impl_get_master_slaves().pop(self.opt,
714 context.cfgimpl_get_values(), index)
715 #set value without valid properties
716 ret = super(Multi, self).pop(index)
720 def _store(self, force=False):
721 self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path,
723 validate_properties=not force)
726 class SubMulti(Multi):
727 __slots__ = ('_index', 'submulti')
729 def __init__(self, value, context, opt, path, index):
731 :param index: index (only for slave with submulti)
735 super(SubMulti, self).__init__(value, context, opt, path)
737 def append(self, value=undefined):
738 super(SubMulti, self).append(value, force=True)
740 def pop(self, index):
741 return super(SubMulti, self).pop(index, force=True)
743 def __setitem__(self, index, value):
744 self._setitem(index, value)
746 def _store(self, force=False):
747 #force is unused here
748 self._getcontext().cfgimpl_get_values()._setvalue(self.opt,
752 def _validate(self, value, force_index, submulti=False):
753 if value is not None:
754 if submulti is False:
755 super(SubMulti, self)._validate(value, force_index)
757 self.opt.impl_validate(value, context=self._getcontext(),
758 force_index=self._index,
759 force_submulti_index=force_index)
761 def _get_validated_value(self, index):
762 values = self._getcontext().cfgimpl_get_values()
763 return values._get_validated_value(self.opt, self.path,
764 True, False, None, True,
766 submulti_index=self._index)