Skip to content

Commit

Permalink
Merge pull request #2 from mklauser/pr136
Browse files Browse the repository at this point in the history
bug fix for the container handling. ToDo Remove remove general except in...
  • Loading branch information
wkerzendorf committed May 4, 2014
2 parents cea21d3 + 011c760 commit ed1d739
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
17 changes: 9 additions & 8 deletions tardis/data/tardis_config_definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ model:


filename:
property_type: string
default: None
mandatory: True
help: file name (with path) to structure model
property_type: string
default: None
mandatory: True
help: file name (with path) to structure model

filetype:
property_type: string
default: None
mandatory: True
help: file type
property_type: string
default: None
mandatory: True
help: file type

#### there are only a handful of types available how do we validate that ####

Expand Down Expand Up @@ -172,6 +172,7 @@ model:
property_type: container-declaration
containers: ['branch85_w7']
+branch85_w7: ['time_0', 'density_coefficient']
_branch85_w7: []


time_0:
Expand Down
96 changes: 49 additions & 47 deletions tardis/io/config_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ def __init__(self, container_default_dict, container_dict, container_path=None):
try:
necessary_items = container_default_dict['type'][entry_name]
except KeyError:
raise ValueError('Container insufficient specified')
raise ValueError('Container insufficient specified. %s is missing.'%entry_name)

#look for additional items
entry_name = '+' + self.__selected_container
Expand All @@ -970,7 +970,7 @@ def __init__(self, container_default_dict, container_dict, container_path=None):

if not self.__paper_abundances:
for item in necessary_items:
if not item in container_dict.keys():
if not item in container_dict:
raise ValueError('Entry %s is missing in container [%s]' % (str(item), self.__container_path))
else:
self.__default_container[item], self.__config_container[item] = self.parse_container_items(
Expand All @@ -993,51 +993,26 @@ def __init__(self, container_default_dict, container_dict, container_path=None):
else:
self.__conf = {"No Name": self.__config_container}

def parse_container_items(top_default, top_config, level_name, full_path):
"""Recursive parser for the container default dictionary and the container configuration
dictionary.
Parameters
----------
top_default: dict
container default dictionary of the upper recursion level
top_config: dict
container configuration dictionary of the upper recursion level
level_name: str
name(key) of the of the upper recursion level
path: list of str
path in the nested container dictionary from the main level to the current level
Returns
-------
If the current recursion level is not a leaf, the function returns a dictionary with itself for
each branch. If the current recursion level is a leaf the configured value and a configuration object is
returned
"""
path = reduce_list(list(full_path), self.__container_path + [item])
tmp_conf_ob = {}
tmp_conf_val = {}
if isinstance(top_default, dict):
default_property = DefaultParser(top_default)
if default_property.is_container():
container_conf = get_value_by_path(top_config, path)
ccontainer = Container(top_default, container_conf)
return ccontainer.get_container_ob(), ccontainer.get_container_conf()
elif not default_property.is_leaf:
for k, v in top_default.items():
tmp_conf_ob[k], tmp_conf_val[k] = parse_container_items(v, top_config, k, full_path + [k])
return tmp_conf_ob, tmp_conf_val
else:
default_property.set_path_in_dic(path)
try:
conf_value = get_value_by_path(top_config, path)
except KeyError:
conf_value = None

if conf_value is not None:
default_property.set_config_value(conf_value)

return default_property, default_property.get_value()
def parse_container_items(self, top_default, top_config, item, full_path):
"""Recursive parser for the container default dictionary and the container configuration
dictionary.
Parameters
----------
top_default: dict
container default dictionary of the upper recursion level
top_config: dict
container configuration dictionary of the upper recursion level
level_name: str
name(key) of the of the upper recursion level
path: list of str
path in the nested container dictionary from the main level to the current level
Returns
-------
If the current recursion level is not a leaf, the function returns a dictionary with itself for
each branch. If the current recursion level is a leaf the configured value and a configuration object is
returned
"""
def reduce_list(a, b):
"""
removes items from list a which are in b. (o.B.d.A trivial)
Expand Down Expand Up @@ -1074,6 +1049,33 @@ def get_value_by_path(_dict, path):
dict = _dict[key]
return _dict


path = reduce_list(list(full_path), self.__container_path + [item])
tmp_conf_ob = {}
tmp_conf_val = {}
if isinstance(top_default, dict):
default_property = DefaultParser(top_default)
if default_property.is_container():
container_conf = get_value_by_path(top_config, path)
ccontainer = Container(top_default, container_conf)
return ccontainer.get_container_ob(), ccontainer.get_container_conf()
elif not default_property.is_leaf:
for k, v in top_default.items():
tmp_conf_ob[k], tmp_conf_val[k] = parse_container_items(v, top_config, k, full_path + [k])
return tmp_conf_ob, tmp_conf_val
else:
default_property.set_path_in_dic(path)
try:
conf_value = get_value_by_path(top_config, path)
except KeyError:
conf_value = None

if conf_value is not None:
default_property.set_config_value(conf_value)

return default_property, default_property.get_value()


def get_container_ob(self):
"""
Return the container configuration object
Expand Down Expand Up @@ -1265,7 +1267,7 @@ def recursive_parser(top_default, configuration, path):
try:
ccontainer = Container(top_default, container_conf, container_path=path)
return ccontainer.get_container_ob(), ccontainer.get_container_conf()

#ToDo: remove general except!!!
except:
logger.warning('Container specified in default_configuration, but not used in the current\
configuration file. [%s]' % str(path))
Expand Down

0 comments on commit ed1d739

Please sign in to comment.