generate correct len for slave if no value

This commit is contained in:
Emmanuel Garette 2013-03-06 17:17:33 +01:00
parent c26b0ca12b
commit 9b3eb33d27
3 changed files with 56 additions and 12 deletions

View File

@ -57,11 +57,13 @@ class Config(object):
if parent is None: if parent is None:
self._cfgimpl_settings = Setting() self._cfgimpl_settings = Setting()
self._cfgimpl_values = Values(self._cfgimpl_context) self._cfgimpl_values = Values(self._cfgimpl_context)
self._cfgimpl_all_paths = {}
else: else:
if context is None: if context is None:
raise ConfigError("cannot find a value for this config") raise ConfigError("cannot find a value for this config")
self._cfgimpl_settings = None self._cfgimpl_settings = None
self._cfgimpl_values = None self._cfgimpl_values = None
self._cfgimpl_all_paths = None
"warnings are a great idea, let's make up a better use of it" "warnings are a great idea, let's make up a better use of it"
self._cfgimpl_warnings = [] self._cfgimpl_warnings = []
self._cfgimpl_toplevel = self._cfgimpl_get_toplevel() self._cfgimpl_toplevel = self._cfgimpl_get_toplevel()
@ -70,6 +72,13 @@ class Config(object):
self._cfgimpl_slots = [key for key, value in methods self._cfgimpl_slots = [key for key, value in methods
if not key.startswith("_")] if not key.startswith("_")]
self._cfgimpl_build() self._cfgimpl_build()
if parent is None:
self._cfgimpl_build_all_paths()
def _cfgimpl_build_all_paths(self):
if self._cfgimpl_all_paths == None:
raise ConfigError('cache paths must not be None')
self._cfgimpl_descr.build_cache(self._cfgimpl_all_paths)
def cfgimpl_get_settings(self): def cfgimpl_get_settings(self):
return self._cfgimpl_context._cfgimpl_settings return self._cfgimpl_context._cfgimpl_settings
@ -326,7 +335,16 @@ class Config(object):
subpath.insert(0, obj._cfgimpl_descr._name) subpath.insert(0, obj._cfgimpl_descr._name)
obj = obj._cfgimpl_parent obj = obj._cfgimpl_parent
return ".".join(subpath) return ".".join(subpath)
# ______________________________________________________________________
# def cfgimpl_previous_value(self, path):
# "stores the previous value"
# home, name = self._cfgimpl_get_home_by_path(path)
# # FIXME fucking name
# return home._cfgimpl_context._cfgimpl_values.previous_values[name]
# def get_previous_value(self, name):
# "for the time being, only the previous Option's value is accessible"
# return self._cfgimpl_context._cfgimpl_values.previous_values[name]
# ______________________________________________________________________ # ______________________________________________________________________
def add_warning(self, warning): def add_warning(self, warning):
"Config implements its own warning pile. Could be useful" "Config implements its own warning pile. Could be useful"
@ -413,15 +431,17 @@ class Config(object):
""" """
paths = [] paths = []
for path in self._cfgimpl_descr.getpaths(include_groups=include_groups): for path in self._cfgimpl_descr.getpaths(include_groups=include_groups):
if allpaths:
paths.append(path)
else:
try: try:
value = getattr(self, path) value = getattr(self, path)
except MandatoryError: except MandatoryError:
if mandatory or allpaths: if mandatory:
paths.append(path) paths.append(path)
except PropertiesOptionError: except PropertiesOptionError:
if allpaths: pass
paths.append(path) # option which have properties added
else: else:
paths.append(path) paths.append(path)
return paths return paths

View File

@ -444,6 +444,21 @@ class OptionDescription(BaseType):
else: else:
paths.append('.'.join(currpath + [attr])) paths.append('.'.join(currpath + [attr]))
return paths return paths
def build_cache(self, paths, currpath=None):
if currpath is None:
currpath = []
for option in self._children:
attr = option._name
if attr.startswith('_cfgimpl'):
continue
if isinstance(option, OptionDescription):
currpath.append(attr)
option.build_cache(paths, currpath=currpath)
currpath.pop()
else:
paths[option] = '.'.join(currpath + [attr])
# ____________________________________________________________ # ____________________________________________________________
def set_group_type(self, group_type): def set_group_type(self, group_type):
"""sets a given group object to an OptionDescription """sets a given group object to an OptionDescription

View File

@ -55,9 +55,18 @@ class Values(object):
if opt not in self.values: if opt not in self.values:
if opt.is_multi(): if opt.is_multi():
multitype = self._get_multitype(opt) multitype = self._get_multitype(opt)
return Multi(opt.getdefault(), self.context, opt, multitype) value = Multi(opt.getdefault(), self.context, opt, multitype)
else: else:
return opt.getdefault() value = opt.getdefault()
if opt in self.slaves:
masterpath = self.context._cfgimpl_all_paths[self.slaves[opt]]
mastervalue = getattr(self.context, masterpath)
masterlen = len(mastervalue)
if len(value) < masterlen:
for num in range(0, masterlen - len(value)):
value.append(None, force=True)
return value
return self.values[opt] return self.values[opt]
def reset(self, opt): def reset(self, opt):