Skip to content

Commit

Permalink
fix: Recursively update the configuration object
Browse files Browse the repository at this point in the history
Closes #519
  • Loading branch information
giulioungaretti committed Mar 10, 2017
1 parent 4651e11 commit 803d8da
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions qcodes/config/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import copy
import json
import logging
Expand Down Expand Up @@ -118,23 +119,25 @@ def update_config(self):
found in the directory.
"""
config = copy.deepcopy(self.defaults)
import pdb
pdb.set_trace()
self.current_schema = copy.deepcopy(self.defaults_schema)

if os.path.isfile(self.home_file_name):
home_config = self.load_config(self.home_file_name)
config.update(home_config)
config = update(config, home_config)
self.validate(config, self.current_schema,
self.schema_home_file_name)

if os.path.isfile(self.env_file_name):
env_config = self.load_config(self.env_file_name)
config.update(env_config)
config = update(config, env_config)
self.validate(config, self.current_schema,
self.schema_env_file_name)

if os.path.isfile(self.cwd_file_name):
cwd_config = self.load_config(self.cwd_file_name)
config.update(cwd_config)
config = update(config, cwd_config)
self.validate(config, self.current_schema,
self.schema_cwd_file_name)

Expand All @@ -161,9 +164,7 @@ def validate(self, json_config=None, schema=None, extra_schema_path=None):
# be overwritten
new_user = json.load(f)["properties"]["user"]
user = schema["properties"]['user']
user["properties"].update(
new_user["properties"]
)
user["properties"].update(new_user["properties"])
jsonschema.validate(json_config, schema)
else:
logger.warning(EMPTY_USER_SCHEMA.format(extra_schema_path))
Expand Down Expand Up @@ -232,11 +233,13 @@ def add(self, key, value, value_type=None, description=None, default=None):
# update schema!
schema_entry = {key: {"type": value_type}}
if description is not None:
schema_entry = {key: {
"type": value_type,
"default": default,
"description": description}
}
schema_entry = {
key: {
"type": value_type,
"default": default,
"description": description
}
}
# the schema is nested we only update properties of the user object
user = self.current_schema['properties']["user"]
user["properties"].update(schema_entry)
Expand Down Expand Up @@ -336,12 +339,7 @@ def describe(self, name):
# add cool description to docstring
base_docstring = """{}.\nCurrent value: {}. Type: {}. Default: {}."""

doc = base_docstring.format(
description,
val,
_type,
default
)
doc = base_docstring.format(description, val, _type, default)

return doc

Expand All @@ -357,15 +355,14 @@ def __getattr__(self, name):
def __repr__(self):
old = super().__repr__()
base = """Current values: \n {} \n Current path: \n {} \n {}"""
return base.format(self.current_config,
self.current_config_path,
old)
return base.format(self.current_config, self.current_config_path, old)


class DotDict(dict):
"""
Wrapper dict that allows to get dotted attributes
"""

def __init__(self, value=None):
if value is None:
pass
Expand Down Expand Up @@ -403,3 +400,13 @@ def __deepcopy__(self, memo):
# dot acces baby
__setattr__ = __setitem__
__getattr__ = __getitem__


def update(d, u):
for k, v in u.items():
if isinstance(v, collections.Mapping):
r = update(d.get(k, {}), v)
d[k] = r
else:
d[k] = u[k]
return d

0 comments on commit 803d8da

Please sign in to comment.