tiramisu/tiramisu/setting.py

792 lines
30 KiB
Python
Raw Normal View History

2012-11-19 10:45:03 +01:00
# -*- coding: utf-8 -*-
"sets the options of the configuration objects Config object itself"
2018-01-26 07:33:47 +01:00
# Copyright (C) 2012-2018 Team tiramisu (see AUTHORS for all contributors)
2012-11-19 10:45:03 +01:00
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
2012-11-19 10:45:03 +01:00
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
2012-11-19 10:45:03 +01:00
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-11-19 10:45:03 +01:00
# ____________________________________________________________
from copy import copy
2014-04-03 22:15:41 +02:00
from logging import getLogger
import weakref
from .error import (RequirementError, PropertiesOptionError,
2016-09-14 20:17:25 +02:00
ConstError, ConfigError, display_list)
from .i18n import _
2013-09-07 21:47:17 +02:00
"Default encoding for display a Config if raise UnicodeEncodeError"
default_encoding = 'utf-8'
2013-09-07 21:47:17 +02:00
"""If cache and expire is enable, time before cache is expired.
This delay start first time value/setting is set in cache, even if
user access several time to value/setting
"""
expires_time = 5
2013-09-07 21:47:17 +02:00
"""List of default properties (you can add new one if needed).
For common properties and personalise properties, if a propery is set for
an Option and for the Config together, Setting raise a PropertiesOptionError
* Common properties:
hidden
option with this property can only get value in read only mode. This
option is not available in read write mode.
disabled
option with this property cannot be set/get
frozen
cannot set value for option with this properties if 'frozen' is set in
config
mandatory
should set value for option with this properties if 'mandatory' is set in
config
* Special property:
permissive
option with 'permissive' cannot raise PropertiesOptionError for properties
set in permissive
config with 'permissive', whole option in this config cannot raise
PropertiesOptionError for properties set in permissive
* Special Config properties:
cache
if set, enable cache settings and values
expire
if set, settings and values in cache expire after ``expires_time``
everything_frozen
whole option in config are frozen (even if option have not frozen
property)
empty
raise mandatory PropertiesOptionError if multi or master have empty value
2013-09-07 21:47:17 +02:00
validator
launch validator set by user in option (this property has no effect
for internal validator)
2015-04-18 23:11:57 +02:00
warnings
display warnings during validation
2013-09-07 21:47:17 +02:00
"""
2017-12-23 20:33:36 +01:00
default_properties = ('cache', 'validator', 'warnings')
2013-09-07 21:47:17 +02:00
"""Config can be in two defaut mode:
read_only
you can get all variables not disabled but you cannot set any variables
if a value has a callback without any value, callback is launch and value
of this variable can change
you cannot access to mandatory variable without values
read_write
you can get all variables not disabled and not hidden
you can set all variables not frozen
"""
2013-09-06 09:05:19 +02:00
ro_append = set(['frozen', 'disabled', 'validator', 'everything_frozen',
'mandatory', 'empty'])
2013-09-07 21:47:17 +02:00
ro_remove = set(['permissive', 'hidden'])
2013-09-06 09:05:19 +02:00
rw_append = set(['frozen', 'disabled', 'validator', 'hidden'])
rw_remove = set(['permissive', 'everything_frozen', 'mandatory', 'empty'])
2017-12-23 20:21:07 +01:00
FORBIDDEN_SET_PROPERTIES = frozenset(['force_store_value'])
2018-09-15 22:44:49 +02:00
FORBIDDEN_SET_PERMISSIVES = frozenset(['force_default_on_freeze', 'force_default_on_freeze'])
2014-07-06 15:31:57 +02:00
2014-04-03 22:15:41 +02:00
log = getLogger('tiramisu')
#FIXME
#import logging
#logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
2017-07-22 16:26:06 +02:00
static_set = frozenset()
2014-04-03 22:15:41 +02:00
2018-08-01 08:37:58 +02:00
class OptionBag:
__slots__ = ('option', # current option
'path',
'index',
'config_bag',
'ori_option', # original option (for example useful for symlinkoption)
'properties', # properties of current option
'apply_requires', # apply requires or not for this option
2018-08-01 08:37:58 +02:00
'fromconsistency' # history for consistency
)
def __init__(self):
self.option = None
self.fromconsistency = []
def set_option(self,
option,
path,
index,
config_bag):
if path is None:
2018-09-06 23:16:17 +02:00
path = option.impl_getpath()
2018-08-01 08:37:58 +02:00
self.path = path
self.index = index
self.option = option
self.config_bag = config_bag
def __getattr__(self, key):
if key == 'properties':
2018-08-02 22:35:40 +02:00
settings = self.config_bag.context.cfgimpl_get_settings()
self.properties = settings.getproperties(self,
apply_requires=self.apply_requires)
2018-08-01 08:37:58 +02:00
return self.properties
elif key == 'ori_option':
return self.option
elif key == 'apply_requires':
return True
2018-09-15 22:44:49 +02:00
raise KeyError('unknown key {} for OptionBag'.format(key)) # pragma: no cover
2018-08-01 08:37:58 +02:00
2018-09-04 08:36:02 +02:00
def __delattr__(self, key):
2018-10-31 16:08:22 +01:00
if key in ['properties', 'permissives']:
2018-09-04 08:36:02 +02:00
return
2018-09-15 22:44:49 +02:00
raise KeyError('unknown key {} for ConfigBag'.format(key)) # pragma: no cover
2018-09-04 08:36:02 +02:00
2018-08-18 10:03:08 +02:00
def copy(self):
kwargs = {}
option_bag = OptionBag()
for key in self.__slots__:
if key == 'properties' and self.config_bag is undefined:
continue
2018-08-18 10:03:08 +02:00
setattr(option_bag, key, getattr(self, key))
return option_bag
2018-08-01 08:37:58 +02:00
class ConfigBag:
2018-08-18 07:51:04 +02:00
__slots__ = ('context', # link to the current context
'properties', # properties for current context
2018-08-18 08:06:29 +02:00
'permissives', # permissives for current context
2017-12-19 23:11:45 +01:00
)
2018-08-02 22:35:40 +02:00
def __init__(self, context, **kwargs):
self.context = context
2017-12-19 23:11:45 +01:00
for key, value in kwargs.items():
2018-08-01 08:37:58 +02:00
setattr(self, key, value)
2017-12-19 23:11:45 +01:00
def __getattr__(self, key):
2018-08-18 07:51:04 +02:00
if key == 'properties':
self.properties = self.context.cfgimpl_get_settings().get_context_properties()
return self.properties
2018-08-18 08:06:29 +02:00
if key == 'permissives':
self.permissives = self.context.cfgimpl_get_settings().get_context_permissives()
return self.permissives
2018-09-15 22:44:49 +02:00
raise KeyError('unknown key {} for ConfigBag'.format(key)) # pragma: no cover
2018-08-17 23:11:25 +02:00
def remove_warnings(self):
self.properties = frozenset(self.properties - {'warnings'})
2018-08-17 23:11:25 +02:00
def remove_validation(self):
2018-08-18 10:03:08 +02:00
self.properties = frozenset(self.properties - {'validator'})
2018-08-18 08:06:29 +02:00
def set_permissive(self):
self.properties = frozenset(self.properties | {'permissive'})
def __delattr__(self, key):
if key in ['properties', 'permissives']:
try:
super().__delattr__(key)
except AttributeError:
pass
return
2018-09-15 22:44:49 +02:00
raise KeyError('unknown key {} for ConfigBag'.format(key)) # pragma: no cover
#def __setattr__(self, key, value):
# super().__setattr__(key, value)
2018-08-01 08:37:58 +02:00
def copy(self):
2017-12-19 23:11:45 +01:00
kwargs = {}
for key in self.__slots__:
if key in ['properties', 'permissives'] and \
not hasattr(self.context, '_impl_settings'):
continue
2018-08-17 23:11:25 +02:00
kwargs[key] = getattr(self, key)
2017-12-19 23:11:45 +01:00
return ConfigBag(**kwargs)
2013-09-07 21:47:17 +02:00
# ____________________________________________________________
2013-09-22 20:57:52 +02:00
class _NameSpace(object):
2012-12-06 18:14:57 +01:00
"""convenient class that emulates a module
2013-09-07 21:47:17 +02:00
and builds constants (that is, unique names)
when attribute is added, we cannot delete it
"""
2012-12-06 18:14:57 +01:00
2017-11-20 17:01:36 +01:00
def __setattr__(self,
name,
value):
if name in self.__dict__:
raise ConstError(_("can't rebind {0}").format(name))
2012-12-06 18:14:57 +01:00
self.__dict__[name] = value
2017-11-20 17:01:36 +01:00
def __delattr__(self,
name):
raise ConstError(_("can't unbind {0}").format(name))
2013-04-03 12:20:26 +02:00
class GroupModule(_NameSpace):
2012-12-10 09:53:13 +01:00
"emulates a module to manage unique group (OptionDescription) names"
2012-12-10 14:38:25 +01:00
class GroupType(str):
2012-12-06 18:14:57 +01:00
"""allowed normal group (OptionDescription) names
*normal* means : groups that are not master
"""
pass
2013-04-03 12:20:26 +02:00
2012-12-10 14:38:25 +01:00
class DefaultGroupType(GroupType):
2012-12-10 14:10:05 +01:00
"""groups that are default (typically 'default')"""
pass
2012-12-10 14:38:25 +01:00
class MasterGroupType(GroupType):
2012-12-06 18:14:57 +01:00
"""allowed normal group (OptionDescription) names
*master* means : groups that have the 'master' attribute set
"""
pass
2013-04-03 12:20:26 +02:00
class OwnerModule(_NameSpace):
2012-12-10 14:10:05 +01:00
"""emulates a module to manage unique owner names.
owners are living in `Config._cfgimpl_value_owners`
"""
class Owner(str):
"""allowed owner names
"""
pass
2013-04-03 12:20:26 +02:00
2012-12-10 14:10:05 +01:00
class DefaultOwner(Owner):
"""groups that are default (typically 'default')"""
pass
2013-09-07 21:47:17 +02:00
2017-11-20 17:01:36 +01:00
def addowner(self, name):
"""
:param name: the name of the new owner
"""
setattr(owners, name, owners.Owner(name))
2013-09-07 21:47:17 +02:00
# ____________________________________________________________
2017-11-20 17:01:36 +01:00
# populate groups
groups = GroupModule()
"""groups.default
default group set when creating a new optiondescription"""
groups.default = groups.DefaultGroupType('default')
2013-09-07 21:47:17 +02:00
2017-11-20 17:01:36 +01:00
"""groups.master
2013-09-07 21:47:17 +02:00
master group is a special optiondescription, all suboptions should be
multi option and all values should have same length, to find master's
option, the optiondescription's name should be same than de master's
2017-11-20 17:01:36 +01:00
option"""
groups.master = groups.MasterGroupType('master')
2013-09-07 21:47:17 +02:00
2017-11-20 17:01:36 +01:00
""" groups.family
example of group, no special behavior with this group's type"""
groups.family = groups.GroupType('family')
2013-04-03 12:20:26 +02:00
2012-12-10 14:38:25 +01:00
2013-09-07 21:47:17 +02:00
# ____________________________________________________________
2017-11-20 17:01:36 +01:00
# populate owners with default attributes
2013-09-07 21:47:17 +02:00
owners = OwnerModule()
2017-11-20 17:01:36 +01:00
"""default
is the config owner after init time"""
owners.default = owners.DefaultOwner('default')
"""user
is the generic is the generic owner"""
owners.user = owners.Owner('user')
"""forced
special owner when value is forced"""
owners.forced = owners.Owner('forced')
2017-11-23 16:56:14 +01:00
forbidden_owners = (owners.default, owners.forced)
2013-02-21 17:07:00 +01:00
2013-04-03 12:20:26 +02:00
# ____________________________________________________________
2014-04-25 22:57:08 +02:00
class Undefined(object):
2018-04-07 20:15:19 +02:00
def __str__(self):
return 'Undefined'
__repr__ = __str__
undefined = Undefined()
2018-10-30 11:57:04 +01:00
# ____________________________________________________________
2013-08-20 09:47:12 +02:00
class Settings(object):
2014-01-06 15:32:28 +01:00
"``config.Config()``'s configuration options settings"
__slots__ = ('_p_',
'_pp_',
'__weakref__')
2013-04-03 12:20:26 +02:00
2018-09-07 06:14:52 +02:00
def __init__(self,
properties,
permissives):
2013-08-21 17:21:09 +02:00
"""
2013-08-21 18:34:32 +02:00
initializer
2013-08-21 17:21:09 +02:00
:param context: the root config
2013-08-21 18:34:32 +02:00
:param storage: the storage type
2013-08-21 23:21:28 +02:00
- dictionary -> in memory
2013-08-21 17:21:09 +02:00
- sqlite3 -> persistent
"""
2013-04-03 12:20:26 +02:00
# generic owner
2017-07-13 22:04:06 +02:00
self._p_ = properties
self._pp_ = permissives
2013-08-20 09:47:12 +02:00
2018-10-30 11:57:04 +01:00
# ____________________________________________________________
2017-11-20 17:01:36 +01:00
# get properties and permissive methods
2013-03-14 11:31:44 +01:00
def get_context_properties(self):
2018-06-25 21:40:16 +02:00
is_cached, props = self._p_.getcache(None,
None,
None,
2018-08-18 07:51:04 +02:00
{},
2018-08-18 08:35:30 +02:00
{},
2018-06-25 21:40:16 +02:00
'context_props')
if not is_cached:
2018-09-05 22:46:45 +02:00
props = self._p_.getproperties(None,
default_properties)
2018-06-25 21:40:16 +02:00
self._p_.setcache(None,
None,
props,
2018-08-18 08:35:30 +02:00
{},
props)
2015-10-29 09:03:13 +01:00
return props
2013-03-14 11:31:44 +01:00
2017-11-20 17:01:36 +01:00
def getproperties(self,
2018-08-01 08:37:58 +02:00
option_bag,
2018-01-26 07:33:47 +01:00
apply_requires=True):
2017-10-22 09:48:08 +02:00
"""
2013-04-03 12:20:26 +02:00
"""
2018-08-01 08:37:58 +02:00
opt = option_bag.option
config_bag = option_bag.config_bag
path = option_bag.path
index = option_bag.index
2017-12-04 20:05:36 +01:00
if opt.impl_is_symlinkoption():
opt = opt.impl_getopt()
2018-09-06 23:16:17 +02:00
path = opt.impl_getpath()
2018-06-25 21:40:16 +02:00
if apply_requires:
2018-08-18 07:51:04 +02:00
props = config_bag.properties
2018-06-25 21:40:16 +02:00
is_cached, props = self._p_.getcache(path,
expires_time,
index,
props,
2018-08-18 08:35:30 +02:00
{},
2018-06-25 21:40:16 +02:00
'self_props')
else:
is_cached = False
2017-11-20 17:01:36 +01:00
if not is_cached:
2018-09-05 22:46:45 +02:00
props = self._p_.getproperties(path,
opt.impl_getproperties())
2018-01-26 07:33:47 +01:00
if apply_requires:
2018-08-01 08:37:58 +02:00
props |= self.apply_requires(option_bag,
False)
2018-08-18 08:06:29 +02:00
props -= self.getpermissives(opt,
path)
2018-06-25 21:40:16 +02:00
if apply_requires:
2017-11-20 17:01:36 +01:00
self._p_.setcache(path,
2018-06-25 21:40:16 +02:00
index,
2017-11-20 17:01:36 +01:00
props,
2018-08-18 08:35:30 +02:00
props,
2018-08-18 07:51:04 +02:00
config_bag.properties)
2017-11-20 17:01:36 +01:00
return props
2018-08-18 08:06:29 +02:00
def get_context_permissives(self):
return self.getpermissives(None, None)
2012-11-19 10:45:03 +01:00
2018-08-18 08:06:29 +02:00
def getpermissives(self,
opt,
path):
2017-12-04 20:05:36 +01:00
if opt and opt.impl_is_symlinkoption():
opt = opt.impl_getopt()
2018-09-06 23:16:17 +02:00
path = opt.impl_getpath()
2018-08-18 08:06:29 +02:00
return self._pp_.getpermissives(path)
2017-11-20 17:01:36 +01:00
def apply_requires(self,
2018-08-01 08:37:58 +02:00
option_bag,
readable):
"""carries out the jit (just in time) requirements between options
a requirement is a tuple of this form that comes from the option's
requirements validation::
(option, expected, action, inverse, transitive, same_action)
let's have a look at all the tuple's items:
2013-09-07 22:16:50 +02:00
- **option** is the target option's
2013-09-07 22:16:50 +02:00
- **expected** is the target option's value that is going to trigger
an action
2013-09-07 22:16:50 +02:00
- **action** is the (property) action to be accomplished if the target
option happens to have the expected value
- if **inverse** is `True` and if the target option's value does not
apply, then the property action must be removed from the option's
properties list (wich means that the property is inverted)
- **transitive**: but what happens if the target option cannot be
accessed ? We don't kown the target option's value. Actually if some
property in the target option is not present in the permissive, the
target option's value cannot be accessed. In this case, the
**action** have to be applied to the option. (the **action** property
is then added to the option).
- **same_action**: actually, if **same_action** is `True`, the
transitivity is not accomplished. The transitivity is accomplished
only if the target option **has the same property** that the demanded
action. If the target option's value is not accessible because of
another reason, because of a property of another type, then an
exception :exc:`~error.RequirementError` is raised.
And at last, if no target option matches the expected values, the
action will not add to the option's properties list.
:param opt: the option on wich the requirement occurs
:type opt: `option.Option()`
:param path: the option's path in the config
:type path: str
"""
2018-08-01 08:37:58 +02:00
current_requires = option_bag.option.impl_getrequires()
# filters the callbacks
if readable:
2016-09-14 20:17:25 +02:00
calc_properties = {}
else:
calc_properties = set()
2016-10-23 09:38:35 +02:00
if not current_requires:
return calc_properties
2018-09-07 06:14:52 +02:00
context = option_bag.config_bag.context
all_properties = None
2016-10-23 09:38:35 +02:00
for requires in current_requires:
for require in requires:
exps, action, inverse, transitive, same_action, operator = require
2017-05-20 16:28:19 +02:00
breaked = False
for option, expected in exps:
if option.issubdyn():
option = option.to_dynoption(option_bag.option._rootpath,
option_bag.option.impl_getsuffix())
2018-09-06 23:16:17 +02:00
reqpath = option.impl_getpath()
#FIXME too later!
2018-08-01 08:37:58 +02:00
if reqpath.startswith(option_bag.path + '.'):
2017-05-20 16:28:19 +02:00
raise RequirementError(_("malformed requirements "
"imbrication detected for option:"
" '{0}' with requirement on: "
2018-08-01 08:37:58 +02:00
"'{1}'").format(option_bag.path, reqpath))
idx = None
is_indexed = False
if option.impl_is_master_slaves('slave'):
2018-08-01 08:37:58 +02:00
idx = option_bag.index
2018-08-19 15:19:42 +02:00
elif option.impl_is_multi() and option_bag.index is not None:
2017-12-29 11:38:41 +01:00
is_indexed = True
2018-08-02 22:35:40 +02:00
config_bag = option_bag.config_bag.copy()
2018-08-18 08:06:29 +02:00
config_bag.set_permissive()
2018-08-01 08:37:58 +02:00
soption_bag = OptionBag()
soption_bag.set_option(option,
reqpath,
idx,
config_bag)
if option_bag.option == option:
2018-08-18 07:51:04 +02:00
soption_bag.config_bag.properties = frozenset()
2018-08-17 23:11:25 +02:00
soption_bag.config_bag.remove_validation()
2018-08-01 08:37:58 +02:00
soption_bag.apply_requires = False
2017-11-13 22:45:53 +01:00
try:
value = context.getattr(reqpath,
2018-08-01 08:37:58 +02:00
soption_bag)
2018-08-19 15:19:42 +02:00
if is_indexed:
2018-08-01 08:37:58 +02:00
value = value[option_bag.index]
2017-11-13 22:45:53 +01:00
except PropertiesOptionError as err:
properties = err.proptype
2017-11-13 22:45:53 +01:00
if not transitive:
if all_properties is None:
all_properties = []
for requires_ in current_requires:
2017-11-20 17:01:36 +01:00
for require_ in requires_:
all_properties.append(require_[1])
if not set(properties) - set(all_properties):
2017-11-13 22:45:53 +01:00
continue
if same_action and action not in properties:
2017-11-13 22:45:53 +01:00
if len(properties) == 1:
prop_msg = _('property')
else:
prop_msg = _('properties')
raise RequirementError(_('cannot access to option "{0}" because '
'required option "{1}" has {2} {3}'
2018-08-01 08:37:58 +02:00
'').format(option_bag.option.impl_get_display_name(),
2017-11-13 22:45:53 +01:00
option.impl_get_display_name(),
prop_msg,
2018-04-11 08:40:59 +02:00
display_list(list(properties), add_quote=True)))
# transitive action, add action
2017-05-20 16:28:19 +02:00
if operator != 'and':
if readable:
2018-08-01 08:37:58 +02:00
for msg in self.apply_requires(err._option_bag,
True).values():
calc_properties.setdefault(action, []).extend(msg)
else:
calc_properties.add(action)
breaked = True
break
else:
if (not inverse and value in expected or
inverse and value not in expected):
if operator != 'and':
if readable:
2017-05-20 16:28:19 +02:00
if not inverse:
2017-12-23 20:21:07 +01:00
msg = _('the value of "{0}" is {1}')
2017-05-20 16:28:19 +02:00
else:
2017-12-23 20:21:07 +01:00
msg = _('the value of "{0}" is not {1}')
2017-05-20 16:28:19 +02:00
calc_properties.setdefault(action, []).append(
msg.format(option.impl_get_display_name(),
2017-12-23 20:21:07 +01:00
display_list(expected, 'or', add_quote=True)))
else:
calc_properties.add(action)
breaked = True
break
elif operator == 'and':
break
2017-05-20 16:28:19 +02:00
else:
if operator == 'and':
calc_properties.add(action)
2018-09-29 21:58:41 +02:00
continue
2017-05-20 16:28:19 +02:00
if breaked:
break
return calc_properties
2013-08-19 11:01:21 +02:00
2017-11-20 17:01:36 +01:00
#____________________________________________________________
# set methods
2017-12-19 23:11:45 +01:00
def set_context_properties(self,
2018-09-07 06:14:52 +02:00
properties,
context):
2017-12-19 23:11:45 +01:00
self.setproperties(None,
properties,
2018-09-07 06:14:52 +02:00
None,
context)
2017-11-20 17:01:36 +01:00
def setproperties(self,
path,
2017-12-19 23:11:45 +01:00
properties,
2018-09-07 06:14:52 +02:00
option_bag,
context):
2017-11-20 17:01:36 +01:00
"""save properties for specified path
(never save properties if same has option properties)
"""
2018-06-25 21:40:16 +02:00
# should have index !!!
2018-08-01 08:37:58 +02:00
if path is not None and option_bag.option.impl_getrequires() is not None:
not_allowed_props = properties & \
getattr(option_bag.option, '_calc_properties', static_set)
2017-12-29 11:38:41 +01:00
if not_allowed_props:
2018-08-01 08:37:58 +02:00
raise ValueError(_('cannot set property {} for option "{}" this property is '
'calculated').format(display_list(list(not_allowed_props),
add_quote=True),
option_bag.option.impl_get_display_name()))
if option_bag is None:
2017-12-19 23:11:45 +01:00
opt = None
else:
2018-08-01 08:37:58 +02:00
opt = option_bag.option
2017-12-04 20:05:36 +01:00
if opt and opt.impl_is_symlinkoption():
2018-04-11 18:32:13 +02:00
raise TypeError(_("can't assign property to the symlinkoption \"{}\""
2017-12-04 20:05:36 +01:00
"").format(opt.impl_get_display_name()))
if 'force_default_on_freeze' in properties and \
'frozen' not in properties and \
opt.impl_is_master_slaves('master'):
raise ConfigError(_('a master ({0}) cannot have '
'"force_default_on_freeze" property without "frozen"'
'').format(opt.impl_get_display_name()))
2017-11-20 17:01:36 +01:00
self._p_.setproperties(path,
properties)
#values too because of slave values could have a PropertiesOptionError has value
2018-09-07 06:14:52 +02:00
context.cfgimpl_reset_cache(option_bag)
2018-08-01 08:37:58 +02:00
if option_bag is not None:
2018-09-15 22:44:49 +02:00
del option_bag.properties
2017-11-20 17:01:36 +01:00
2018-08-18 08:14:47 +02:00
def set_context_permissives(self,
permissives):
self.setpermissives(None,
permissives)
2017-11-20 17:01:36 +01:00
2018-08-18 08:14:47 +02:00
def setpermissives(self,
option_bag,
permissives):
2017-11-20 17:01:36 +01:00
"""
enables us to put the permissives in the storage
:param path: the option's path
:param type: str
:param opt: if an option object is set, the path is extracted.
it is better (faster) to set the path parameter
instead of passing a :class:`tiramisu.option.Option()` object.
"""
if not isinstance(permissives, frozenset):
raise TypeError(_('permissive must be a frozenset'))
2018-08-01 08:37:58 +02:00
if option_bag is not None:
opt = option_bag.option
if opt and opt.impl_is_symlinkoption():
raise TypeError(_("can't assign permissive to the symlinkoption \"{}\""
"").format(opt.impl_get_display_name()))
path = option_bag.path
else:
path = None
2017-12-28 11:47:29 +01:00
forbidden_permissives = FORBIDDEN_SET_PERMISSIVES & permissives
2017-11-20 17:01:36 +01:00
if forbidden_permissives:
raise ConfigError(_('cannot add those permissives: {0}').format(
' '.join(forbidden_permissives)))
2018-08-18 08:14:47 +02:00
self._pp_.setpermissives(path, permissives)
2018-08-01 08:37:58 +02:00
if option_bag is not None:
2018-09-07 06:14:52 +02:00
option_bag.config_bag.context.cfgimpl_reset_cache(option_bag)
2017-11-20 17:01:36 +01:00
#____________________________________________________________
# reset methods
2017-11-23 16:56:14 +01:00
def reset(self,
2018-09-07 06:14:52 +02:00
option_bag,
context):
2018-08-01 08:37:58 +02:00
if option_bag is None:
opt = None
path = None
2018-08-01 08:37:58 +02:00
else:
opt = option_bag.option
assert not opt.impl_is_symlinkoption(), _("can't reset properties to "
"the symlinkoption \"{}\""
"").format(opt.impl_get_display_name())
path = option_bag.path
self._p_.delproperties(path)
context.cfgimpl_reset_cache(option_bag)
def reset_permissives(self,
option_bag,
context):
if option_bag is None:
opt = None
path = None
2017-11-20 17:01:36 +01:00
else:
opt = option_bag.option
assert not opt.impl_is_symlinkoption(), _("can't reset permissives to "
"the symlinkoption \"{}\""
"").format(opt.impl_get_display_name())
path = option_bag.path
self._pp_.delpermissive(path)
2018-09-07 06:14:52 +02:00
context.cfgimpl_reset_cache(option_bag)
2017-11-20 17:01:36 +01:00
#____________________________________________________________
# validate properties
def calc_raises_properties(self,
option_properties,
config_properties,
config_permissives):
2018-10-30 11:57:04 +01:00
properties = option_properties & config_properties - {'frozen',
'mandatory',
'empty'}
# remove global permissive properties
if properties and ('permissive' in config_properties):
properties -= config_permissives
# at this point an option should not remain in properties
return properties
2017-11-20 17:01:36 +01:00
def validate_properties(self,
2018-08-01 08:37:58 +02:00
option_bag):
2017-11-20 17:01:36 +01:00
"""
2017-12-05 21:49:19 +01:00
validation upon the properties related to `opt`
2017-11-20 17:01:36 +01:00
2017-12-05 21:49:19 +01:00
:param opt: an option or an option description object
2017-11-20 17:01:36 +01:00
:param force_permissive: behaves as if the permissive property
was present
"""
2018-08-01 08:37:58 +02:00
config_bag = option_bag.config_bag
2018-08-18 07:51:04 +02:00
if not config_bag.properties:
return
properties = self.calc_raises_properties(option_bag.properties,
2018-10-30 11:57:04 +01:00
config_bag.properties,
config_bag.permissives)
2017-11-20 17:01:36 +01:00
if properties != frozenset():
2018-08-01 08:37:58 +02:00
raise PropertiesOptionError(option_bag,
2017-11-23 16:56:14 +01:00
properties,
self)
2017-11-23 16:56:14 +01:00
2017-11-28 22:42:30 +01:00
def validate_mandatory(self,
value,
2018-08-01 08:37:58 +02:00
option_bag):
2018-08-18 07:51:04 +02:00
if 'mandatory' in option_bag.config_bag.properties:
2018-09-07 06:14:52 +02:00
values = option_bag.config_bag.context.cfgimpl_get_values()
2018-08-17 23:11:25 +02:00
is_mandatory = False
2018-08-18 08:06:29 +02:00
if ('permissive' in option_bag.config_bag.properties) and \
'mandatory' in option_bag.config_bag.permissives:
2017-12-28 11:47:29 +01:00
pass
2018-08-17 23:11:25 +02:00
elif 'mandatory' in option_bag.properties and values.isempty(option_bag.option,
2017-12-28 11:47:29 +01:00
value,
2018-08-01 08:37:58 +02:00
index=option_bag.index):
2017-12-28 11:47:29 +01:00
is_mandatory = True
2018-08-17 23:11:25 +02:00
if 'empty' in option_bag.properties and values.isempty(option_bag.option,
2017-12-19 23:11:45 +01:00
value,
force_allow_empty_list=True,
2018-08-01 08:37:58 +02:00
index=option_bag.index):
2017-12-28 11:47:29 +01:00
is_mandatory = True
2018-08-17 23:11:25 +02:00
if is_mandatory:
raise PropertiesOptionError(option_bag,
['mandatory'],
self)
2017-11-20 17:01:36 +01:00
2017-12-13 22:15:34 +01:00
def validate_frozen(self,
2018-08-01 08:37:58 +02:00
option_bag):
2018-08-18 07:51:04 +02:00
if option_bag.config_bag.properties and \
('everything_frozen' in option_bag.config_bag.properties or
2018-08-01 08:37:58 +02:00
'frozen' in option_bag.properties) and \
2018-08-18 08:06:29 +02:00
not (('permissive' in option_bag.config_bag.properties) and
'frozen' in option_bag.config_bag.permissives):
2018-08-01 08:37:58 +02:00
raise PropertiesOptionError(option_bag,
['frozen'],
self)
2017-12-28 11:47:29 +01:00
return False
2017-11-20 17:01:36 +01:00
#____________________________________________________________
# read only/read write
def _read(self,
remove,
2018-09-07 06:14:52 +02:00
append,
context):
2017-11-20 17:01:36 +01:00
props = self._p_.getproperties(None,
default_properties)
modified = False
2017-12-28 11:47:29 +01:00
if remove & props:
2017-11-20 17:01:36 +01:00
props = props - remove
modified = True
if append & props != append:
props = props | append
modified = True
if modified:
2018-09-07 06:14:52 +02:00
self.set_context_properties(frozenset(props),
context)
2017-11-20 17:01:36 +01:00
2018-09-07 06:14:52 +02:00
def read_only(self,
context):
2017-11-20 17:01:36 +01:00
"convenience method to freeze, hide and disable"
self._read(ro_remove,
2018-09-07 06:14:52 +02:00
ro_append,
context)
2017-11-20 17:01:36 +01:00
2018-09-07 06:14:52 +02:00
def read_write(self,
context):
2017-11-20 17:01:36 +01:00
"convenience method to freeze, hide and disable"
self._read(rw_remove,
2018-09-07 06:14:52 +02:00
rw_append,
context)