owners are *real* objects now

This commit is contained in:
gwen
2012-12-10 14:10:05 +01:00
parent 07d3cb1037
commit cd50cf7551
7 changed files with 147 additions and 114 deletions

View File

@ -26,7 +26,7 @@ from tiramisu.error import (PropertiesOptionError, ConfigError, NotFoundError,
MandatoryError, MethodCallError, NoValueReturned)
from tiramisu.option import (OptionDescription, Option, SymLinkOption,
Multi, apply_requires)
from tiramisu.setting import settings, groups
from tiramisu.setting import settings, groups, owners
# ____________________________________________________________
class Config(object):
@ -81,7 +81,7 @@ class Config(object):
childdef = child.getdefault()
self._cfgimpl_values[child._name] = childdef
self._cfgimpl_previous_values[child._name] = childdef
child.setowner(self, 'default')
child.setowner(self, owners.default)
elif isinstance(child, OptionDescription):
self._validate_duplicates(child._children)
self._cfgimpl_values[child._name] = Config(child, parent=self)
@ -100,7 +100,7 @@ class Config(object):
copy(child.getdefault()), config=self, opt=child)
else:
self._cfgimpl_values[child._name] = copy(child.getdefault())
child.setowner(self, 'default')
child.setowner(self, owners.default)
elif isinstance(child, OptionDescription):
if child._name not in self._cfgimpl_values:
self._cfgimpl_values[child._name] = Config(child, parent=self)
@ -117,7 +117,7 @@ class Config(object):
return setattr(homeconfig, name, value)
if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
self._validate(name, getattr(self._cfgimpl_descr, name))
self.setoption(name, value, settings.owner)
self.setoption(name, value, owners.user)
def _validate(self, name, opt_or_descr, permissive=False):
"validation for the setattr and the getattr"
@ -263,7 +263,7 @@ class Config(object):
raise ConfigError('invalid calculated value returned'
' for option {0}'.format(name))
self._cfgimpl_values[name] = _result
opt_or_descr.setowner(self, 'default')
opt_or_descr.setowner(self, owners.default)
# frozen and force default
if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze():
value = opt_or_descr.getdefault()
@ -272,7 +272,7 @@ class Config(object):
use_default_multi=True,
default_multi=opt_or_descr.getdefault_multi())
self._cfgimpl_values[name] = value
opt_or_descr.setowner(self, 'default')
opt_or_descr.setowner(self, owners.default)
self._test_mandatory(name, opt_or_descr)
value = self._cfgimpl_values[name]
self._valid_len(name, value)
@ -307,17 +307,14 @@ class Config(object):
def setoption(self, name, value, who=None):
"""effectively modifies the value of an Option()
(typically called by the __setattr__)
:param who: is an owner's name
who is **not necessarily** a owner, because it cannot be a list
:type who: string
:param who : an object that lives in `setting.owners`
"""
child = getattr(self._cfgimpl_descr, name)
if type(child) != SymLinkOption:
if who == None:
who = settings.owner
if child.is_multi():
if who != 'default':
if not isinstance(who, owners.DefaultOwner):
if type(value) != Multi:
if type(value) == list:
value = Multi(value, self, child)
@ -329,11 +326,15 @@ class Config(object):
use_default_multi=True,
default_multi=child.getdefault_multi())
self._valid_len(name, value)
child.setoption(self, value, who)
if not isinstance(who, owners.Owner):
raise TypeError("invalid owner [{0}] for option: {1}".format(
str(who), name))
print "ssdfsdfsdfsdfsdf", type(child)
child.setoption(self, value)
child.setowner(self, who)
else:
homeconfig = self._cfgimpl_get_toplevel()
child.setoption(homeconfig, value, who)
child.setoption(homeconfig, value)
def set(self, **kwargs):
"""
@ -355,7 +356,7 @@ class Config(object):
pass
except Exception, e:
raise e # HiddenOptionError or DisabledOptionError
homeconfig.setoption(name, value, settings.owner)
homeconfig.setoption(name, value, owners.user)
elif len(candidates) > 1:
raise AmbigousOptionError(
'more than one option that ends with %s' % (key, ))
@ -457,8 +458,8 @@ class Config(object):
"""iteration on groups objects only.
All groups are returned if `group_type` is `None`, otherwise the groups
can be filtered by categories (families, or whatever).
:param group_type: if defined, is an instance of `settings.GroupName`
or `settings.MasterGroupName` that lives in
:param group_type: if defined, is an instance of `groups.GroupName`
or `groups.MasterGroupName` that lives in
`settings.groups`
"""

View File

@ -26,7 +26,7 @@ from tiramisu.error import (ConfigError, ConflictConfigError, NotFoundError,
RequiresError, RequirementRecursionError, MandatoryError,
PropertiesOptionError)
from tiramisu.autolib import carry_out_calculation
from tiramisu.setting import settings, groups
from tiramisu.setting import settings, groups, owners
requires_actions = [('hide', 'show'), ('enable', 'disable'), ('freeze', 'unfreeze')]
@ -65,7 +65,7 @@ class Multi(list):
super(Multi, self).__init__(lst)
def __setitem__(self, key, value):
return self._setvalue(value, key, who=settings.owner)
return self._setvalue(value, key, who=owners.user)
def append(self, value, add_master=True):
"""the list value can be updated (appened)
@ -307,13 +307,13 @@ class Option(HiddenBaseType, DisabledBaseType):
"""
:param config: *must* be only the **parent** config
(not the toplevel config)
:param owner: is a **real** owner, that is a name or a list
which is allowable here
:param owner: is a **real** owner, that is an object
that lives in setting.owners
"""
name = self._name
if not type(owner) == str:
raise ConfigError("invalid type for owner option: {0}".format(
name))
if not isinstance(owner, owners.Owner):
raise ConfigError("invalid type owner for option: {0}".format(
str(name)))
config._cfgimpl_value_owners[name] = owner
def getowner(self, config):
@ -323,7 +323,7 @@ class Option(HiddenBaseType, DisabledBaseType):
def reset(self, config):
"""resets the default value and owner
"""
config.setoption(self._name, self.getdefault(), 'default')
config.setoption(self._name, self.getdefault(), owners.default)
def is_default_owner(self, config):
"""
@ -333,11 +333,10 @@ class Option(HiddenBaseType, DisabledBaseType):
"""
return self.getowner(config) == 'default'
def setoption(self, config, value, who):
def setoption(self, config, value):
"""changes the option's value with the value_owner's who
:param config: the parent config is necessary here to store the value
:param who : is **not necessarily** a owner because it cannot be a list
:type who: string """
"""
name = self._name
rootconfig = config._cfgimpl_get_toplevel()
if not self.validate(value, settings.validator):
@ -446,7 +445,7 @@ class SymLinkOption(object):
self.path = path
self.opt = opt
def setoption(self, config, value, who):
def setoption(self, config, value):
setattr(config, self.path, value)
def __getattr__(self, name):

View File

@ -42,6 +42,10 @@ class GroupModule(_const):
*normal* means : groups that are not master
"""
pass
class DefaultGroupName(GroupName):
"""groups that are default (typically 'default')"""
pass
class MasterGroupName(GroupName):
"""allowed normal group (OptionDescription) names
*master* means : groups that have the 'master' attribute set
@ -54,16 +58,44 @@ def populate_groups():
"populates the available groups in the appropriate namespaces"
_available_group_names = ('default', 'family', 'group')
_available_groups_with_a_master = ('group', )
_available_default_groups = ('default', )
# populates normal or master groups
for grp in _available_group_names:
if grp in _available_groups_with_a_master:
setattr(groups, grp, groups.MasterGroupName(grp))
elif grp in _available_default_groups:
setattr(groups, grp, groups.DefaultGroupName(grp))
else:
setattr(groups, grp, groups.GroupName(grp))
# names are in the module now
populate_groups()
# ____________________________________________________________
class OwnerModule(_const):
"""emulates a module to manage unique owner names.
owners are living in `Config._cfgimpl_value_owners`
"""
class Owner(str):
"""allowed owner names
"""
pass
class DefaultOwner(Owner):
"""groups that are default (typically 'default')"""
pass
# setting.owners (emulates a module)
owners = OwnerModule()
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'))
setattr(owners,'user', owners.Owner('user'))
# names are in the module now
populate_owners()
#____________________________________________________________
class Setting():
"``Config()``'s configuration options"
# properties attribute: the name of a property enables this property
@ -75,8 +107,6 @@ class Setting():
frozen = True
# enables validation function for options if set
validator = False
# generic owner. 'default' is the general config owner after init time
owner = 'user'
# ____________________________________________________________
# properties methods
def has_properties(self):