2012-11-19 10:45:03 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"sets the options of the configuration objects Config object itself"
|
2013-03-01 13:10:52 +01:00
|
|
|
# Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
|
2012-11-19 10:45:03 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
# The original `Config` design model is unproudly borrowed from
|
|
|
|
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
|
|
|
|
# the whole pypy projet is under MIT licence
|
|
|
|
# ____________________________________________________________
|
2013-04-18 20:26:40 +02:00
|
|
|
from time import time
|
2013-05-08 18:14:42 +02:00
|
|
|
from copy import copy
|
2013-08-27 11:39:32 +02:00
|
|
|
import weakref
|
2013-08-26 21:48:42 +02:00
|
|
|
from tiramisu.error import (RequirementError, PropertiesOptionError,
|
|
|
|
ConstError, ConfigError)
|
2013-04-14 12:01:32 +02:00
|
|
|
from tiramisu.i18n import _
|
2013-04-08 16:05:56 +02:00
|
|
|
|
|
|
|
|
2013-07-01 16:49:10 +02:00
|
|
|
default_encoding = 'utf-8'
|
2013-04-18 20:26:40 +02:00
|
|
|
expires_time = 5
|
2013-09-04 09:09:37 +02:00
|
|
|
ro_remove = set('permissive', 'hidden')
|
|
|
|
ro_append = set('frozen', 'disabled', 'validator', 'everything_frozen',
|
|
|
|
'mandatory')
|
|
|
|
rw_remove = set('permissive', 'everything_frozen', 'mandatory')
|
|
|
|
rw_append = set('frozen', 'disabled', 'validator', 'hidden')
|
2013-07-13 10:42:10 +02:00
|
|
|
default_properties = ('expire', 'validator')
|
|
|
|
|
2013-04-18 20:26:40 +02:00
|
|
|
|
2013-08-26 21:48:42 +02:00
|
|
|
class StorageType:
|
|
|
|
default_storage = 'dictionary'
|
|
|
|
storage_type = None
|
|
|
|
|
|
|
|
def set_storage(self, name):
|
|
|
|
if self.storage_type is not None:
|
|
|
|
raise ConfigError(_('storage_type is already set, cannot rebind it'))
|
|
|
|
self.storage_type = name
|
|
|
|
|
|
|
|
def get_storage(self):
|
|
|
|
if self.storage_type is None:
|
2013-08-26 21:54:41 +02:00
|
|
|
self.storage_type = self.default_storage
|
|
|
|
storage = self.storage_type
|
2013-08-26 21:48:42 +02:00
|
|
|
return 'tiramisu.storage.{0}.storage'.format(
|
|
|
|
storage)
|
|
|
|
|
|
|
|
|
|
|
|
storage_type = StorageType()
|
|
|
|
|
|
|
|
|
|
|
|
class _NameSpace:
|
2012-12-06 18:14:57 +01:00
|
|
|
"""convenient class that emulates a module
|
2012-12-10 09:53:13 +01:00
|
|
|
and builds constants (that is, unique names)"""
|
2012-12-06 18:14:57 +01:00
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
2013-04-03 12:20:26 +02:00
|
|
|
if name in self.__dict__:
|
2013-08-26 21:48:42 +02:00
|
|
|
raise ConstError(_("can't rebind {0}").format(name))
|
2012-12-06 18:14:57 +01:00
|
|
|
self.__dict__[name] = value
|
|
|
|
|
|
|
|
def __delattr__(self, name):
|
2013-04-03 12:20:26 +02:00
|
|
|
if name in self.__dict__:
|
2013-08-26 21:48:42 +02:00
|
|
|
raise ConstError(_("can't unbind {0}").format(name))
|
2013-04-14 12:01:32 +02:00
|
|
|
raise ValueError(name)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
|
|
|
|
2012-12-10 09:53:13 +01:00
|
|
|
# ____________________________________________________________
|
2013-08-26 21:48:42 +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
|
2012-12-10 09:53:13 +01:00
|
|
|
# setting.groups (emulates a module)
|
|
|
|
groups = GroupModule()
|
2012-12-06 18:14:57 +01:00
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2012-12-10 09:53:13 +01:00
|
|
|
def populate_groups():
|
|
|
|
"populates the available groups in the appropriate namespaces"
|
2013-02-06 16:21:30 +01:00
|
|
|
groups.master = groups.MasterGroupType('master')
|
|
|
|
groups.default = groups.DefaultGroupType('default')
|
|
|
|
groups.family = groups.GroupType('family')
|
|
|
|
|
2012-12-10 09:53:13 +01:00
|
|
|
# names are in the module now
|
2012-12-06 18:14:57 +01:00
|
|
|
populate_groups()
|
2013-04-03 12:20:26 +02:00
|
|
|
|
|
|
|
|
2012-12-06 18:14:57 +01:00
|
|
|
# ____________________________________________________________
|
2013-08-26 21:48:42 +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
|
|
|
|
# setting.owners (emulates a module)
|
|
|
|
owners = OwnerModule()
|
2012-12-04 12:06:26 +01:00
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2012-12-10 14:10:05 +01:00
|
|
|
def populate_owners():
|
|
|
|
"""populates the available owners in the appropriate namespaces
|
|
|
|
|
|
|
|
- 'user' is the generic is the generic owner.
|
|
|
|
- 'default' is the config owner after init time
|
|
|
|
"""
|
|
|
|
setattr(owners, 'default', owners.DefaultOwner('default'))
|
2013-04-03 12:20:26 +02:00
|
|
|
setattr(owners, 'user', owners.Owner('user'))
|
|
|
|
|
2013-08-27 16:12:53 +02:00
|
|
|
def addowner(name):
|
2012-12-10 14:38:25 +01:00
|
|
|
"""
|
|
|
|
:param name: the name of the new owner
|
|
|
|
"""
|
|
|
|
setattr(owners, name, owners.Owner(name))
|
2013-08-27 16:12:53 +02:00
|
|
|
setattr(owners, 'addowner', addowner)
|
2012-12-10 14:38:25 +01:00
|
|
|
|
2012-12-10 14:10:05 +01:00
|
|
|
# names are in the module now
|
|
|
|
populate_owners()
|
2013-02-21 17:07:00 +01:00
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-08-26 21:48:42 +02:00
|
|
|
class MultiTypeModule(_NameSpace):
|
2013-05-23 14:55:52 +02:00
|
|
|
"namespace for the master/slaves"
|
2013-02-21 17:07:00 +01:00
|
|
|
class MultiType(str):
|
|
|
|
pass
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-02-21 17:07:00 +01:00
|
|
|
class DefaultMultiType(MultiType):
|
|
|
|
pass
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-02-21 17:07:00 +01:00
|
|
|
class MasterMultiType(MultiType):
|
|
|
|
pass
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-02-21 17:07:00 +01:00
|
|
|
class SlaveMultiType(MultiType):
|
|
|
|
pass
|
|
|
|
|
|
|
|
multitypes = MultiTypeModule()
|
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-02-21 17:07:00 +01:00
|
|
|
def populate_multitypes():
|
2013-05-23 14:55:52 +02:00
|
|
|
"populates the master/slave namespace"
|
2013-02-21 17:07:00 +01:00
|
|
|
setattr(multitypes, 'default', multitypes.DefaultMultiType('default'))
|
|
|
|
setattr(multitypes, 'master', multitypes.MasterMultiType('master'))
|
|
|
|
setattr(multitypes, 'slave', multitypes.SlaveMultiType('slave'))
|
|
|
|
|
|
|
|
populate_multitypes()
|
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-04-20 17:30:05 +02:00
|
|
|
class Property(object):
|
2013-05-21 18:42:56 +02:00
|
|
|
"a property is responsible of the option's value access rules"
|
2013-08-21 22:21:50 +02:00
|
|
|
__slots__ = ('_setting', '_properties', '_opt', '_path')
|
2013-04-20 17:30:05 +02:00
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def __init__(self, setting, prop, opt=None, path=None):
|
2013-04-20 17:30:05 +02:00
|
|
|
self._opt = opt
|
2013-08-21 22:21:50 +02:00
|
|
|
self._path = path
|
2013-04-20 17:30:05 +02:00
|
|
|
self._setting = setting
|
|
|
|
self._properties = prop
|
|
|
|
|
|
|
|
def append(self, propname):
|
2013-08-22 22:46:02 +02:00
|
|
|
if self._opt is not None and self._opt._calc_properties is not None \
|
|
|
|
and propname in self._opt._calc_properties:
|
|
|
|
raise ValueError(_('cannot append {0} property for option {1}: '
|
|
|
|
'this property is calculated').format(
|
|
|
|
propname, self._opt._name))
|
2013-05-08 18:14:42 +02:00
|
|
|
self._properties.add(propname)
|
2013-08-21 22:21:50 +02:00
|
|
|
self._setting._setproperties(self._properties, self._opt, self._path)
|
2013-04-20 17:30:05 +02:00
|
|
|
|
|
|
|
def remove(self, propname):
|
|
|
|
if propname in self._properties:
|
|
|
|
self._properties.remove(propname)
|
2013-08-21 22:21:50 +02:00
|
|
|
self._setting._setproperties(self._properties, self._opt, self._path)
|
2013-07-13 10:42:10 +02:00
|
|
|
|
|
|
|
def reset(self):
|
2013-08-24 16:30:46 +02:00
|
|
|
self._setting.reset(_path=self._path)
|
2013-04-20 17:30:05 +02:00
|
|
|
|
|
|
|
def __contains__(self, propname):
|
|
|
|
return propname in self._properties
|
|
|
|
|
2013-04-22 09:19:05 +02:00
|
|
|
def __repr__(self):
|
2013-05-08 18:14:42 +02:00
|
|
|
return str(list(self._properties))
|
2013-04-22 09:19:05 +02:00
|
|
|
|
2013-04-20 17:30:05 +02:00
|
|
|
|
2013-08-26 21:48:42 +02:00
|
|
|
def set_storage(name, **args):
|
|
|
|
storage_type.set_storage(name)
|
|
|
|
settings = __import__(storage_type.get_storage(), globals(), locals(),
|
2013-08-28 11:33:43 +02:00
|
|
|
['Setting']).Setting()
|
2013-08-26 21:48:42 +02:00
|
|
|
for option, value in args.items():
|
|
|
|
try:
|
|
|
|
getattr(settings, option)
|
|
|
|
setattr(settings, option, value)
|
|
|
|
except AttributeError:
|
|
|
|
raise ValueError(_('option {0} not already exists in storage {1}'
|
|
|
|
'').format(option, name))
|
2013-08-25 20:49:24 +02:00
|
|
|
|
|
|
|
|
2013-08-27 09:46:52 +02:00
|
|
|
def get_storage(context, session_id, persistent):
|
2013-08-25 20:49:24 +02:00
|
|
|
def gen_id(config):
|
|
|
|
return str(id(config)) + str(time())
|
|
|
|
|
2013-08-25 21:57:11 +02:00
|
|
|
if session_id is None:
|
|
|
|
session_id = gen_id(context)
|
2013-08-27 17:13:20 +02:00
|
|
|
return __import__(storage_type.get_storage(), globals(), locals(),
|
2013-08-28 11:33:43 +02:00
|
|
|
['Storage']).Storage(session_id, persistent)
|
2013-08-25 20:49:24 +02:00
|
|
|
|
|
|
|
|
2013-08-26 21:54:41 +02:00
|
|
|
def list_sessions():
|
|
|
|
return __import__(storage_type.get_storage(), globals(), locals(),
|
2013-08-28 11:33:43 +02:00
|
|
|
['list_sessions']).list_sessions()
|
2013-08-26 21:54:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def delete_session(session_id):
|
|
|
|
return __import__(storage_type.get_storage(), globals(), locals(),
|
2013-08-28 11:33:43 +02:00
|
|
|
['delete_session']).delete_session(session_id)
|
2013-08-26 21:54:41 +02:00
|
|
|
|
|
|
|
|
2012-12-10 14:10:05 +01:00
|
|
|
#____________________________________________________________
|
2013-08-20 09:47:12 +02:00
|
|
|
class Settings(object):
|
2012-11-19 10:45:03 +01:00
|
|
|
"``Config()``'s configuration options"
|
2013-08-27 21:36:52 +02:00
|
|
|
__slots__ = ('context', '_owner', '_p_', '__weakref__')
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-08-20 22:45:11 +02:00
|
|
|
def __init__(self, context, storage):
|
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
|
2013-04-20 17:30:05 +02:00
|
|
|
self._owner = owners.user
|
2013-08-27 11:39:32 +02:00
|
|
|
self.context = weakref.ref(context)
|
2013-08-25 20:49:24 +02:00
|
|
|
import_lib = 'tiramisu.storage.{0}.setting'.format(storage.storage)
|
2013-08-28 11:33:43 +02:00
|
|
|
self._p_ = __import__(import_lib, globals(), locals(), ['Settings']
|
|
|
|
).Settings(storage)
|
2013-08-20 09:47:12 +02:00
|
|
|
|
2012-12-11 11:18:53 +01:00
|
|
|
#____________________________________________________________
|
2012-11-19 10:45:03 +01:00
|
|
|
# properties methods
|
2013-04-20 17:30:05 +02:00
|
|
|
def __contains__(self, propname):
|
2013-08-21 18:34:32 +02:00
|
|
|
"enables the pythonic 'in' syntaxic sugar"
|
2013-08-14 23:06:31 +02:00
|
|
|
return propname in self._getproperties()
|
2013-04-20 17:30:05 +02:00
|
|
|
|
2013-04-22 09:19:05 +02:00
|
|
|
def __repr__(self):
|
2013-08-14 23:06:31 +02:00
|
|
|
return str(list(self._getproperties()))
|
2013-04-22 09:19:05 +02:00
|
|
|
|
2013-04-20 17:30:05 +02:00
|
|
|
def __getitem__(self, opt):
|
2013-08-24 16:30:46 +02:00
|
|
|
path = self._get_opt_path(opt)
|
2013-08-21 22:21:50 +02:00
|
|
|
return self._getitem(opt, path)
|
|
|
|
|
|
|
|
def _getitem(self, opt, path):
|
|
|
|
return Property(self, self._getproperties(opt, path), opt, path)
|
2013-03-14 11:31:44 +01:00
|
|
|
|
2013-04-20 17:30:05 +02:00
|
|
|
def __setitem__(self, opt, value):
|
2013-08-25 18:06:07 +02:00
|
|
|
raise ValueError('you should only append/remove properties')
|
2013-04-20 17:30:05 +02:00
|
|
|
|
2013-08-24 16:30:46 +02:00
|
|
|
def reset(self, opt=None, _path=None, all_properties=False):
|
|
|
|
if all_properties and (_path or opt):
|
2013-07-13 10:42:10 +02:00
|
|
|
raise ValueError(_('opt and all_properties must not be set '
|
|
|
|
'together in reset'))
|
|
|
|
if all_properties:
|
2013-08-20 09:47:12 +02:00
|
|
|
self._p_.reset_all_propertives()
|
2013-07-13 10:42:10 +02:00
|
|
|
else:
|
2013-08-24 16:30:46 +02:00
|
|
|
if opt is not None and _path is None:
|
|
|
|
_path = self._get_opt_path(opt)
|
|
|
|
self._p_.reset_properties(_path)
|
2013-08-27 11:39:32 +02:00
|
|
|
self.context().cfgimpl_reset_cache()
|
2013-07-12 16:20:34 +02:00
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def _getproperties(self, opt=None, path=None, is_apply_req=True):
|
2013-04-03 12:20:26 +02:00
|
|
|
if opt is None:
|
2013-08-21 22:21:50 +02:00
|
|
|
props = self._p_.getproperties(path, default_properties)
|
2013-04-03 12:20:26 +02:00
|
|
|
else:
|
2013-08-21 22:21:50 +02:00
|
|
|
if path is None:
|
2013-08-22 11:08:26 +02:00
|
|
|
raise ValueError(_('if opt is not None, path should not be'
|
|
|
|
' None in _getproperties'))
|
2013-08-14 23:06:31 +02:00
|
|
|
ntime = None
|
2013-08-21 22:21:50 +02:00
|
|
|
if self._p_.hascache('property', path):
|
2013-08-14 23:06:31 +02:00
|
|
|
ntime = time()
|
2013-08-21 22:21:50 +02:00
|
|
|
is_cached, props = self._p_.getcache('property', path, ntime)
|
2013-08-14 23:06:31 +02:00
|
|
|
if is_cached:
|
2013-05-02 11:23:04 +02:00
|
|
|
return props
|
2013-08-21 22:21:50 +02:00
|
|
|
props = self._p_.getproperties(path, opt._properties)
|
2013-08-22 22:46:02 +02:00
|
|
|
if is_apply_req:
|
|
|
|
props |= self.apply_requires(opt, path)
|
2013-08-14 23:06:31 +02:00
|
|
|
if 'expire' in self:
|
|
|
|
if ntime is None:
|
|
|
|
ntime = time()
|
2013-08-21 22:21:50 +02:00
|
|
|
self._p_.setcache('property', path, props, ntime + expires_time)
|
2013-04-20 11:25:14 +02:00
|
|
|
return props
|
2013-03-14 11:31:44 +01:00
|
|
|
|
2013-04-20 17:30:05 +02:00
|
|
|
def append(self, propname):
|
2012-11-19 10:45:03 +01:00
|
|
|
"puts property propname in the Config's properties attribute"
|
2013-08-22 22:46:02 +02:00
|
|
|
props = self._p_.getproperties(None, default_properties)
|
|
|
|
props.add(propname)
|
|
|
|
self._setproperties(props, None, None)
|
2012-11-19 10:45:03 +01:00
|
|
|
|
2013-04-20 17:30:05 +02:00
|
|
|
def remove(self, propname):
|
2012-11-19 10:45:03 +01:00
|
|
|
"deletes property propname in the Config's properties attribute"
|
2013-08-22 22:46:02 +02:00
|
|
|
props = self._p_.getproperties(None, default_properties)
|
|
|
|
if propname in props:
|
|
|
|
props.remove(propname)
|
|
|
|
self._setproperties(props, None, None)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def _setproperties(self, properties, opt, path):
|
2013-04-03 12:20:26 +02:00
|
|
|
"""save properties for specified opt
|
|
|
|
(never save properties if same has option properties)
|
|
|
|
"""
|
|
|
|
if opt is None:
|
2013-08-21 23:21:28 +02:00
|
|
|
self._p_.setproperties(None, properties)
|
2013-04-03 12:20:26 +02:00
|
|
|
else:
|
2013-08-22 22:46:02 +02:00
|
|
|
if opt._calc_properties is not None:
|
|
|
|
properties -= opt._calc_properties
|
2013-07-13 10:42:10 +02:00
|
|
|
if set(opt._properties) == properties:
|
2013-08-21 22:21:50 +02:00
|
|
|
self._p_.reset_properties(path)
|
2013-04-03 12:20:26 +02:00
|
|
|
else:
|
2013-08-21 22:21:50 +02:00
|
|
|
self._p_.setproperties(path, properties)
|
2013-08-27 11:39:32 +02:00
|
|
|
self.context().cfgimpl_reset_cache()
|
2013-04-20 17:30:05 +02:00
|
|
|
|
2012-12-11 11:18:53 +01:00
|
|
|
#____________________________________________________________
|
2013-08-21 22:21:50 +02:00
|
|
|
def validate_properties(self, opt_or_descr, is_descr, is_write, path,
|
2013-04-17 21:33:34 +02:00
|
|
|
value=None, force_permissive=False,
|
|
|
|
force_properties=None):
|
2013-08-20 16:38:06 +02:00
|
|
|
"""
|
2013-08-20 16:44:52 +02:00
|
|
|
validation upon the properties related to `opt_or_descr`
|
|
|
|
|
2013-08-20 16:38:06 +02:00
|
|
|
:param opt_or_descr: an option or an option description object
|
2013-08-22 11:08:26 +02:00
|
|
|
:param force_permissive: behaves as if the permissive property
|
|
|
|
was present
|
2013-08-20 16:44:52 +02:00
|
|
|
:param is_descr: we have to know if we are in an option description,
|
2013-08-22 11:08:26 +02:00
|
|
|
just because the mandatory property
|
|
|
|
doesn't exist here
|
2013-08-20 16:44:52 +02:00
|
|
|
|
2013-08-20 16:38:06 +02:00
|
|
|
:param is_write: in the validation process, an option is to be modified,
|
2013-08-22 11:08:26 +02:00
|
|
|
the behavior can be different
|
|
|
|
(typically with the `frozen` property)
|
2013-08-20 16:38:06 +02:00
|
|
|
"""
|
2013-08-21 17:21:09 +02:00
|
|
|
# opt properties
|
2013-08-21 22:21:50 +02:00
|
|
|
properties = copy(self._getproperties(opt_or_descr, path))
|
2013-08-21 17:21:09 +02:00
|
|
|
# remove opt permissive
|
2013-08-21 22:21:50 +02:00
|
|
|
properties -= self._p_.getpermissive(path)
|
2013-08-21 17:21:09 +02:00
|
|
|
# remove global permissive if need
|
2013-08-14 23:06:31 +02:00
|
|
|
self_properties = copy(self._getproperties())
|
2013-08-20 16:45:54 +02:00
|
|
|
if force_permissive is True or 'permissive' in self_properties:
|
2013-08-20 09:47:12 +02:00
|
|
|
properties -= self._p_.getpermissive()
|
2013-05-02 11:23:04 +02:00
|
|
|
|
2013-08-20 16:38:06 +02:00
|
|
|
# global properties
|
2013-05-02 11:23:04 +02:00
|
|
|
if force_properties is not None:
|
2013-05-08 18:14:42 +02:00
|
|
|
self_properties.update(force_properties)
|
2013-05-02 11:23:04 +02:00
|
|
|
|
2013-08-20 16:38:06 +02:00
|
|
|
# calc properties
|
2013-05-08 18:14:42 +02:00
|
|
|
properties &= self_properties
|
2013-08-20 16:38:06 +02:00
|
|
|
# mandatory and frozen are special properties
|
2013-05-02 11:23:04 +02:00
|
|
|
if is_descr:
|
2013-05-08 18:14:42 +02:00
|
|
|
properties -= frozenset(('mandatory', 'frozen'))
|
2013-05-02 11:23:04 +02:00
|
|
|
else:
|
|
|
|
if 'mandatory' in properties and \
|
2013-08-27 11:39:32 +02:00
|
|
|
not self.context().cfgimpl_get_values()._isempty(
|
2013-08-20 16:33:32 +02:00
|
|
|
opt_or_descr, value):
|
2013-05-02 11:23:04 +02:00
|
|
|
properties.remove('mandatory')
|
|
|
|
if is_write and 'everything_frozen' in self_properties:
|
|
|
|
properties.add('frozen')
|
|
|
|
elif 'frozen' in properties and not is_write:
|
|
|
|
properties.remove('frozen')
|
2013-08-20 16:44:52 +02:00
|
|
|
# at this point an option should not remain in properties
|
2013-05-02 11:23:04 +02:00
|
|
|
if properties != frozenset():
|
2013-07-23 14:24:42 +02:00
|
|
|
props = list(properties)
|
2013-05-02 11:23:04 +02:00
|
|
|
if 'frozen' in properties:
|
2013-07-18 21:25:07 +02:00
|
|
|
raise PropertiesOptionError(_('cannot change the value for '
|
2013-08-20 12:08:02 +02:00
|
|
|
'option {0} this option is'
|
|
|
|
' frozen').format(
|
2013-08-20 16:48:19 +02:00
|
|
|
opt_or_descr._name),
|
|
|
|
props)
|
2013-05-02 11:23:04 +02:00
|
|
|
else:
|
2013-07-18 21:25:07 +02:00
|
|
|
raise PropertiesOptionError(_("trying to access to an option "
|
|
|
|
"named: {0} with properties {1}"
|
2013-08-20 12:08:02 +02:00
|
|
|
"").format(opt_or_descr._name,
|
2013-08-20 16:48:19 +02:00
|
|
|
str(props)), props)
|
2013-04-17 21:33:34 +02:00
|
|
|
|
2013-08-28 22:05:44 +02:00
|
|
|
def setpermissive(self, permissive, opt=None, path=None):
|
2013-08-29 09:32:32 +02:00
|
|
|
"""
|
|
|
|
enables us to put the permissives in the storage
|
2013-09-02 17:13:07 +02:00
|
|
|
|
|
|
|
:param path: the option's path
|
2013-08-29 09:32:32 +02:00
|
|
|
:param type: str
|
2013-09-02 17:13:07 +02:00
|
|
|
:param opt: if an option object is set, the path is extracted.
|
|
|
|
it is better (faster) to set the path parameter
|
2013-08-29 09:32:32 +02:00
|
|
|
instead of passing a :class:`tiramisu.option.Option()` object.
|
|
|
|
"""
|
2013-08-28 22:05:44 +02:00
|
|
|
if opt is not None and path is None:
|
|
|
|
path = self._get_opt_path(opt)
|
2013-04-20 17:30:05 +02:00
|
|
|
if not isinstance(permissive, tuple):
|
|
|
|
raise TypeError(_('permissive must be a tuple'))
|
2013-08-21 22:21:50 +02:00
|
|
|
self._p_.setpermissive(path, permissive)
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2013-02-06 14:59:24 +01:00
|
|
|
#____________________________________________________________
|
2013-04-03 12:20:26 +02:00
|
|
|
def setowner(self, owner):
|
|
|
|
":param owner: sets the default value for owner at the Config level"
|
|
|
|
if not isinstance(owner, owners.Owner):
|
2013-04-14 12:01:32 +02:00
|
|
|
raise TypeError(_("invalid generic owner {0}").format(str(owner)))
|
2013-04-20 17:30:05 +02:00
|
|
|
self._owner = owner
|
2012-11-19 10:45:03 +01:00
|
|
|
|
2013-04-03 12:20:26 +02:00
|
|
|
def getowner(self):
|
2013-04-20 17:30:05 +02:00
|
|
|
return self._owner
|
2013-02-06 14:59:24 +01:00
|
|
|
|
|
|
|
#____________________________________________________________
|
2013-04-20 21:58:52 +02:00
|
|
|
def _read(self, remove, append):
|
|
|
|
for prop in remove:
|
|
|
|
self.remove(prop)
|
|
|
|
for prop in append:
|
|
|
|
self.append(prop)
|
|
|
|
|
2012-11-19 10:45:03 +01:00
|
|
|
def read_only(self):
|
|
|
|
"convenience method to freeze, hidde and disable"
|
2013-04-20 21:58:52 +02:00
|
|
|
self._read(ro_remove, ro_append)
|
2012-11-19 10:45:03 +01:00
|
|
|
|
|
|
|
def read_write(self):
|
|
|
|
"convenience method to freeze, hidde and disable"
|
2013-04-20 21:58:52 +02:00
|
|
|
self._read(rw_remove, rw_append)
|
2013-04-08 16:05:56 +02:00
|
|
|
|
2013-04-19 22:42:33 +02:00
|
|
|
def reset_cache(self, only_expired):
|
|
|
|
if only_expired:
|
2013-08-20 09:47:12 +02:00
|
|
|
self._p_.reset_expired_cache('property', time())
|
2013-04-19 22:42:33 +02:00
|
|
|
else:
|
2013-08-20 09:47:12 +02:00
|
|
|
self._p_.reset_all_cache('property')
|
2013-04-18 20:26:40 +02:00
|
|
|
|
2013-08-21 22:21:50 +02:00
|
|
|
def apply_requires(self, opt, path):
|
2013-08-22 11:08:26 +02:00
|
|
|
"""carries out the jit (just in time) requirements between options
|
2013-08-22 22:46:02 +02:00
|
|
|
|
|
|
|
a requirement is a tuple of this form that comes from the option's
|
2013-08-22 11:08:26 +02:00
|
|
|
requirements validation::
|
|
|
|
|
|
|
|
(option, expected, action, inverse, transitive, same_action)
|
|
|
|
|
|
|
|
let's have a look at all the tuple's items:
|
2013-08-22 22:46:02 +02:00
|
|
|
|
2013-08-22 11:08:26 +02:00
|
|
|
- **option** is the target option's name or path
|
2013-08-22 22:46:02 +02:00
|
|
|
|
2013-08-22 11:08:26 +02:00
|
|
|
- **expected** is the target option's value that is going to trigger an action
|
2013-08-22 22:46:02 +02:00
|
|
|
|
|
|
|
- **action** is the (property) action to be accomplished if the target option
|
2013-08-22 11:08:26 +02:00
|
|
|
happens to have the expected value
|
2013-08-22 22:46:02 +02:00
|
|
|
|
|
|
|
- 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
|
2013-08-22 11:08:26 +02:00
|
|
|
properties list (wich means that the property is inverted)
|
2013-08-22 22:46:02 +02:00
|
|
|
|
|
|
|
- **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
|
2013-08-22 11:08:26 +02:00
|
|
|
is then added to the option).
|
2013-08-22 22:46:02 +02:00
|
|
|
|
|
|
|
- **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
|
2013-08-22 11:08:26 +02:00
|
|
|
exception :exc:`~error.RequirementError` is raised.
|
|
|
|
|
2013-08-22 22:46:02 +02:00
|
|
|
And at last, if no target option matches the expected values, the
|
2013-08-22 11:08:26 +02:00
|
|
|
action must be removed from the option's properties list.
|
2013-08-22 22:46:02 +02:00
|
|
|
|
2013-08-22 11:08:26 +02:00
|
|
|
: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
|
|
|
|
"""
|
2013-07-01 11:55:32 +02:00
|
|
|
if opt._requires is None:
|
2013-08-22 22:46:02 +02:00
|
|
|
return frozenset()
|
2013-07-01 11:55:32 +02:00
|
|
|
|
|
|
|
# filters the callbacks
|
2013-08-22 22:46:02 +02:00
|
|
|
calc_properties = set()
|
2013-07-01 11:55:32 +02:00
|
|
|
for requires in opt._requires:
|
|
|
|
for require in requires:
|
2013-08-20 12:08:02 +02:00
|
|
|
option, expected, action, inverse, \
|
2013-08-20 16:48:19 +02:00
|
|
|
transitive, same_action = require
|
2013-08-21 22:21:50 +02:00
|
|
|
reqpath = self._get_opt_path(option)
|
|
|
|
if reqpath == path or reqpath.startswith(path + '.'):
|
2013-07-01 11:55:32 +02:00
|
|
|
raise RequirementError(_("malformed requirements "
|
2013-08-20 16:48:19 +02:00
|
|
|
"imbrication detected for option:"
|
|
|
|
" '{0}' with requirement on: "
|
2013-08-21 22:21:50 +02:00
|
|
|
"'{1}'").format(path, reqpath))
|
2013-07-01 11:55:32 +02:00
|
|
|
try:
|
2013-08-27 11:39:32 +02:00
|
|
|
value = self.context()._getattr(reqpath,
|
2013-08-27 17:13:20 +02:00
|
|
|
force_permissive=True)
|
2013-08-28 11:33:43 +02:00
|
|
|
except PropertiesOptionError as err:
|
2013-07-01 11:55:32 +02:00
|
|
|
if not transitive:
|
|
|
|
continue
|
|
|
|
properties = err.proptype
|
|
|
|
if same_action and action not in properties:
|
2013-08-20 12:08:02 +02:00
|
|
|
raise RequirementError(_("option '{0}' has "
|
|
|
|
"requirement's property "
|
|
|
|
"error: "
|
|
|
|
"{1} {2}").format(opt._name,
|
2013-08-21 22:21:50 +02:00
|
|
|
reqpath,
|
2013-08-20 12:08:02 +02:00
|
|
|
properties))
|
2013-08-21 17:21:09 +02:00
|
|
|
# transitive action, force expected
|
2013-07-03 15:04:15 +02:00
|
|
|
value = expected[0]
|
2013-07-01 11:55:32 +02:00
|
|
|
inverse = False
|
2013-08-20 12:08:02 +02:00
|
|
|
if (not inverse and
|
2013-08-20 16:48:19 +02:00
|
|
|
value in expected or
|
|
|
|
inverse and value not in expected):
|
2013-08-22 22:46:02 +02:00
|
|
|
calc_properties.add(action)
|
2013-08-21 17:21:09 +02:00
|
|
|
# the calculation cannot be carried out
|
2013-07-01 11:55:32 +02:00
|
|
|
break
|
2013-09-02 17:13:07 +02:00
|
|
|
return calc_properties
|
2013-08-19 11:01:21 +02:00
|
|
|
|
|
|
|
def _get_opt_path(self, opt):
|
2013-08-27 11:39:32 +02:00
|
|
|
return self.context().cfgimpl_get_description().impl_get_path_by_opt(opt)
|