list is Multi now which enables us to implement item access

This commit is contained in:
gwen 2012-07-11 16:47:07 +02:00
parent 736a1c77b6
commit 1ccca60530
3 changed files with 24 additions and 6 deletions

View File

@ -25,7 +25,7 @@ from error import (HiddenOptionError, ConfigError, NotFoundError,
SpecialOwnersError, MandatoryError, MethodCallError, SpecialOwnersError, MandatoryError, MethodCallError,
DisabledOptionError, ModeOptionError) DisabledOptionError, ModeOptionError)
from option import (OptionDescription, Option, SymLinkOption, group_types, from option import (OptionDescription, Option, SymLinkOption, group_types,
apply_requires, modes) Multi, apply_requires, modes)
import autolib import autolib
from autolib import special_owners, special_owner_factory from autolib import special_owners, special_owner_factory
# ____________________________________________________________ # ____________________________________________________________
@ -65,8 +65,14 @@ class Config(object):
self._validate_duplicates(self._cfgimpl_descr._children) self._validate_duplicates(self._cfgimpl_descr._children)
for child in self._cfgimpl_descr._children: for child in self._cfgimpl_descr._children:
if isinstance(child, Option): if isinstance(child, Option):
self._cfgimpl_values[child._name] = child.getdefault() if child.is_multi():
self._cfgimpl_previous_values[child._name] = child.getdefault() mm = Multi(child.getdefault())
self._cfgimpl_values[child._name] = mm
self._cfgimpl_previous_values[child._name] = mm
else:
dd = child.getdefault()
self._cfgimpl_values[child._name] = dd
self._cfgimpl_previous_values[child._name] = dd
if child.getcallback() is not None: if child.getcallback() is not None:
if child._is_hidden(): if child._is_hidden():
self._cfgimpl_value_owners[child._name] = 'auto' self._cfgimpl_value_owners[child._name] = 'auto'

View File

@ -30,7 +30,19 @@ reverse_actions = {'hide': 'show', 'show': 'hide',
# ____________________________________________________________ # ____________________________________________________________
# OptionDescription authorized group_type values # OptionDescription authorized group_type values
group_types = ['default', 'family', 'group', 'master'] group_types = ['default', 'family', 'group', 'master']
# multi types
class Multi(list):
"wrapper for list (multi) option types"
def __getitem__(self, key):
# FIXME test if None, etc...
return super(Multi, self).__getitem__(key)
# return list.__getitem__(self, key)
def __setitem__(self, key, value):
# FIXME do some stuff here
return super(Multi, self).__setitem__(key, value)
# ____________________________________________________________ # ____________________________________________________________
class Option(HiddenBaseType, DisabledBaseType, ModeBaseType): class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
#reminder: an Option object is **not** a container for the value #reminder: an Option object is **not** a container for the value
_frozen = False _frozen = False
@ -65,7 +77,7 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
if self.multi == True: if self.multi == True:
if default == None: if default == None:
default = [] default = []
if type(default) != list or not self.validate(default): if not isinstance(default, list) or not self.validate(default):
raise ConfigError("invalid default value {0} " raise ConfigError("invalid default value {0} "
"for option {1} : not list type".format(str(default), name)) "for option {1} : not list type".format(str(default), name))
else: else:
@ -80,7 +92,7 @@ class Option(HiddenBaseType, DisabledBaseType, ModeBaseType):
if value != None: if value != None:
return self._validate(value) return self._validate(value)
else: else:
if type(value) != list: if not isinstance(value, list):
raise ConfigError("invalid value {0} " raise ConfigError("invalid value {0} "
"for option {1} which must be a list".format(value, "for option {1} which must be a list".format(value,
self._name)) self._name))

View File

@ -36,7 +36,7 @@ def reverse_from_paths(data):
} }
def option_factory(name, value): def option_factory(name, value):
"dummy -> Option('dummy')" "dummy -> Option('dummy')"
if type(value) == list: if isinstance(value, list):
return _build_map[type(value[0])](name, '', multi=True, default=value) return _build_map[type(value[0])](name, '', multi=True, default=value)
else: else:
return _build_map[type(value)](name, '', default=value) return _build_map[type(value)](name, '', default=value)